【发布时间】:2017-01-12 02:44:30
【问题描述】:
当我使用 urllib2 时,有时会出现类似“urllib2 urlopen error [Errno 61] Connection denied”的错误,我会尝试再次连接。但是,一旦连接出错,在我重新启动 python 脚本之前它永远不会成功。 以下代码是连接部分。
req = urllib2.Request(self.__servAddr, body)
response = None
try:
response = urllib2.urlopen(req, timeout = 15)
res = response.read()
res = eval(res)
print res
if 200 == res["retcode"]:
if res["data"]:
infoReady = 1
xxxxxx
except Exception, e:
print "request error %s" % str(e)
if response:
response.close()
似乎当前连接对下一个连接有影响。我该如何解决?
【问题讨论】:
-
你在连接什么?远程主机可能在多次尝试后拒绝您的连接...
-
远程主机只是一个普通的服务器。所以你说可能是服务器的问题? @l'l'l
-
一般来说,如果一个连接被“拒绝”,那么无论你试图连接的是什么按钮。如果您在服务器上敲打、刮擦等,他们通常会这样做。通常,您需要使用正确的标头或其 API(如果有可用的 API)来解决问题。尝试标题方法通常是第一个开始的地方:stackoverflow.com/a/36853786/499581
标签: python connection urllib2