Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask crashes with ValueError: too many values to unpack

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
like image 805
Baruch Avatar asked May 23 '13 13:05

Baruch


People also ask

How do you fix ValueError too many values to unpack?

The Python "ValueError: too many values to unpack (expected 2) in Python" occurs when the number of variables in the assignment is not the same as the number of values in the iterable. To solve the error, declare exactly as many variables as there are items in the iterable.

How do I stop too many values to unpack in python?

To solve this problem, use the items() method to iterate over a dictionary. Another common cause is when you try to unpack too many values into variables without assigning enough variables. You solve this issue by ensuring the number of variables to which a list unpacked is equal to the number of items in the list.

What does too many values to unpack mean?

The valueerror: too many values to unpack occurs during a multiple-assignment where you either don't have enough objects to assign to the variables or you have more objects to assign than variables.


1 Answers

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()
like image 107
Markus Unterwaditzer Avatar answered Sep 28 '22 13:09

Markus Unterwaditzer