Trying to send Python HTTPConnection content after accepting 100-continue header(尝试在接受100-Continue标头后发送Python HTTPConnection内容)
问题描述
我一直在尝试调试我继承的Python脚本。它正试图通过HTTPLib将CSV发布到网站上。据我所知,问题在于HTTPLib不能按照python http client stuck on 100 continue处理接收100-Continue响应。与该帖子类似,this";仅通过Curl工作,但是出于各种原因,我们需要从Python脚本运行它。
我已尝试采用该帖子答案中详细说明的解决方法,但我找不到在接受100-Continue响应后使用该方法提交CSV的方法。
 一般流程需要如下所示: 以下是代码的当前状态,删除了我的10多个其他尝试解决方法的注释残留物:
但这只会返回一个400;Bad Request&Quot;响应。问题(我认为)存在于";摄取";变量的格式和类型上。如果我没有通过json.dump()和encode()运行它,那么HTTPConnection.send()方法就会拒绝它:#!/usr/bin/env python
import os
import ssl
import http.client
import binascii
import logging
import json
#classes taken from https://stackoverflow.com/questions/38084993/python-http-client-stuck-on-100-continue
class ContinueHTTPResponse(http.client.HTTPResponse):
    def _read_status(self, *args, **kwargs):
        version, status, reason = super()._read_status(*args, **kwargs)
        if status == 100:
            status = 199
        return version, status, reason
    def begin(self, *args, **kwargs):
        super().begin(*args, **kwargs)
        if self.status == 199:
            self.status = 100
    def _check_close(self, *args, **kwargs):
        return super()._check_close(*args, **kwargs) and self.status != 100
class ContinueHTTPSConnection(http.client.HTTPSConnection):
    response_class = ContinueHTTPResponse
    def getresponse(self, *args, **kwargs):
        logging.debug('running getresponse')
        response = super().getresponse(*args, **kwargs)
        if response.status == 100:
            setattr(self, '_HTTPConnection__state', http.client._CS_REQ_SENT)
            setattr(self, '_HTTPConnection__response', None)
        return response
def uploadTradeIngest(ingestFile, certFile, certPass, host, port, url):
  boundary = binascii.hexlify(os.urandom(16)).decode("ascii")
  headers = {
    "accept": "application/json",
    "Content-Type": "multipart/form-data; boundary=%s" % boundary,
    "Expect": "100-continue",
  }
  context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
  context.load_cert_chain(certfile=certFile, password=certPass)
  connection = ContinueHTTPSConnection(
    host, port=port, context=context)
  with open(ingestFile, "r") as fh:
    ingest = fh.read()
  ## Create form-data boundary
  ingest = "--%s
Content-Disposition: form-data; " % boundary + 
    "name="file"; filename="%s"" % os.path.basename(ingestFile) + 
    "
%s
--%s--
" % (ingest, boundary)
  print("pre-request")
  connection.request(
    method="POST", url=url, headers=headers)
  print("post-request")
  #resp = connection.getresponse()
  resp = connection.getresponse()
  if resp.status == http.client.CONTINUE:
    resp.read()
    print("pre-send ingest")
    ingest = json.dumps(ingest)
    ingest = ingest.encode()
    print(ingest)
    connection.send(ingest)
    print("post-send ingest")
    resp = connection.getresponse()
    print("response1")
    print(resp)
    print("response2")
    print(resp.read())
    print("response3")
    return resp.read()
ERROR: Got error: memoryview: a bytes-like object is required, not 'str'
我考虑过改用请求库,但我无法让它使用我的本地证书包来接受站点的证书。我有一个带有加密密钥的完整链,我确实对其进行了解密,但仍然遇到来自请求的持续的SSL_VERIFY错误。如果您有解决我当前请求问题的建议,我也很乐意沿着这条路走下去。
如何使用HTTPLib或请求(或任何其他库)来实现我需要的目标?
推荐答案
如果将来有人遇到此问题,我最终使用了一些杂乱无章的方法来解决它。我尝试过HTTPLib、请求和URLLib3,它们都不能处理100-Continue标头,所以.我刚刚通过subprocess.run()函数在Curl周围编写了一个Python包装器,如下所示:
def sendReq(upFile):
    sendFile=f"file=@{upFile}"
    completed = subprocess.run([
    curlPath,
    '--cert',
    args.cert,
    '--key',
    args.key,
    targetHost,
    '-H',
    'accept: application/json',
    '-H',
    'Content-Type: multipart/form-data',
    '-H',
    'Expect: 100-continue',
    '-F',
    sendFile,
     '-s'
    ], stdout=subprocess.PIPE, universal_newlines=True)
    return completed.stdout
我遇到的唯一问题是,如果Curl是针对NSS库构建的,那么它就会失败,我通过在包中包含一个静态构建的Curl二进制文件(其路径包含在代码中的curlPath变量中)解决了这个问题。我从this Github repo获取此二进制文件。
                        这篇关于尝试在接受100-Continue标头后发送Python HTTPConnection内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:尝试在接受100-Continue标头后发送Python HTTPConnection内容
				
        
 
            
        基础教程推荐
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
 - 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
 - PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
 - 求两个直方图的卷积 2022-01-01
 - 修改列表中的数据帧不起作用 2022-01-01
 - 在Python中从Azure BLOB存储中读取文件 2022-01-01
 - PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
 - Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
 - 包装空间模型 2022-01-01
 - 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				