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

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

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

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

import os

import cherrypy
from django.contrib.staticfiles.handlers import StaticFilesHandler
from django.core.management.base import BaseCommand, CommandError


class Command(BaseCommand):
    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):
        from django.core.handlers.wsgi import WSGIHandler
        if args:
            raise CommandError('Usage is runservercp %s' % self.args)

        def inner_run():
            from django.conf import settings
            from django.utils import translation

            translation.activate(settings.LANGUAGE_CODE)

            try:
                handler = StaticFilesHandler(WSGIHandler())

                print(os.path.abspath(os.getcwd()))
                print(os.path.abspath(os.getcwd()) + '/static')
                cherrypy.config.update({
                    'server.socket_host': "0.0.0.0",
                    'server.socket_port': 8080,
                    'server.thread_pool': 100,
                })
                cherrypy.tree.graft(handler, '/')
                cherrypy.engine.start()
                cherrypy.engine.block()
            finally:
                cherrypy.engine.exit()

        inner_run()

1275879-20180912160730959-316192453.png

转载于:https://www.cnblogs.com/abcyrf/p/9635385.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值