使用python httplib和pem文件连接时出现SSL错误

0 投票
1 回答
1460 浏览
提问于 2025-04-18 17:46

我正在尝试用Python客户端连接一个HTTPS的网络服务,使用的是PEM文件和httplib。

这是我的代码:

# HTTPS connection with python
#!/usr/bin/env python

import httplib , urllib 

CERTFILE = 'path_to_pem_file'
hostname = 'IP_address:Port_number'

headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}

Json_data = {
    "amountTransaction": {
        some json data .. 
    }
}

params = urllib.urlencode(Json_data)

conn = httplib.HTTPSConnection(
    hostname,
    key_file = CERTFILE,
    cert_file = CERTFILE 
)


conn.request("POST", '/url_to_call', params, headers)



print conn.getreponse().read()

print response.status, response.reason
conn.close()

但是我收到了以下错误:

SSLError: [Errno 336265225] _ssl.c:354: error:140B0009:SSL        
routines:SSL_CTX_use_PrivateKey_file:PEM lib

你能帮我看看哪里出问题了吗?

1 个回答

1

cert_filekey_file 是用来让客户端验证服务器身份的,它们需要包含证书和对应的私钥。我理解这个错误信息的意思是,可能是PEM文件里没有私钥,或者私钥和证书不匹配,或者私钥是加密的,导致无法读取。

撰写回答