Hyper crawler https2.0 protocol website, using py2exe can not package certs.pem solution

        Recently, when using hyper to crawl the data of https2.0 protocol website, after finally using py2exe to package it into exe, it was found that the certificate certs.pem was not packaged in.

        I have been trying to use py2exe to package certs.pem into exe in the early stage. Many methods on the Internet are to directly copy certs.pem into the exe, but the actual work is not feasible at all; there are also ways to add data_flies to the setup, but this is Some resource files are added, but they cannot be packaged into the exe, and finally solved through the official API, with the address: https://hyper.readthedocs.io/en/latest/api.html#sslcontext

        

        As you can see, the official API uses the init_context in the tls class to configure the certificate, that is, certs.pem. cert is the client's certificate path. When cert_path=None, cert=None, the hyper certificate is used by default. I cannot pack the hyper certificate in the package here, so the hyper default certificate cannot be used. You can go to the hyper and download the default certificate to the local , And then use this method to specify the local certificate, code:

from hyper import HTTPConnection, tls

conn = HTTPConnection('xxxx:443', secure=True,ssl_context=tls.init_context(cert_path=r'certs.pem'))
conn.request('GET', '/xxxx/xxxx', None, None)
resp = conn.get_response()
str = resp.read(decode_content=False).decode(encoding='utf8', errors='ignore')
resp.close()

        HTTP/2 uses multiplexing, a connection is divided into multiple streams (stream), each stream has its own request-response pair. In hyper, when sending the request conn.request, when the request returns the stream id, it means that the request is currently sent using http2.0; if the conn.request has no return value of the stream id, it means that the request is sent using http1.0. When sending an https request, that is, when the connection requires authentication, secure=True, and ssl_context is the certificate setting; when sending an http request, secure=False. ssl_context=tls.init_context(cert_path=None, cert=None, cert_password=None), ssl_context parameters include cert_path, cert, cert_password, which can be left blank by default.

  

 

Guess you like

Origin blog.csdn.net/qq_41061437/article/details/115306901