setting the timeout on a urllib2.request() call

南楼画角 提交于 2019-11-27 08:38:57

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()
Giorgoc

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 :)

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)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!