在Flask上下文中的Python get Request失败,出现504 Gateway Error

时间:2018-04-13 08:58:25

标签: python flask python-requests consul

  

我想为用户创建一个接口,以便请求一个KV基础的REST API

     

对于这种需要,我使用了烧瓶,请求和Python(用2.7和3.6测试)

     

请求:2.18.4,Flask:0.12.2

用户可以访问的API就是这样写的:

目的是客户端可以访问/ getval并检索KV中的值

# for testing
@app.route("/")
def hello():
    return "Hello From PYTHON"

@app.route("/getval")
def getval():
    val0 = getKVval(url)
    return val0

我在此处定义了从我的KV产品中检索价值的电话

def getKVval(url):
    r = requests.get(url)
    # If Key does not exist set to defaultVal
    if r.status_code == 404:
        setConsulVal(url, defaultval)
        return defaultval
    o = json.loads(r.content)
    valb64 = o[0]['Value']
    dec = base64.b64decode(valb64)
    return dec

当我启动python进程时,一切正常

Restarting with stat
Debugger is active!
Debugger PIN: 241-055-893
Running on http://127.0.0.1:5000/ 

进程已启动,客户端可以在默认端口(5000)

上访问
$ curl 127.0.0.1:5000
Hello From PYTHON App
  

尝试访问/ getval时会出现问题

当我在没有烧瓶的情况下调用我的函数getKVval时,它可以完美地运行

StatusCode : 200
[{u'LockIndex': 0, u'ModifyIndex': 34, u'Value': u'Mw==', u'Flags': 0, u'Key': u'mysqld/serverid', u'CreateIndex': 31}]
Mw==
3

但是,当我尝试使用/ getval访问时,它会崩溃错误

127.0.0.1 - - [13/Apr/2018 10:44:53] "GET /getval HTTP/1.1" 500 -
    Traceback (most recent call last):
    File "...venv\lib\site-packages\flask\app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
File "...venv\lib\site-packages\flask\app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
File "...venv\lib\site-packages\flask\app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
File "...venv\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
File "...venv\lib\site-packages\flask\app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
File "...venv\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
File "...venv\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
File "...venv\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
File "...venv\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
File "...venv\lib\site-packages\flask\app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
File "...interface\interface.py", line 82, in getval
    val0 = getConsulVal(url)
File "...interface\interface.py", line 36, in getConsulVal
    o = json.loads(r.content)
File "....Python36\lib\json\__init__.py", line 354, in loads
    return _default_decoder.decode(s)
File "....Python36\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "....Python36\lib\json\decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我做了很多调试(有/没有代理配置),打印输出......但现在找不到任何东西

编辑&解决方案:

在我的应用中发现了一些在错误之前返回的日志:

  

('状态代码:',504)

     

HTTP 504 IS' GATEWAY TIMEOUT'错误

所以我做了一些更多的测试来停用/配置代理

我尝试了这个没有解决的配置:

proxies = {
    "http": None,
    "https": None,
}

然后用

调用它
r = requests.get(url, proxies)

结果:没有成功......

最终解决方案:

os.environ['NO_PROXY'] = '127.0.0.1'

0 个答案:

没有答案