【问题标题】:How to handle exception "error: [Errno 10054] An existing connection was forcibly closed by the remote host"如何处理异常“错误:[Errno 10054] 现有连接被远程主机强行关闭”
【发布时间】:2014-07-13 21:12:03
【问题描述】:

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

【问题讨论】:

标签: python google-app-engine


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    • 2012-02-07
    • 1970-01-01
    • 2013-09-20
    • 2011-06-06
    • 1970-01-01
    相关资源
    最近更新 更多