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

Posted abcyrf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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()

以上是关于django采用cherrypy作为轻量级web服务器。的主要内容,如果未能解决你的问题,请参考以下文章

CherryPy 与 Cheetah 作为插件 + 工具 - 空白页

如何将 StringTemplate 引擎集成到 CherryPy Web 服务器中

django的入门-----管理站点

更改 Cherrypy 端口并重新启动 Web 服务器

有哪些python框架需要学习

Cherrypy中的RESTful Web服务示例