eran
eran

Reputation: 15156

setting the timeout on a urllib2.request() call

I need to set the timeout on urllib2.request().

I do not use urllib2.urlopen() since i am using the data parameter of request. How can I set this?

Upvotes: 23

Views: 37894

Answers (3)

OrangeG
OrangeG

Reputation: 169

still, you can avoid using urlopen and proceed like this:

request = urllib2.Request('http://example.com')
response = opener.open(request,timeout=4)
response_result = response.read()

this works too :)

Upvotes: 4

Jared
Jared

Reputation: 26427

Although urlopen does accept data param for POST, you can call urlopen on a Request object like this,

import urllib2
request = urllib2.Request('http://www.example.com', data)
response = urllib2.urlopen(request, timeout=4)
content = response.read()

Upvotes: 52

Alex
Alex

Reputation: 1126

Why not use the awesome requests? You'll save yourself a lot of time.

If you are worried about deployment just copy it in your project.

Eg. of requests:

>>> requests.post('http://github.com', data={your data here}, timeout=10)

Upvotes: 3

Related Questions