简体   繁体   中英

Unable to load ssl cert in python: TypeError: certfile should be a valid filesystem path

I am trying to send a POST request using client certificates (which are self-signed) in Python3. The application runs on Docker (although it should make no difference).

When I try to load the cert and key files in the context, I get the error below. I have verified that the certificates exist at the path /app in the filesystem. What is going wrong here?

TypeError: certfile should be a valid filesystem path

Here is my code:

import ssl

context = ssl._create_unverified_context()
context.load_cert_chain("/app/attendance.pem", "/app/key.pem")

conn = http.client.HTTPSConnection("someHost", 8200, context) # The error is thrown on this line
conn = http.client.HTTPSConnection("someHost", 8200, context) # The error is thrown on this line

From the documentation of HTTPSConnection :

 class http.client.HTTPSConnection(host, port=None, key_file=None, cert_file=None, [timeout, ] source_address=None, *, context=None, ...

Based on this the third parameter is key_file . This means your context will be interpreted as the key. This causes the error message you see since the context is no valid path. Instead of using a (wrong) positional parameter for context you should use a (correct) named parameter:

 conn = http.client.HTTPSConnection("someHost", 8200, context = context)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM