gpt4 book ai didi

python - Flask 崩溃并出现 ValueError : too many values to unpack

转载 作者:太空狗 更新时间:2023-10-29 22:08:07 25 4
gpt4 key购买 nike

我有一个与另一个网络服务通信的 flask 应用程序。我有这个错误,似乎只有当两个应用程序在同一台服务器上运行时才会发生,但我不知道来源是什么。 Flask 应用程序通过 Apache 中的 WSGIScriptAlias 托管在 /tools

[Thu May 23 13:11:44 2013] [error] [client 41.164.8.114] mod_wsgi (pid=25705): Exception occurred processing WSGI script '/opt/tools-frontend/wsgi.py'.
[Thu May 23 13:11:44 2013] [error] [client 41.164.8.114] Traceback (most recent call last):
[Thu May 23 13:11:44 2013] [error] [client 41.164.8.114] File "/opt/tools-frontend/ENV_1/lib/python2.7/site-packages/flask/app.py", line 1701, in __call__
[Thu May 23 13:11:44 2013] [error] [client 41.164.8.114] return self.wsgi_app(environ, start_response)
[Thu May 23 13:11:44 2013] [error] [client 41.164.8.114] File "/opt/tools-frontend/ENV_1/lib/python2.7/site-packages/flask/app.py", line 1689, in wsgi_app
[Thu May 23 13:11:44 2013] [error] [client 41.164.8.114] response = self.make_response(self.handle_exception(e))
[Thu May 23 13:11:44 2013] [error] [client 41.164.8.114] File "/opt/tools-frontend/ENV_1/lib/python2.7/site-packages/flask/app.py", line 1687, in wsgi_app
[Thu May 23 13:11:44 2013] [error] [client 41.164.8.114] response = self.full_dispatch_request()
[Thu May 23 13:11:44 2013] [error] [client 41.164.8.114] File "/opt/tools-frontend/ENV_1/lib/python2.7/site-packages/flask/app.py", line 1361, in full_dispatch_request
[Thu May 23 13:11:44 2013] [error] [client 41.164.8.114] response = self.make_response(rv)
[Thu May 23 13:11:44 2013] [error] [client 41.164.8.114] File "/opt/tools-frontend/ENV_1/lib/python2.7/site-packages/flask/app.py", line 1447, in make_response
[Thu May 23 13:11:44 2013] [error] [client 41.164.8.114] rv = self.response_class(rv, headers=headers, status=status)
[Thu May 23 13:11:44 2013] [error] [client 41.164.8.114] File "/opt/tools-frontend/ENV_1/lib/python2.7/site-packages/werkzeug/wrappers.py", line 627, in __init__
[Thu May 23 13:11:44 2013] [error] [client 41.164.8.114] self.headers = Headers(headers)
[Thu May 23 13:11:44 2013] [error] [client 41.164.8.114] File "/opt/tools-frontend/ENV_1/lib/python2.7/site-packages/werkzeug/datastructures.py", line 836, in __init__
[Thu May 23 13:11:44 2013] [error] [client 41.164.8.114] self.extend(defaults)
[Thu May 23 13:11:44 2013] [error] [client 41.164.8.114] File "/opt/tools-frontend/ENV_1/lib/python2.7/site-packages/werkzeug/datastructures.py", line 978, in extend
[Thu May 23 13:11:44 2013] [error] [client 41.164.8.114] for key, value in iterable:
[Thu May 23 13:11:44 2013] [error] [client 41.164.8.114] ValueError: too many values to unpack
[Thu May 23 13:11:44 2013] [debug] mod_deflate.c(615): [client 41.164.8.114] Zlib: Compressed 590 to 372 : URL /tools/api/login/, referer: http://www.website.com/tools

API 托管在同一台机器上的不同域中,查看日志文件,它工作正常。

API 调用在以下函数中进行:

@app.route('/api/', methods=['GET', 'POST', 'PUT', 'DELETE'])
@app.route('/api/<path:endpoint>', methods=['GET', 'POST', 'PUT', 'DELETE'])
def api(endpoint=None):
# extract POST/PUT variables
dat = request.form
if len(dat) == 0:
# extract GET variables
dat = request.args
# submit request to API
out = call_api(request.method, endpoint, dat, request.files)
return out

调用:

def call_api(method, endpoint, data=None, files=None):
url = 'https://api.example.com' + endpoint
if method.upper() == "GET":
r = requests.get(url, data=data, verify=False)
# ... similarly for other verbs
return r.text, r.status_code, r.headers

最佳答案

我最好的猜测是你返回一个特殊的头字典(来自 python-requests)而不是一个普通的。 Flask 采用两种形式的 header :

{'key': 'value'}
# and
[('key', 'value')]

由于您的特殊字典未被识别为真正的字典,因此它将被视为失败的元组列表。

改变

return r.text, r.status_code, r.headers

return r.text, r.status_code, r.headers.items()

关于python - Flask 崩溃并出现 ValueError : too many values to unpack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16715000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com