尝试在接受100-Continue标头后发送Python HTTPConnection内容 [英] Trying to send Python HTTPConnection content after accepting 100-continue header

查看:55
本文介绍了尝试在接受100-Continue标头后发送Python HTTPConnection内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试调试我继承的Python脚本。它正试图通过HTTPLib将CSV发布到网站上。据我所知,问题在于HTTPLib不能按照python http client stuck on 100 continue处理接收100-Continue响应。与该帖子类似,this";仅通过Curl工作,但是出于各种原因,我们需要从Python脚本运行它。

我已尝试采用该帖子答案中详细说明的解决方法,但我找不到在接受100-Continue响应后使用该方法提交CSV的方法。

一般流程需要如下所示:

  • ->;建立连接
  • ->;发送数据,包括";Expect:100-Continue&Quot;Header,但不包括JSON正文
  • <;-接收&q;100-继续(&Q)
  • ->;使用相同的连接,发送请求的JSON正文
  • <;-在带有其他信息的JSON响应中接收200 OK消息

以下是代码的当前状态,删除了我的10多个其他尝试解决方法的注释残留物:

#!/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()
但这只会返回一个400;Bad Request&Quot;响应。问题(我认为)存在于";摄取";变量的格式和类型上。如果我没有通过json.dump()和encode()运行它,那么HTTPConnection.send()方法就会拒绝它:

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内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆