When I add the timeout argument to the function, my code always goes into the exception and prints out "I failed". When I remove the timeout argument, the code works as it should, and it enters the try clause. Any info on how the timeout argument works in urllib.request function?
import urllib.request
url = 'stackoverflow.com'
try:
req = urllib.request.Request("http://" + url, timeout = 1000000000)
req = urllib.request.urlopen(req)
print("I tried.")
except:
print("I failed.")
I tried using requests instead of urllib, and the code works with the timeout argument included. In the code bellow, it always enters the try clause, with or without timeout argument passed.
import requests
url = 'stackoverflow.com'
try:
req = requests.get("http://" + url, timeout=1000000)
print("I tried.")
except:
print("I failed.")
However, I do not want to use requests, due to some conflicts in my code later. Any ideas on how to include timeout with urllib?
in the urllib.request.Request no initial parameters such as timeout
but it in the urllib.request.urlopen, so you need:
import urllib.request
url = 'stackoverflow.com'
try:
req = urllib.request.Request("http://" + url)
req = urllib.request.urlopen(req, timeout=1000000000)
# ^^^^^^^
print("I tried.")
except:
print("I failed.")
If you look at the documentation of the Request function (https://docs.python.org/3/library/urllib.request.html#urllib.request.Request) you will see that there is no timeout argument. So your function simply throws since you pass an unrecognized keyworded argument. One way to see what happens is to actually catch and print the exception like this:
import urllib.request
url = 'stackoverflow.com'
try:
req = urllib.request.Request("http://" + url,timeout=1000)
req = urllib.request.urlopen(req)
print("I tried.")
except Exception as exc:
print(exc)
which prints:
init() got an unexpected keyword argument 'timeout'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With