setting the timeout on a urllib2.request() call

前端 未结 3 1041
失恋的感觉
失恋的感觉 2020-12-01 14:25

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

I do not use urllib2.urlopen() since i am using the data parameter of re

相关标签:
3条回答
  • 2020-12-01 14:54

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

    0 讨论(0)
  • 2020-12-01 14:58

    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()
    
    0 讨论(0)
  • 2020-12-01 15:07

    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)
    
    0 讨论(0)
提交回复
热议问题