In my script, requests.get
never returns:
import requests
print ("requesting..")
# This call never returns!
r = requests.get(
"http://www.justdial.com",
proxies = {'http': '222.255.169.74:8080'},
)
print(r.ok)
What could be the possible reason(s)? Any remedy? What is the default timeout that get
uses?
The default timeout is
None
, which means it'll wait (hang) until the connection is closed.What happens when you pass in a timeout value?
Reviewed all the answers and came to conclusion that the problem still exists. On some sites requests may hang infinitely and using multiprocessing seems to be overkill. Here's my approach(Python 3.5+):
From requests documentation:
It happens a lot to me that requests.get() takes a very long time to return even if the
timeout
is 1 second. There are a few way to overcome this problem:1. Use the
TimeoutSauce
internal classFrom: https://github.com/kennethreitz/requests/issues/1928#issuecomment-35811896
2. Use a fork of requests from kevinburke: https://github.com/kevinburke/requests/tree/connect-timeout
From its documentation: https://github.com/kevinburke/requests/blob/connect-timeout/docs/user/advanced.rst
NOTE: The change has since been merged to the main Requests project.
3. Using
evenlet
orsignal
as already mentioned in the similar question: Timeout for python requests.get entire response