Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

urllib.request.Request timeout argument error

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?

like image 664
Emilija Trpcevska Avatar asked Jun 05 '18 14:06

Emilija Trpcevska


2 Answers

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.")
like image 181
Brown Bear Avatar answered Oct 01 '22 01:10

Brown Bear


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'

like image 41
Manos Agelidis Avatar answered Oct 01 '22 02:10

Manos Agelidis