Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python CherryPy:创建在apache2(mod_wsgi)后面运行的Web服务_Python_Apache_Rest_Cherrypy - Fatal编程技术网

Python CherryPy:创建在apache2(mod_wsgi)后面运行的Web服务

Python CherryPy:创建在apache2(mod_wsgi)后面运行的Web服务,python,apache,rest,cherrypy,Python,Apache,Rest,Cherrypy,我是cherrypy的新手,选择它来创建web服务,以便在其他web应用程序中使用。我想使用apache2和mod_wsgi运行它。我遵循了相反的方法,hello world的例子效果很好 我现在看的是turotials当然还有。但是我无法让它运行。我在apache日志中获得状态500和一个错误: TypeError: expose_() missing 1 required positional argument: 'func' 为了达到这一目的,我调整了教程中的脚本,类似于hello wo

我是cherrypy的新手,选择它来创建web服务,以便在其他web应用程序中使用。我想使用apache2和mod_wsgi运行它。我遵循了相反的方法,hello world的例子效果很好

我现在看的是turotials当然还有。但是我无法让它运行。我在apache日志中获得状态500和一个错误:

TypeError: expose_() missing 1 required positional argument: 'func'
为了达到这一目的,我调整了教程中的脚本,类似于hello world示例,以便与apache一起使用:

import sys
sys.stdout = sys.stderr

import random
import string

import cherrypy

cherrypy.config.update({'environment': 'embedded'})

@cherrypy.expose
class StringGeneratorWebService(object):

    @cherrypy.tools.accept(media='text/plain')
    def GET(self):
        return cherrypy.session['mystring']

    def POST(self, length=8):
        some_string = ''.join(random.sample(string.hexdigits, int(length)))
        cherrypy.session['mystring'] = some_string
        return some_string

    def PUT(self, another_string):
        cherrypy.session['mystring'] = another_string

    def DELETE(self):
        cherrypy.session.pop('mystring', None)


conf = {
    '/': {
        'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
        'tools.sessions.on': True,
        'tools.response_headers.on': True,
        'tools.response_headers.headers': [('Content-Type', 'text/plain')],
    }
}
cherrypy.quickstart(StringGeneratorWebService(), '/', conf)
我做错了什么?

第1期:

TypeError: expose_() missing 1 required positional argument: 'func'
这是因为我使用的是anaconda python,而随
conda install cherrypy
安装的cherrypy版本已经过时(3.8.0)。删除该版本并使用pip安装最新版本解决了此问题

问题2:

错误的路线

cherrypy.quickstart(StringGeneratorWebService(), '/', conf)
应该是

cherrypy.Application(StringGeneratorWebService(), script_name=None, config=conf)
然后只需输入脚本文件的路径即可

问题3:

cherrypy会话是内存中的默认会话,这在mod_wsgi中不起作用。您需要为会话使用文件存储,例如调整配置:

conf = {
'/': {
    'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
    'tools.sessions.on': True,
    'tools.sessions.storage_type': 'file',
    'tools.sessions.storage_path': '/path/to/sessions', # in case of 500 error check privileges of session folder!!!
    'tools.response_headers.on': True,
    'tools.response_headers.headers': [('Content-Type', 'text/plain')]
}

}

这是他们的一篇教程中的直接内容。错误是
cherrypy.quickstart
vs
cherrypy.Application
。就像这样,它是有效的,任何有同样问题的人都不应该投否决票。注意:就我个人而言,我需要让这个工作为我的骄傲,但我现在建议选择另一个框架,因为文档是稀疏的,往往是过时的。