Make requests to download file in url with nameko

Hey my friend, I need your help. I try to download a file in url like (http://google.com/favicon.ico). It works well locally with (“requests” , “urllib3” , “urllib.request” ), but it doesn’t work with nameko. I can’t solve this problem. My code likes this:

from nameko.web.handlers import http
import requests

class MyService:
    name = "my_service"

    @http("POST", "/post/my")
    def do_post(self, request):
        url = "http://google.com/favicon.ico"
        r = requests.get(url, allow_redirects=True)
        print(r.status)
        open("google.ico", "wb").write(r.content)
        return u"received: {}".format(request.get_data(as_text=True))

and the error is always like this.
“wrap_socket() got an unexpected keyword argument ‘_context’”

Can anyone help me pls.

In fact, I have already tried “requests” , “urllib3” , “urllib.request” . these three libs of python 3.7.4. But They all have the same error.

And it works well in python3.7.4 without nameko.

Could you paste entire stack trace of the error here? How are you running the service?

Thank you very much.

Traceback (most recent call last):
  File "/Users/tyu/project/etl_v3/venv/lib/python3.7/site-packages/requests/sessions.py", line 682, in send
    r = adapter.send(request, **kwargs)
  File "/Users/tyu/project/etl_v3/venv/lib/python3.7/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/Users/tyu/project/etl_v3/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 603, in urlopen
    chunked=chunked)
  File "/Users/tyu/project/etl_v3/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 344, in _make_request
    self._validate_conn(conn)
  File "/Users/tyu/project/etl_v3/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 843, in _validate_conn
    conn.connect()
  File "/Users/tyu/project/etl_v3/venv/lib/python3.7/site-packages/urllib3/connection.py", line 370, in connect
    ssl_context=context)
  File "/Users/tyu/project/etl_v3/venv/lib/python3.7/site-packages/urllib3/util/ssl_.py", line 355, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "/Users/tyu/project/etl_v3/venv/lib/python3.7/site-packages/eventlet/green/ssl.py", line 438, in wrap_socket
    return GreenSSLSocket(sock, *a, _context=self, **kw)
  File "/Users/tyu/project/etl_v3/venv/lib/python3.7/site-packages/eventlet/green/ssl.py", line 76, in __new__
    *args, **kw
TypeError: wrap_socket() got an unexpected keyword argument '_context'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/tyu/project/etl_v3/venv/lib/python3.7/site-packages/requests/sessions.py", line 704, in send
    history = [resp for resp in gen] if allow_redirects else []
  File "/Users/tyu/project/etl_v3/venv/lib/python3.7/site-packages/requests/sessions.py", line 704, in <listcomp>
    history = [resp for resp in gen] if allow_redirects else []
  File "/Users/tyu/project/etl_v3/venv/lib/python3.7/site-packages/requests/sessions.py", line 258, in resolve_redirects
    **adapter_kwargs
  File "/Users/tyu/project/etl_v3/venv/lib/python3.7/site-packages/requests/sessions.py", line 727, in send
    raise Exception(msg)
Exception: Error on session(). Cause : wrap_socket() got an unexpected keyword argument '_context' in /Users/tyu/project/etl_v3/venv/lib/python3.7/site-packages/requests/sessions.py L:682

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/tyu/project/etl_v3/venv/lib/python3.7/site-packages/requests/sessions.py", line 564, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/tyu/project/etl_v3/venv/lib/python3.7/site-packages/requests/sessions.py", line 727, in send
    raise Exception(msg)
Exception: Error on session(). Cause : Error on session(). Cause : wrap_socket() got an unexpected keyword argument '_context' in /Users/tyu/project/etl_v3/venv/lib/python3.7/site-packages/requests/sessions.py L:682 in /Users/tyu/project/etl_v3/venv/lib/python3.7/site-packages/requests/sessions.py L:704

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/tyu/project/etl_v3/venv/lib/python3.7/site-packages/nameko/containers.py", line 392, in _run_worker
    result = method(*worker_ctx.args, **worker_ctx.kwargs)
  File "./test-nameko.py", line 29, in do_post
    r = requests.get(url, allow_redirects=True)
  File "/Users/tyu/project/etl_v3/venv/lib/python3.7/site-packages/requests/api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "/Users/tyu/project/etl_v3/venv/lib/python3.7/site-packages/requests/api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "/Users/tyu/project/etl_v3/venv/lib/python3.7/site-packages/requests/sessions.py", line 569, in request
    raise Exception(msg)
Exception: Error on request(). Cause : Error on session(). Cause : Error on session(). Cause : wrap_socket() got an unexpected keyword argument '_context' in /Users/tyu/project/etl_v3/venv/lib/python3.7/site-packages/requests/sessions.py L:682 in /Users/tyu/project/etl_v3/venv/lib/python3.7/site-packages/requests/sessions.py L:704 in /Users/tyu/project/etl_v3/venv/lib/python3.7/site-packages/requests/sessions.py L:564
127.0.0.1 - - [03/Sep/2019 14:50:21] "POST /post/my?type=url&url=https%3A%2F%2Fwww.python.org%2Fstatic%2Fimg%2Fpython-logo.png HTTP/1.1" 500 566 0.080115

The python file is like above named “test-nameko.py”.

I run it in shell with command : nameko run test-nameko

And I do the simple http request with insomnia(with postman has the same error)

Hi @yuut007 unfortunately looks like eventlet library which is a nameko’s dependency is the root cause of the issue here as documented here: https://github.com/eventlet/eventlet/issues/526

We have a test reproducing this issue and tracking it with: https://github.com/nameko/nameko/pull/644

Is it possible for you to use Python 3.6 in meantime?

1 Like

Thanks a million. :grin: It works with python3.6. I am willing to tracking this problem with 3.7 in the future.