0
我正在編寫驗證我們的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!"}
但是當我使用的請求模塊做同樣的事情,它失敗:
>>> 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服務器設置的權限,從而將日誌記錄方式變爲現實,並且可能會對此有所瞭解。但客戶有什麼方法可以弄清楚爲什麼這樣做會失敗嗎?