-
Notifications
You must be signed in to change notification settings - Fork 19
/
runtornado.py
101 lines (86 loc) · 4 KB
/
runtornado.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from django.core.management.base import BaseCommand, CommandError
from optparse import make_option
import os
import sys
class Command(BaseCommand):
option_list = BaseCommand.option_list + (
make_option('--reload', action='store_true',
dest='use_reloader', default=False,
help="Tells Tornado to use auto-reloader"),
make_option('--admin', action='store_true',
dest='admin_media', default=False,
help="Serve admin media"),
make_option('--adminmedia', dest='admin_media_path', default='',
help="Specifies the directory from which to serve admin media."),
)
help = "Starts a Tornado Web."
args = '[optional port number, or ipaddr:port]'
# Validation is called explicitly each time the server is reloaded.
requires_model_validation = False
def handle(self, addrport='', *args, **options):
import django
from django.core.handlers.wsgi import WSGIHandler
from tornado import httpserver, wsgi, ioloop
# reopen stdout/stderr file descriptor with write mode
# and 0 as the buffer size (unbuffered).
# XXX: why?
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
sys.stderr = os.fdopen(sys.stderr.fileno(), 'w', 0)
if args:
raise CommandError('Usage is runserver %s' % self.args)
if not addrport:
addr = ''
port = '8000'
else:
try:
addr, port = addrport.split(':')
except ValueError:
addr, port = '', addrport
if not addr:
addr = '127.0.0.1'
if not port.isdigit():
raise CommandError("%r is not a valid port number." % port)
use_reloader = options.get('use_reloader', False)
serve_admin_media = options.get('admin_media', False)
admin_media_path = options.get('admin_media_path', '')
shutdown_message = options.get('shutdown_message', '')
quit_command = (sys.platform == 'win32') and 'CTRL-BREAK' or 'CONTROL-C'
def inner_run():
from django.conf import settings
from django.utils import translation
print "Validating models..."
self.validate(display_num_errors=True)
print "\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE)
print "Server is running at http://%s:%s/" % (addr, port)
print "Quit the server with %s." % quit_command
# django.core.management.base forces the locate to en-us. We
# should set it up correctly for the first request
# (particularly important in the not "--reload" case).
translation.activate(settings.LANGUAGE_CODE)
try:
# Instance Django's wsgi handler.
if serve_admin_media:
# Enable admin media wsgi middleware.
# Only use it in development mode!.
from django.core.servers.basehttp import AdminMediaHandler
application = AdminMediaHandler(WSGIHandler(),
admin_media_path)
else:
application = WSGIHandler()
# Wrap Django's wsgi application on Tornado's wsgi
# container.
container = wsgi.WSGIContainer(application)
# start tornado web server in single-threaded mode
# instead auto pre-fork mode with bind/start.
http_server = httpserver.HTTPServer(container)
http_server.listen(int(port), address=addr)
ioloop.IOLoop.instance().start()
except KeyboardInterrupt:
if shutdown_message:
print shutdown_message
sys.exit(0)
if use_reloader:
# Use tornado reload to handle IOLoop restarting.
from tornado import autoreload
autoreload.start()
inner_run()