I need to set the timeout on urllib2.request()
.
我需要在urllib2.request()上设置超时。
I do not use urllib2.urlopen()
since i am using the data
parameter of request
. How can I set this?
我不使用urllib2.urlopen()因为我使用请求的数据参数。我怎么设置这个?
3 个解决方案
#1
38
Although urlopen
does accept data
param for POST
, you can call urlopen
on a Request
object like this,
虽然urlopen确实接受了POST的数据参数,但你可以在这样的Request对象上调用urlopen,
import urllib2
request = urllib2.Request('http://www.example.com', data)
response = urllib2.urlopen(request, timeout=4)
content = response.read()
#2
2
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)
#3
2
still, you can avoid using urlopen and proceed like this:
仍然,你可以避免使用urlopen并继续这样:
request = urllib2.Request('http://example.com')
response = opener.open(request,timeout=4)
response_result = response.read()
this works too :)
这也有效:)
#1
38
Although urlopen
does accept data
param for POST
, you can call urlopen
on a Request
object like this,
虽然urlopen确实接受了POST的数据参数,但你可以在这样的Request对象上调用urlopen,
import urllib2
request = urllib2.Request('http://www.example.com', data)
response = urllib2.urlopen(request, timeout=4)
content = response.read()
#2
2
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)
#3
2
still, you can avoid using urlopen and proceed like this:
仍然,你可以避免使用urlopen并继续这样:
request = urllib2.Request('http://example.com')
response = opener.open(request,timeout=4)
response_result = response.read()
this works too :)
这也有效:)