如何处理异常 "错误: [Errno 10054] 现有连接被远程主机强制关闭
我是一名Python新手。我有一个脚本在谷歌应用引擎上运行。这个脚本用urllib3来和一个API进行交流。在我电脑上运行得很好,但在云端运行时却失败了,出现了一个错误:
"错误: [Errno 10054] 现有连接被远程主机强制关闭"
我查了一些资料,发现最好的解决办法似乎是处理异常。那么我该怎么做呢?
for row in rows:
address = row['emailaddress']
status = row['status']
location = row['location']
if status != 'Active':
status = 'Inactive'
payload = '{xxx}'
r = http.urlopen('POST', url, body=payload, headers={'Content-Type': 'application/json'})
1 个回答
0
把你的代码放在一个叫做'尝试和捕获'的结构里,同时使用异步请求(正如Tim提到的),下面是一个例子:
rpc = urlfetch.create_rpc()
urlfetch.make_fetch_call(rpc, 'http://www.google.com/')
# ... do other things ...
try:
result = rpc.get_result()
if result.status_code == 200:
text = result.content
self.response.write(text)
else:
self.response.status_int = result.status_code
self.response.write('URL returned status code {}'.format(
result.status_code))
except urlfetch.DownloadError:
self.response.status_int = 500
self.response.write('Error fetching URL')