Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python requests module hangs on socket connection but cURL works

I'm trying to make GET/POST requests between docker containers.

In one of them (the others work fine), when doing the following:

ADDR = 'http://172.20.0.2:5002/see'
r = requests.post(ADDR, data=data, headers=headers)

the command hangs forever (or until timeout, if one is defined) on trying to connect. It never even establishes the connection:

^CTraceback (most recent call last):
  File "/workspace/master_server.py", line 35, in <module>
    r = requests.post(ADDR, data=data, headers=headers)
  File "/usr/local/lib/python3.5/dist-packages/requests/api.py", line 112, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/requests/sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.5/dist-packages/requests/sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/requests/adapters.py", line 440, in send
    timeout=timeout
  File "/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py", line 357, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib/python3.5/http/client.py", line 1106, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python3.5/http/client.py", line 1151, in _send_request
    self.endheaders(body)
  File "/usr/lib/python3.5/http/client.py", line 1102, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python3.5/http/client.py", line 934, in _send_output
    self.send(msg)
  File "/usr/lib/python3.5/http/client.py", line 877, in send
    self.connect()
  File "/usr/local/lib/python3.5/dist-packages/urllib3/connection.py", line 166, in connect
    conn = self._new_conn()
  File "/usr/local/lib/python3.5/dist-packages/urllib3/connection.py", line 141, in _new_conn
    (self.host, self.port), self.timeout, **extra_kw)
  File "/usr/local/lib/python3.5/dist-packages/urllib3/util/connection.py", line 73, in create_connection
    sock.connect(sa)

If I run the exact same request using curl from bash, it works effortlesly:

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '"[1215 2589 1392 3019]"' 'http://172.20.0.2:5002/see'

Any other containers that I try to use in the exact same way work with either the requests module or curl. Any help on how to fix it or at least debug the problem effectively?

like image 730
Gabriel Lefundes Avatar asked Oct 19 '18 03:10

Gabriel Lefundes


1 Answers

I had the same issue on macOS (Catalina) recently and it turned out to be due to IPv6.

I had to go to System Preferences -> Network -> Wi-Fi -> Advanced -> TCP/IP and change Configure IPv6 to Link-local only (there wasn't an "Off" option in the list, though this would also be applicable). Then click OK and Apply.

You can also disable altogether using:

sudo networksetup -setv6off Wi-Fi
like image 170
gawbul Avatar answered Sep 29 '22 04:09

gawbul