码迷,mamicode.com
首页 > Web开发 > 详细

django采用cherrypy作为轻量级web服务器。

时间:2018-09-12 16:58:25      阅读:569      评论:0      收藏:0      [点我收藏+]

标签:correct   server   添加   ons   %s   git   seh   gis   control   

https://www.xuebuyuan.com/zh-hant/1547838.html

新建app,并添加到settings.py
在app下增加management包。

將以下代碼保存為文件runservercp.py,放到management/commands/runservercp.py

from django.core.management.base import BaseCommand, CommandError  
from optparse import make_option  
import os, sys  
import cherrypy  
from cherrypy import wsgiserver  
class Command(BaseCommand):  
    option_list = BaseCommand.option_list + (  
        make_option(‘--noreload‘, action=‘store_false‘, dest=‘use_reloader‘, default=True,  
            help=‘Tells Django to NOT use the auto-reloader.‘),  
        make_option(‘--adminmedia‘, dest=‘admin_media_path‘, default=‘‘,  
            help=‘Specifies the directory from which to serve admin media.‘),  
    )  
    help = "Starts a lightweight Web server for development."  
    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.servers.basehttp import AdminMediaHandler  
        from django.core.handlers.wsgi import WSGIHandler  
        if args:  
            raise CommandError(‘Usage is runservercp %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‘, True)  
        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 "\nValidating models..."  
            self.validate(display_num_errors=True)  
            print "\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE)  
            print "Development server is running at http://%s:%s/" % (addr, port)  
            print "Quit the server with %s." % quit_command  
              
            # django.core.management.base forces the locale to en-us. We should  
            # set it up correctly for the first request (particularly important  
            # in the "--noreload" case).  
            translation.activate(settings.LANGUAGE_CODE)  
              
            try:  
                path = admin_media_path or django.__path__[0] + ‘/contrib/admin/media‘  
                handler = AdminMediaHandler(WSGIHandler(), path)  
                cherrypy.config.update({  
                        ‘server.socket_host‘: addr,  
                        ‘server.socket_port‘: int(port),  
                        ‘engine.autoreload_on‘:  use_reloader  
                    })  
      
                cherrypy.tree.graft(handler, ‘/‘)  
      
                cherrypy.engine.start()  
                cherrypy.engine.block()  
            finally:  
                if shutdown_message:  
                    print shutdown_message  
                cherrypy.engine.exit()  
        inner_run()  

技术分享图片

django采用cherrypy作为轻量级web服务器。

标签:correct   server   添加   ons   %s   git   seh   gis   control   

原文地址:https://www.cnblogs.com/abcyrf/p/9635385.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!