为什么瓶子不显示我返回的页面?

why bottle does not show the page I returned?

我是瓶子新手。我已经搜索了答案,但找不到任何答案。

没有 redirect 是否可以加载页面?

代码是这样的:

from bottle import get, run, template

@get('/list/item')
def listItems():
    r = requests.get('my/url/which/works/')
    return r.content

if __name__ == '__main__':
    run(host='localhost', port=8080 )

网页是空的。我也试过 return r.textreturn template(r.content)return str(r.content)return str(r.content.decode('utf-8'))

nf = urllib.urlopen('my/url/whick/works')
return nf.read()

none 其中 returns 个我想要的页面。

但是,如果我写这个 return r.content[0],它就可以工作。该页面将显示第一个字母,即“<”。但是如果我写return r.content[0:100],它又是returns一个空页。

如果我运行命令行中的请求,这就是它returns:

>>> import requests
>>> r = requests.get('my/url/which/works/')
>>>
>>> r.content
'<?xml version="1.0" encoding="utf-8" ?> \n<body copyright="...>\n</body>\n'

是否有人可以提供帮助?非常感谢。

您的问题没有具体说明当您的代码按预期工作时您希望看到什么,但这可能对您有所帮助:

from bottle import get, run, template, response

@get('/list/item')
def listItems():
    r = requests.get('my/url/which/works/')
    response.content_type = 'text/plain'  # added this
    return r.content

...

唯一的变化是您的网络服务器现在将响应的内容类型设置为 text/plain,这应该会使您的浏览器呈现页面。

从长远来看,如果(正如我从您的问题中推断的那样)您打算 return XML 回复,那么我建议安装浏览器插件 (here's an example),或者,更好的是,使用 curlwget 来查看准确的响应。这将帮助您调试服务器的响应。