在 urllib2.request() 调用上设置超时 [英] setting the timeout on a urllib2.request() call

查看:47
本文介绍了在 urllib2.request() 调用上设置超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我不使用 urllib2.urlopen() 因为我使用了 requestdata 参数.我该如何设置?

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

推荐答案

虽然 urlopen 确实接受 data param for POST,你可以调用urlopen 在像这样的 Request 对象上,

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

这篇关于在 urllib2.request() 调用上设置超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆