Python requests module hangs on socket connection but cURL works
I recently ran into the same issue locally, and like some comments on the original question, my code worked in windows, but not linux. After a few hours found the culprit for my case: IPv6.
The python module I was using attempts to make a socket connection with a server and was by default using IPv6. After turning IPv6 off in my linux environment (Ubuntu 18.04) with the following steps:
- Open terminal
- Enter:
sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1
- Enter:
sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1
The module worked as expected**.
**note that these steps will likely vary per environment
I faced the same problem and lost a lot of time ... In my case, the problem was as follows (guniconf.py):
workers = 1
change to:
workers = 10 # any number is greater than 1
More: There is a Flask application that serves some REST API.The application has a REST API method (let's call it A-method), which makes inside itself a call to another REST API method (let's call it B-method) using requests.post()
. When the B-method is called from A-method, app hangs.As I understand it, when calling the A-method, the gunicorn worker processes it, but with the B-method, there are no more workers.
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