我在我的脚本中使用 urllib2.py 将文件发布到网络服务器,并且一直超时。我的代码:
def postdata(nodemac,filename,timestamp):
try:
wakeup()
socket.setdefaulttimeout(TIMEOUT)
opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler)
host = HOST
func = "post_data"
url = "http://{0}{1}?f={2}&nodemac={3}&time={4}".format(host, URI, func, nodemac, timestamp)
if os.path.isfile(filename):
data = {"data":open(filename,"rb")}
response = opener.open(url, data, timeout=TIMEOUT)
retval = response.read()
if "SUCCESS" in retval:
return 0
else:
print "RETVAL "+retval
return 99
else:
print filename +" is not a file"
return 99
except Exception as e:
print e
return 99
我将 TIMEOUT 设置为 20、60 和 120,但同样的事情一直在发生。我想知道这是怎么回事?有什么不对劲!超时设置为 20 过去可以正常工作,然后突然之间,今天它开始对我超时......有人有任何线索吗?我在网上找不到任何能让我更进一步的东西,所以我想我会在这里尝试......!
谢谢,
追溯:
File "gateway.py", line 686, in CloudRun
read_img()
File "gateway.py", line 668, in read_img
retval = database.postimg(mac,fh,timestmp)
File "/root/database.py", line 100, in postimg
response = opener.open(url, data, timeout=TIMEOUT)
File "/usr/lib/python2.7/urllib2.py", line 394, in open
File "/usr/lib/python2.7/urllib2.py", line 412, in _open
File "/usr/lib/python2.7/urllib2.py", line 372, in _call_chain
File "/usr/lib/python2.7/urllib2.py", line 1199, in http_open
File "/usr/lib/python2.7/urllib2.py", line 1174, in do_open
URLError: <urlopen error timed out>