Python在使用本地证书时请求抛出SSLError

时间:2015-06-09 12:16:28

标签: python ssl python-requests python-sockets

我正在将脚本编写到服务器并验证它的证书。如果我使用套接字编写脚本,它可以正常工作:

m = re.search('-agent-(.*?)-\d', checkstr)
if m:
    citystr = m.group(1)
    city, _, state = citystr.rpartition('-')
    if len(state) <> 2:
        city = citystr
        state = ''
    city = city.replace('-', ' ').title()
    state = state.upper()

如果我尝试使用请求做类似的事情:

import socket, ssl, pprint

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssl_sock = ssl.wrap_socket(s,
                           ca_certs="mule.crt",
                           cert_reqs=ssl.CERT_REQUIRED)

ssl_sock.connect(('localhost', 8081))

message = "GET /api HTTP/1.1\r\nHost:localhost\r\n\r\n"
ssl_sock.write(bytes(message, 'UTF-8'))

data = ssl_sock.read().decode("utf-8")
print(data)

ssl_sock.close()

这引发:

import requests

r = requests.get('https://localhost:8081/api', cert=r'C:\path\to\cert\mule.crt')

print(r.status_code)
print(r.text)

任何想法我可能做错了什么?

0 个答案:

没有答案