I need to communicate with a server (on which I cannot act) via APIs thanks to which I can read the status of some requested variables.The APIs are based on POST calls via the HTTPS protocol.The problem is that I need to have a certain communication speed but the session I use for a POST call closes immediately afterwards, forcing me to recreate another one (a procedure that takes about 3 seconds).
I created a function to create a session:
import pycurldef 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
and one for 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
The log I get is always the following:
* 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.1Host: 172.21.51.191User-Agent: PycURL/"7.45.3" libcurl/8.6.0-DEV Schannel zlib/1.3.1Accept: */*Content-Type: application/jsonConnection: keep-aliveKeep-Alive: timeout=500.0, max=1000Content-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
Where I note that the HTTP/1.x protocol is used which requires the session to be closed after each call.
I have tried different python libraries but I always have the same problem.What can I do?