[Python] requests.Session with client certificates and own CA

Discussão em 'Python' iniciado por Stack, Fevereiro 7, 2025.

  1. Stack

    Stack Membro Participativo

    Here is my code

    os.environ['REQUESTS_CA_BUNDLE'] = os.path.join('/path/to/','ca-own.crt')
    s = requests.Session()
    s.cert = ('some.crt', 'some.key')

    s.get('https://some.site.com')


    Last instruction returns:

    requests.exceptions.SSLError: HTTPSConnectionPool(host='some.site.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1131)')))

    With curl:

    curl --cacert ca-own.crt --key some.key --cert some.crt https://some.site.com

    returns normal html code.

    How can i make python requests.Session send correct certificates to the endpoint?

    P.S. The same situation will be if i add the following

    s.verify = 'some.crt'


    or

    cat some.crt ca-own.crt > res.crt

    s.verify = 'res.crt'


    P.P.S.

    cat some.crt some.key > res.pem

    s.cert = "res.pem"


    requests.exceptions.SSLError: HTTPSConnectionPool(host='some.site.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1131)')))

    cat ca-own.crt some.crt some.key > res.pem

    s.cert = "res.pem"


    requests.exceptions.SSLError: HTTPSConnectionPool(host='some.site.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(116, '[X509: KEY_VALUES_MISMATCH] key values mismatch (_ssl.c:4067)')))

    Continue reading...

Compartilhe esta Página