Wizzard
Wizzard

Reputation: 12712

Python - urllib2 timeout

I got something below is snippet of my code

opener = urllib2.build_opener(redirect_handler.MyHTTPRedirectHandler())
opener.addheaders = [('Accept-encoding', 'gzip')]
fetch_timeout = 12
self.response = opener.open(url, timeout=fetch_timeout)

however, it code still waits 60~ seconds before timing out... Any clues?

Upvotes: 3

Views: 12296

Answers (3)

pyfunc
pyfunc

Reputation: 66739

Which version are you using. It was added in 2.6

Also the method is

urllib2.urlopen(url[, data][, timeout])

Can you try providing

self.response = opener.open(url, None, fetch_timeout)

Yeah for all others, you could still use socket module to set socket time out.

Upvotes: 2

bgporter
bgporter

Reputation: 36564

Look at the OpenerDirector class and the urllib2.install_opener() method.

Upvotes: 0

synthesizerpatel
synthesizerpatel

Reputation: 28056

At a guess you probably need to set the socket timeout

import socket

default_timeout = 12

socket.setdefaulttimeout(default_timeout)

Upvotes: 3

Related Questions