【问题标题】:Flask passing another url through url variableFlask 通过 url 变量传递另一个 url
【发布时间】:2017-04-03 07:58:37
【问题描述】:

我正在尝试从头开始通过烧瓶构建一个 cors 代理。这是我的代码

@app.route('/api/v1/cors/url=<name>&method=<method>', methods=['GET'])
def api_cors(name, method):
    if method == 'http' or method == 'https':
        r = request.urlopen(method+"://"+name)
        return r.read()
    else:
        return "method not set!"

到目前为止它运行良好,但我有一个问题,当我通过“url=google.com&method=https”时它运行正常但是当我通过类似“url=google.com/images/image.jpg&method= https" "/" 将被视为一个新目录

在烧瓶中无论如何可以避免这个?

【问题讨论】:

标签: python python-3.x flask web-development-server


【解决方案1】:

不要尝试将值作为路由本身的一部分传递。将其作为查询参数传递。

@app.route('/api/v1/cors/')
def api_cors():
    url = request.args.get('url')

并将其称为"/api/v1/cors/?url=https://google.com/images/image.jpg"

【讨论】:

  • 嘿,我在做和你一样的事情时遇到属性错误
  • AttributeError: 'module' 对象没有属性 'args'
  • 我做了导入请求顺便说一句,:从烧瓶导入请求
  • 你有一个request.py 可以在某处导入吗?
  • 哪里有?它不应该作为一个包来,而且我没有收到任何导入错误
【解决方案2】:

如果您想使用与现在相同的 URL 方案,请将您的路由装饰器更改为此,它会起作用。

@app.route('/api/v1/cors/url=<path:name>&method=<method>', methods=['GET'])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-03
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 2011-11-03
    • 2011-09-25
    • 2016-09-30
    相关资源
    最近更新 更多