ALPN:服务器未就协议达成一致。使用默认值。 - 使用 pycurl 进行 https 通信

问题描述 投票:0回答:1

我需要通过 API 与服务器(我无法在其上执行操作)进行通信,通过 API,我可以读取某些请求变量的状态。 API 基于通过 HTTPS 协议的 POST 调用。 问题是我需要有一定的通信速度,但用于 POST 调用的会话随后立即关闭,迫使我重新创建另一个会话(这一过程大约需要 3 秒)。

我创建了一个函数来创建会话:

import pycurl
def create_session():
    session = pycurl.Curl()
    session.setopt(session.COOKIEFILE, "")  # Enable cookie handling
    session.setopt(session.COOKIEJAR, "cookies.txt")  # Save cookies to a file
    session.setopt(session.SSL_VERIFYPEER, 0)  # Disable SSL verification
    session.setopt(session.SSL_VERIFYHOST, 0)  # Disable SSL verification
    session.setopt(session.VERBOSE, True)  # Enable verbose output for debugging
    session.setopt(session.SSL_SESSIONID_CACHE, 1)  # Enable session ID caching
    session.setopt(session.HTTP_VERSION, pycurl.CURL_HTTP_VERSION_1_1)
    #session.setopt(session.CAINFO, certifi.where())
    #session.setopt(session.OPT_CERTINFO, True)
    #session.setopt(session.SSL_ENABLE_ALPN, False)
    return session

还有一个用于 POST:

def post(url, file, header, session):
    buffer = BytesIO()
    #postfields = urllib.parse.urlencode(file)
    session.setopt(session.URL, url)
    session.setopt(session.POSTFIELDS, file)
    session.setopt(session.HTTPHEADER, [f"{k}: {v}" for k, v in header.items()])
    session.setopt(session.WRITEDATA, buffer)

    # Add keep-alive header
    header['Connection'] = 'keep-alive'

    try:
        session.perform()
        response_code = session.getinfo(pycurl.RESPONSE_CODE)
        response_body = buffer.getvalue().decode('utf-8')
    except pycurl.error as e:
        response_code = None
        response_body = f"Request failed: {e}"

    return response_body

我得到的日志始终如下:

*   Trying 172.21.51.191:443...
* Connected to 172.21.51.191 (172.21.51.191) port 443
* schannel: disabled automatic use of client certificate
* schannel: using IP address, SNI is not supported by OS.
* ALPN: curl offers http/1.1
* ALPN: server did not agree on a protocol. Uses default.
* using HTTP/1.x
> POST /rest/login.cgi HTTP/1.1
Host: 172.21.51.191
User-Agent: PycURL/"7.45.3" libcurl/8.6.0-DEV Schannel zlib/1.3.1
Accept: */*
Content-Type: application/json
Connection: keep-alive
Keep-Alive: timeout=500.0, max=1000
Content-Length: 43

< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< Server: embedded HTTPD
< Expires: 1 JAN 2013 00:00:00 GMT
< Last-Modified: 3 SEP 2024 16:18:28 GMT
< Cache-Control: no-cache
< Access-Control-Allow-Origin: http://localhost:3000
< Last-Modified: 1 JAN 1995 00:00:00 GMT
< Transfer-Encoding: chunked
< 
* Leftovers after chunking: 7 bytes
* Connection #0 to host 172.21.51.191 left intact
*   Trying 172.21.51.191:443...
* Connected to 172.21.51.191 (172.21.51.191) port 443
* schannel: disabled automatic use of client certificate
* schannel: using IP address, SNI is not supported by OS.
* ALPN: curl offers http/1.1

我注意到使用了 HTTP/1.x 协议,该协议要求每次调用后关闭会话。

我尝试过不同的Python库,但总是遇到同样的问题。 我能做什么?

python api post https pycurl
1个回答
0
投票

非常有趣的问题。我也有同样的问题,正在关注

© www.soinside.com 2019 - 2024. All rights reserved.