Flask crashes with ValueError: too many values to unpack

Baruch picture Baruch · May 23, 2013 · Viewed 8k times · Source

I have a flask app which communicates with another web service. I have this error which only seems to occur when both applications are running on the same server, but I don't know what the source is. The Flask application is hosted at /tools via a WSGIScriptAlias in Apache.

[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

The API is hosted at a different domain on the same machine, looking at the log file for that, it is working correctly.

The API call is made in the following functions:

@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

which calls:

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

Answer

Markus Unterwaditzer picture Markus Unterwaditzer · May 28, 2013

My best guess is that you return a special headers dictionary (from python-requests) instead of a normal one. Flask takes the headers in two forms:

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

Since your special dict is not recognized as a real dict, it will be treated like a list of tuples, which fails.

Change

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

to

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