2014-09-03 109 views
0

我正在嘗試使用請求模塊向需要ssl身份驗證的端點發出發布請求。我的pem文件在指定的路徑中,包含客戶端證書和私鑰。但是,我不斷收到證書驗證失敗異常。我在nginx日誌中看到,請求甚至從未將它發送到那裏。任何人有任何想法爲什麼?我知道這些證書應該起作用。Python請求模塊。 SSL證書驗證錯誤

params = {  
      "param_2" : "32100", 
      "param_1" : "abc" 
      } 

headers = { 
      "Content-Type" : "application/json" 
      } 
body = json.dumps(params) 

r = requests.post(
        https://somesite.com/somepath, 
        data=body, 
        headers=headers, 
        timeout=10, 
        verify="/path/to/cert.pem" 
       ) 

Traceback (most recent call last): 
File "./somefile.py", line 264, in <module> 
    start() 
File "./somefile.py", line 149, in start 
    verify="/path/to/cert.pem" 
File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 88, in post 
    return request('post', url, data=data, **kwargs) 
File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 44, in request 
    return session.request(method=method, url=url, **kwargs) 
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 448, in request 
    resp = self.send(prep, **send_kwargs) 
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 554, in send 
    r = adapter.send(request, **kwargs) 
File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 417, in send 
    raise SSLError(e, request=request) 
requests.exceptions.SSLError: [Errno 1] _ssl.c:504: error:14090086:SSL  routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed 

回答

2

從請求文檔:

請求也可以忽略,如果你設置 驗證爲False驗證SSL證書。

requests.get( 'https://kennethreitz.com',驗證=假)

默認情況下,驗證被設置爲True。選項驗證僅適用於主機 證書。

您還可以指定一個本地證書作爲客戶端證書使用, 爲單個文件(包含私鑰和證書)或 既是文件的路徑的元組:

requests.get( 'https://kennethreitz.com',證書=( '/路徑/ server.crt這', '/路徑/鍵'))

所以看來你剛剛得到了錯誤的參數。嘗試使用'cert'而不是驗證。

+0

是的,它與「verify = False」一起工作。謝謝! – user695624 2014-09-04 05:24:25

+0

verify = False可能不是你想要的..如果你沒有驗證證書,那麼使用SSL是沒有意義的。 – synthesizerpatel 2014-09-04 12:51:38