使用cherrypy发出HTTP请求

约尼·利维(Yoni Levy)

我已经搜索了很长时间,但是找不到有关如何使用cherrypy创建HTTP请求的任何文档。

我想实现以下目标:

@cherrypy.expose
def index(self):

    json = http_request("http://somesite/")

    processed = process_json(json)

    tmpl = env.get_template("template.html")
    return tmpl.render(data=processed)

知道我该如何实现吗?

安德鲁·克鲁斯(Andrew Kloos)

如果您正在谈论python 3,则需要urllib。

import urllib.request

@cherrypy.tools.json_in() 
@cherrypy.expose
def index(self):    
    httpreq = urllib.request.Request(url="http://somesite/")
    response = urllib.request.urlopen(httpreq)
    jsonobject = response.read()

让我知道您是否需要其他版本的python。

希望这可以帮助!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章