|
22
|
1 |
from django.core.management.commands.runserver import Command as RunserverCommand |
|
|
2 |
from django.core.servers.basehttp import WSGIServer, WSGIRequestHandler |
|
|
3 |
import sys |
|
|
4 |
import threading |
|
|
5 |
from threading import Event |
|
|
6 |
from django.conf import settings |
|
|
7 |
|
|
|
8 |
def run(addr, port, wsgi_handler, keep_running=None, ready_event=None): |
|
|
9 |
server_address = (addr, port) |
|
|
10 |
httpd = WSGIServer(server_address, WSGIRequestHandler) |
|
|
11 |
httpd.set_app(wsgi_handler) |
|
|
12 |
if keep_running is None: |
|
|
13 |
if ready_event is not None: |
|
|
14 |
ready_event.set() |
|
|
15 |
httpd.serve_forever() |
|
|
16 |
else: |
|
|
17 |
if ready_event is not None: |
|
|
18 |
ready_event.set() |
|
|
19 |
while keep_running(): |
|
|
20 |
httpd.handle_request() |
|
|
21 |
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
class Command(RunserverCommand): |
|
|
25 |
def handle(self, addrport='', keep_running=None, ready_event=None, *args, **options): |
|
|
26 |
import django |
|
|
27 |
from django.core.servers.basehttp import AdminMediaHandler, WSGIServerException |
|
|
28 |
from django.core.handlers.wsgi import WSGIHandler |
|
|
29 |
if args: |
|
|
30 |
raise CommandError('Usage is testrunserver %s' % self.args) |
|
|
31 |
if not addrport: |
|
|
32 |
addr = '' |
|
|
33 |
port = '8000' |
|
|
34 |
else: |
|
|
35 |
try: |
|
|
36 |
addr, port = addrport.split(':') |
|
|
37 |
except ValueError: |
|
|
38 |
addr, port = '', addrport |
|
|
39 |
if not addr: |
|
|
40 |
addr = '127.0.0.1' |
|
|
41 |
|
|
|
42 |
if not port.isdigit(): |
|
|
43 |
raise CommandError("%r is not a valid port number." % port) |
|
|
44 |
|
|
|
45 |
admin_media_path = options.get('admin_media_path', '') |
|
|
46 |
shutdown_message = options.get('shutdown_message', '') |
|
|
47 |
quit_command = (sys.platform == 'win32') and 'CTRL-BREAK' or 'CONTROL-C' |
|
|
48 |
|
|
|
49 |
from django.conf import settings |
|
|
50 |
from django.utils import translation |
|
|
51 |
print "Validating models..." |
|
|
52 |
self.validate(display_num_errors=True) |
|
|
53 |
print "\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE) |
|
|
54 |
print "Development server is running at http://%s:%s/" % (addr, port) |
|
|
55 |
print "Quit the server with %s." % quit_command |
|
|
56 |
|
|
|
57 |
translation.activate(settings.LANGUAGE_CODE) |
|
|
58 |
|
|
|
59 |
try: |
|
|
60 |
handler = AdminMediaHandler(WSGIHandler(), admin_media_path) |
|
|
61 |
run(addr, int(port), handler, keep_running, ready_event) |
|
|
62 |
except WSGIServerException, e: |
|
|
63 |
# Use helpful error messages instead of ugly tracebacks. |
|
|
64 |
ERRORS = { |
|
|
65 |
13: "You don't have permission to access that port.", |
|
|
66 |
98: "That port is already in use.", |
|
|
67 |
99: "That IP address can't be assigned-to.", |
|
|
68 |
} |
|
|
69 |
try: |
|
|
70 |
error_text = ERRORS[e.args[0].args[0]] |
|
|
71 |
except (AttributeError, KeyError): |
|
|
72 |
error_text = str(e) |
|
|
73 |
sys.stderr.write(self.style.ERROR("Error: %s" % error_text) + '\n') |
|
|
74 |
# Need to use an OS exit because sys.exit doesn't work in a thread |
|
|
75 |
except KeyboardInterrupt: |
|
|
76 |
if shutdown_message: |
|
|
77 |
print shutdown_message |
|
|
78 |
|
|
|
79 |
|