[英]How can I generate multiple URL paths with cherrypy?
I feel like I'm running into a brick wall, as I'm not getting anywhere with this, and I believe, simple task. 我觉得自己正在碰壁,因为我无法做到这一点,而且我相信这是简单的任务。
I'm trying to generate a URL by the likes of '/path/to/url', but upon gazing at multiple StackOverflow Q&A's, the official documentation for cherrypy, I still cannot seem to wrap my head around the issue. 我正在尝试通过“ / path / to / url”之类的方法生成一个URL,但是凝视了多个StackOverflow问答,Cherrypy的官方文档后,我仍然似乎无法解决这个问题。
Here's my code so far: 到目前为止,这是我的代码:
import details
import input_checker as input
import time
import cherrypy
class Index(object):
@cherrypy.expose
def input(self):
return input.check_input()
@cherrypy.expose
def stream(self):
while True:
return 'Hey'
#return input.check_input()
time.sleep(3)
if __name__ == '__main__':
index = Index()
cherrypy.tree.mount(index.stream(), '/input/stream', {})
cherrypy.config.update(
{'server.socket_host': '0.0.0.0'})
cherrypy.quickstart(index)
So essentially, I want to be able to visit http://127.0.0.1:8080/input/stream , and I will be returned with the given result. 因此,从本质上讲,我希望能够访问http://127.0.0.1:8080/input/stream ,并且将返回给定的结果。
After executing this code, and multiple variants of it, I'm still being returned with a 404 not found error, and I'm not sure what I need to do, in order to get it working. 在执行完这段代码及其多种变体之后,我仍然返回404 not found错误,并且不确定要使其正常工作需要做什么。
Any tips and/or supporting documentation that I may have skimmed over? 我可能会略过的任何提示和/或支持文档?
Thanks guys. 多谢你们。
So there are couple problems here, why do you use MethodDispatcher
do you actually need it? 所以这里有几个问题,为什么您实际上需要使用
MethodDispatcher
?
To serve you stream
function on /input/stream
you have to mount it as such: 为了在
/input/stream
上提供stream
功能,您必须这样安装它:
cherrypy.tree.mount(index.stream(), '/input/stream', your_config)
note /input/stream
instead of /stream
. 注意
/input/stream
而不是/stream
。
But because you're using MethodDispatcher
this will likely make your endpoint return 405 as GET is not allowed on this endpoint - to fix that just remove the MethodDispatcher
bit. 但是因为您使用的是
MethodDispatcher
这很可能会使您的端点返回405,因为此端点上不允许执行MethodDispatcher
要解决此问题,只需删除MethodDispatcher
位即可。
But if you do require MethodDispatcher
you will have to refactor a bit to something like that: 但是,如果确实需要
MethodDispatcher
,则必须将其重构为类似以下内容:
class Stream:
exposed = True # to let cherrypy know that we're exposing all methods in this one
def GET(self):
return something
stream = Stream()
cherrypy.tree.mount(stream , '/index/stream',
{'/':
{'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
}
)
Also make sure to not actually call your methods when mounting them into cherrypy tree, just pass in the name of the function/class 此外,请确保在将它们装入樱桃树时,不实际调用您的方法,只需传入函数/类的名称即可
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.