Ticket #9653: runservercp.py

File runservercp.py, 2.6 KB (added by lakin@…, 16 years ago)

A runservercp command which allows one to use the cherrypy server instead of the built in one.

Line 
1from django.core.management.base import BaseCommand, CommandError
2from optparse import make_option
3import os
4
5import cherrypy
6
7class Command(BaseCommand):
8 option_list = BaseCommand.option_list + (
9 make_option('--noreload', action='store_false', dest='use_reloader', default=True,
10 help='Tells Django to NOT use the auto-reloader.'),
11 make_option('--adminmedia', dest='admin_media_path', default='',
12 help='Specifies the directory from which to serve admin media.'),
13 )
14 help = "Starts a lightweight Web server for development."
15 args = '[optional port number, or ipaddr:port]'
16
17 # Validation is called explicitly each time the server is reloaded.
18 requires_model_validation = False
19
20 def handle(self, addrport='', *args, **options):
21 import django
22 from django.core.servers.basehttp import AdminMediaHandler
23 from django.core.handlers.wsgi import WSGIHandler
24 if args:
25 raise CommandError('Usage is runservercp %s' % self.args)
26 if not addrport:
27 addr = ''
28 port = '8000'
29 else:
30 try:
31 addr, port = addrport.split(':')
32 except ValueError:
33 addr, port = '', addrport
34 if not addr:
35 addr = '127.0.0.1'
36
37 if not port.isdigit():
38 raise CommandError("%r is not a valid port number." % port)
39
40 use_reloader = options.get('use_reloader', True)
41 admin_media_path = options.get('admin_media_path', '')
42
43 from django.conf import settings
44 from django.utils import translation
45 print "Validating models..."
46 self.validate(display_num_errors=True)
47 print "\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE)
48 print "Development server is running at http://%s:%s/" % (addr, port)
49
50 # django.core.management.base forces the locale to en-us. We should
51 # set it up correctly for the first request (particularly important
52 # in the "--noreload" case).
53 translation.activate(settings.LANGUAGE_CODE)
54
55 try:
56 path = admin_media_path or django.__path__[0] + '/contrib/admin/media'
57
58 handler = AdminMediaHandler(WSGIHandler(), path)
59 cherrypy.config.update({
60 'server.socket_host': addr,
61 'server.socket_port': int(port),
62 'engine.autoreload_on': use_reloader
63 })
64
65 cherrypy.tree.graft(handler, '/')
66
67 cherrypy.engine.start()
68 cherrypy.engine.block()
69 finally:
70 cherrypy.engine.exit()
71
Back to Top