在urllib2.request()调用上设置超时

era*_*ran 18 python urllib2

我需要设置超时urllib2.request().

我不使用,urllib2.urlopen()因为我使用的data参数request.我怎么设置这个?

Jar*_*red 43

虽然urlopen接受dataparam POST,你可以调用urlopen这样的Request对象,

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

  • @MöbiusStripMallSews([手册](https://docs.python.org/2/library/urllib2.html)) (6认同)