我正在编写验证我们的JSONRPC服务器的测试,我想使用the requests module来测试设置无效的Content-Length和Content-Type标头等等。但是,我们的服务器需要有效的客户端证书,我无法让请求模块正确使用我的客户端证书as documented in their turorial。
如果我只是打开一个套接字并手动发送数据,它就可以正常工作:
>>> 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!"}
但是当我使用requests模块做同样的事情时,它就失败了:
>>> 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服务器设置的权限,以便改变日志记录,这可能会对它有所了解。但客户端有什么方法可以找出失败的原因吗?
答案 0 :(得分:1)
当我升级到最新版本的请求模块时,此问题就消失了。我一般还想知道如何诊断这类证书错误,所以如果有人知道的话请发表答案!