繁体   English   中英

如何使用请求模块诊断客户端证书错误?

[英]How do I diagnose client certificate errors with the requests module?

我正在编写测试以验证我们的JSONRPC服务器,并且我想使用请求模块来测试诸如设置无效的Content-Length和Content-Type标头等内容。但是,我们的服务器需要有效的客户端证书,因此我无法获取请求模块以正确利用我的客户证书中记录的证书

如果我只是打开一个套接字并手动发送数据,它就可以正常工作:

>>> import socket, ssl
>>> s = """\
... POST /jsonrpc HTTP/1.1
... Host: example.com
... Content-Length: 77
... 
... {"params": [{"example": "parameter"}], "id": 1, "method": "example.function"}\
... """.replace("\n", "\r\n")
>>> sock = socket.create_connection(("example.com", 443))
>>> sock = ssl.wrap_socket(sock, keyfile = "key.pem", certfile = "cert.pem")
>>> sock.sendall(s)
144
>>> print(sock.recv(4096) + sock.recv(4096))
HTTP/1.1 200 OK
Date: Mon, 23 Jul 2012 19:50:17 GMT
Server: CherryPy/3.2.0
Content-Length: 53
Content-Type: application/json
Set-Cookie: session_id=4ee3f4c435aee126c8042d4fba99962a48ca0a37; expires=Mon, 23 Jul 2012 20:20:17 GMT; Path=/; secure
Connection: close

{"jsonrpc": "2.0", "id": 1, "result": "Hello World!"}

但是,当我使用请求模块执行相同的操作时,它失败了:

>>> import requests
>>> data = '{"params": [{"example": "parameter"}], "id": 1, "method": "example.function"}'
>>> r = requests.post("https://example.com/jsonrpc", data = data, headers = {"Content-Length": "77"}, timeout = 2, verify = False, cert = ("cert.pem", "key.pem"))
>>> print r.content
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /jsonrpc
on this server.</p>
<hr>
<address>Apache/2.2.15 (Red Hat) Server at example.com Port 443</address>
</body></html>

因此,我不仅不知道为什么失败,甚至不知道如何找出问题所在。 我可以尝试获得许可来更改我们的Apache服务器设置,以增加日志记录的方式,这可能会给我们带来一些启示。 但是,客户端上是否有任何方法可以弄清失败的原因?

当我升级到最新版本的请求模块时,此问题消失了。 我仍然想知道一般如何诊断这类证书错误,因此,如果有人知道,请发表答案!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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