【问题标题】:Chunked encoding using python requests - Error 10054 connection forcibly closed by the remote host使用 python 请求进行分块编码 - 错误 10054 连接被远程主机强制关闭
【发布时间】:2017-10-31 17:27:17
【问题描述】:

在使用 python 请求运行我的脚本时,我收到一个块编码错误,我想知道为什么我会收到这个错误。

当我执行resp.encoding 时,我会返回None

我目前没有在我的脚本中使用任何 SSL 认证验证,因此我也收到了不安全请求警告。

    for attachment in attachments:
     if attachment['type'] == 'Form':
        continue
      # create insertcursor row
      row = cursorPointData.newRow()
      pprint(attachments)

      for key in attachment:
        pprint(key)
        pprint(attachment[key])

        row.setValue(key, attachment[key])

    # Download file

      guid = attachment['guid']
      resp = requests.get(
        'https:...' + guid,
        headers={'Authorization': 'Bearer ' + token},
        stream=True,
        verify=False
      )
     contentType = resp.headers['Content-Type']
     contentLength = int(resp.headers['content-length'])
     pprint('contentLength = ' + str(contentLength))
     extension = contentType.split('/')[1]
     filename = '{0}.{1}'.format(guid, extension)
     output_path = os.path.join(attachmentDir, filename)
     attachmentPath = os.path.join("filepath", filename)

    with open(output_path, 'wb') as f:
        for chunk in resp.iter_content(chunk_size=None):
            if chunk:
                f.write(chunk)
                f.flush() #flush the data to file
                os.fsync(f.fileno()) #force the file write and free memory

    row.setValue('attachmentPath', attachmentPath)

    cursorPointData.insertRow(row)
del cursorPointData, row

【问题讨论】:

    标签: python redirect python-requests http-status-code-301 chunked-encoding


    【解决方案1】:

    我检查了 Chrome 开发者工具的网络标签,我得到了状态 200。但是在 Firefox 开发者工具中,我得到了 301 状态:“永久移动”。

    原来我在请求中输入了错误的 url,需要将 url 更改为它重定向到的更新版本。

    发现您可以在 python 请求库中使用response.history 来查找任何重定向问题。

    现在,我返回 utf-8 作为响应编码而不是无。

    把这个留在这里,以防其他人遇到同样的问题。

    【讨论】:

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