Fixed #360 -- runserver now takes optional 'ip:port' in addition to 'port'. Thanks, benno@jeamland.net

git-svn-id: http://code.djangoproject.com/svn/django/trunk@539 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2005-08-19 21:23:56 +00:00
parent 8d6c276328
commit 00c6acaaf3
4 changed files with 30 additions and 11 deletions

View File

@ -87,10 +87,14 @@ def main():
ACTION_MAPPING[action](name, os.getcwd()) ACTION_MAPPING[action](name, os.getcwd())
elif action == 'runserver': elif action == 'runserver':
if len(args) < 2: if len(args) < 2:
addr = ''
port = '8000' port = '8000'
else: else:
port = args[1] try:
ACTION_MAPPING[action](port) addr, port = args[1].split(':')
except ValueError:
addr, port = '', args[1]
ACTION_MAPPING[action](addr, port)
else: else:
from django.core import meta from django.core import meta
if action == 'dbcheck': if action == 'dbcheck':

View File

@ -543,10 +543,12 @@ def validate():
print '%s error%s found.' % (num_errors, num_errors != 1 and 's' or '') print '%s error%s found.' % (num_errors, num_errors != 1 and 's' or '')
validate.args = '' validate.args = ''
def runserver(port): def runserver(addr, port):
"Starts a lightweight Web server for development." "Starts a lightweight Web server for development."
from django.core.servers.basehttp import run, AdminMediaHandler, WSGIServerException from django.core.servers.basehttp import run, AdminMediaHandler, WSGIServerException
from django.core.handlers.wsgi import WSGIHandler from django.core.handlers.wsgi import WSGIHandler
if not addr:
addr = '127.0.0.1'
if not port.isdigit(): if not port.isdigit():
sys.stderr.write("Error: %r is not a valid port number.\n" % port) sys.stderr.write("Error: %r is not a valid port number.\n" % port)
sys.exit(1) sys.exit(1)
@ -555,15 +557,16 @@ def runserver(port):
print "Validating models..." print "Validating models..."
validate() validate()
print "\nStarting server on port %s with settings module %r." % (port, SETTINGS_MODULE) print "\nStarting server on port %s with settings module %r." % (port, SETTINGS_MODULE)
print "Go to http://127.0.0.1:%s/ for Django." % port print "Go to http://%s:%s/ for Django." % (addr, port)
print "Quit the server with CONTROL-C (Unix) or CTRL-BREAK (Windows)." print "Quit the server with CONTROL-C (Unix) or CTRL-BREAK (Windows)."
try: try:
run(int(port), AdminMediaHandler(WSGIHandler())) run(addr, int(port), AdminMediaHandler(WSGIHandler()))
except WSGIServerException, e: except WSGIServerException, e:
# Use helpful error messages instead of ugly tracebacks. # Use helpful error messages instead of ugly tracebacks.
ERRORS = { ERRORS = {
13: "You don't have permission to access that port.", 13: "You don't have permission to access that port.",
98: "That port is already in use.", 98: "That port is already in use.",
99: "That IP address can't be assigned-to.",
} }
try: try:
error_text = ERRORS[e.args[0].args[0]] error_text = ERRORS[e.args[0].args[0]]
@ -575,4 +578,4 @@ def runserver(port):
sys.exit(0) sys.exit(0)
from django.utils import autoreload from django.utils import autoreload
autoreload.main(inner_run) autoreload.main(inner_run)
runserver.args = '[optional port number]' runserver.args = '[optional port number, or ipaddr:port]'

View File

@ -636,8 +636,8 @@ class AdminMediaHandler:
start_response(status, headers.items()) start_response(status, headers.items())
return output return output
def run(port, wsgi_handler): def run(addr, port, wsgi_handler):
server_address = ('', port) server_address = (addr, port)
httpd = WSGIServer(server_address, WSGIRequestHandler) httpd = WSGIServer(server_address, WSGIRequestHandler)
httpd.set_app(wsgi_handler) httpd.set_app(wsgi_handler)
httpd.serve_forever() httpd.serve_forever()

View File

@ -80,11 +80,12 @@ install [app app ...]
Executes the equivalent of ``sqlall`` for the given app(s). Executes the equivalent of ``sqlall`` for the given app(s).
runserver [optional port number] runserver [optional port number, or ipaddr:port]
-------------------------------- ------------------------------------------------
Starts a lightweight development Web server on the local machine. By default, Starts a lightweight development Web server on the local machine. By default,
the server runs on port 8000. You can pass in a port number explicitly. the server runs on port 8000 on the IP address 127.0.0.1. You can pass in an
IP address and port number explicitly.
If you run this script as a user with normal privileges (recommended), you If you run this script as a user with normal privileges (recommended), you
might not have access to start a port on a low port number. Low port numbers might not have access to start a port on a low port number. Low port numbers
@ -103,6 +104,17 @@ them to standard output, but it won't stop the server.
You can run as many servers as you want, as long as they're on separate ports. You can run as many servers as you want, as long as they're on separate ports.
Just execute ``django-admin.py runserver`` more than once. Just execute ``django-admin.py runserver`` more than once.
Examples:
~~~~~~~~~
Port 7000 on IP address 127.0.0.1::
django-admin.py runserver 7000
Port 7000 on IP address 1.2.3.4::
django-admin.py runserver 1.2.3.4:7000
sql [app app ...] sql [app app ...]
----------------- -----------------