如何处理异常"错误:[Errno 10054]远程主机"强制关闭现有连接。

时间:2014-05-24 16:35:38

标签: python google-app-engine

我是一个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 :(得分:0)

用“尝试捕捉”代码来包裹你的代码。并使用async requests(如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')