Python “如何处理异常”;错误:[Errno 10054]现有连接被远程主机强制关闭;

Python “如何处理异常”;错误:[Errno 10054]现有连接被远程主机强制关闭;,python,google-app-engine,Python,Google App Engine,我是Python新手。我有一个在谷歌应用程序引擎上运行的脚本。它使用urllib3与api通信。它在我的电脑上运行得很好,但它失败了,给了我一个机会 “错误:[Errno 10054]现有连接已由强制关闭。” “远程主机” 我做了一些研究,最好的答案似乎是异常处理。我该怎么做 for row in rows: address = row['emailaddress'] status = row['status'] location = row['location']

我是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'})

用“try-and-catch”包装代码并使用(如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')

这可能会有帮助:为什么要使用urllib3连接池在appengine上不起作用,除非您使用套接字/计费。如果你想要某种程度的并发性(你的例子确实如此),那么你应该考虑使用异步请求。