Skip to content

Commit b68c478

Browse files
Added 'django-admin.py runserver', which starts a lightweight development server running Django on a local port
git-svn-id: http://code.djangoproject.com/svn/django/trunk@174 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent fbfa3d6 commit b68c478

File tree

3 files changed

+617
-0
lines changed

3 files changed

+617
-0
lines changed

django/bin/django-admin.py

+31
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,30 @@ def startapp(app_name, directory):
362362
startapp.help_doc = "Creates a Django app directory structure for the given app name in the current directory."
363363
startapp.args = "[appname]"
364364

365+
def runserver(port):
366+
"Starts a lightweight Web server for development."
367+
from django.core.servers.basehttp import run, WSGIServerException
368+
from django.core.handlers.wsgi import WSGIHandler
369+
if not port.isdigit():
370+
sys.stderr.write("Error: %r is not a valid port number.\n" % port)
371+
sys.exit(1)
372+
print "Starting server on port %s. Go to http://127.0.0.1:%s/ for Django." % (port, port)
373+
try:
374+
run(int(port), WSGIHandler())
375+
except WSGIServerException, e:
376+
# Use helpful error messages instead of ugly tracebacks.
377+
ERRORS = {
378+
13: "You don't have permission to access that port.",
379+
98: "That port is already in use.",
380+
}
381+
try:
382+
error_text = ERRORS[e.args[0].args[0]]
383+
except (AttributeError, KeyError):
384+
error_text = str(e)
385+
sys.stderr.write("Error: %s\n" % error_text)
386+
sys.exit(1)
387+
runserver.args = '[optional port number]'
388+
365389
def usage():
366390
sys.stderr.write("Usage: %s [action]\n" % sys.argv[0])
367391

@@ -376,6 +400,7 @@ def usage():
376400
ACTION_MAPPING = {
377401
'adminindex': get_admin_index,
378402
# 'dbcheck': database_check,
403+
'runserver': runserver,
379404
'sql': get_sql_create,
380405
'sqlall': get_sql_all,
381406
'sqlclear': get_sql_delete,
@@ -406,6 +431,12 @@ def usage():
406431
usage()
407432
ACTION_MAPPING[action](name, os.getcwd())
408433
sys.exit(0)
434+
elif action == 'runserver':
435+
if len(sys.argv) < 3:
436+
port = '8000'
437+
else:
438+
port = sys.argv[2]
439+
ACTION_MAPPING[action](port)
409440
elif action == 'dbcheck':
410441
from django.core import meta
411442
mod_list = meta.get_all_installed_modules()

django/core/servers/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)