我创建了一个简单的heroku应用程序,它以url作为参数,读取url数据并将其显示在浏览器中。这是代码:
import os
from flask import Flask
import urllib2, urlparse
app = Flask(__name__)
@app.route('/<url>')
def getdata(url):
url.replace('www.','')
if url.count('http') == 0:
url = 'http://'+url
u = urlparse.urlparse(url)
nurl = u.geturl()
response = urllib2.urlopen(nurl).read()
return response
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
当传递像 google.com
这样的网址时,它工作正常。但是当传递像 google.com/images
这样的内容时(我知道网址返回 503),它会抛出 404 错误。当我尝试在 python shell 中正常读取子网址时,它工作得很好。
为什么会发生这种情况以及如何解决它?
最佳答案
尝试使用path option in the @route
。像下面这样的东西应该有效:
@app.route('/<path:url>')
def getdata(url):
...
关于python - Flask 应用程序无法解析子网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13920958/