使用python的分块编码请求错误10054连接被远程hos强制关闭

2024-09-30 18:14:18 发布

您现在位置:Python中文网/ 问答频道 /正文

在使用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

Tags: pathkeyinforattachmentfilenamerespfile
1条回答
网友
1楼 · 发布于 2024-09-30 18:14:18

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

结果我给请求输入了错误的url,需要将url更改为重定向到的更新版本。在

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

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

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

相关问题 更多 >