2013-07-29 101 views
4

我發出HTTPS GET請求到REST服務,我自己用httplib2的,但是我們得到的錯誤:SSL版本 - EOF發生違反協議

[Errno 8] _ssl.c:504: EOF occurred in violation of protocol 

所有其他客戶端運行良好(瀏覽器,Java客戶端等),但PHP curl需要設置爲使用SSL v3的小例外。

我搜索了一下,它似乎確實是關於SSL版本的錯誤,但我似乎無法找到在httplib2中更改它的方法。 有它周圍的任何方式改變,除了下面一行的源代碼:

# We should be specifying SSL version 3 or TLS v1, but the ssl module 
# doesn't expose the necessary knobs. So we need to go with the default 
# of SSLv23. 
return ssl.wrap_socket(sock, keyfile=key_file, certfile=cert_file, 
         cert_reqs=cert_reqs, ca_certs=ca_certs) 
+0

你還研究了下面的鏈接嗎? : [對代理SSL錯誤] [1] [Python客戶端側SSL錯誤] [2] [1]:http://stackoverflow.com/questions/16883595/https-代理請求errno-8-ssl-c504-eof-occurrence-in-violation- [2]:http://stackoverflow.com/questions/8247416/python-sslerror-client-side-erroreof發生違反協議服務 –

+0

@PhahaladDeshpande是的,那不是我的情況。服務器位於Scala(Jetty),我沒有客戶端和服務器之間的代理服務器。 –

回答

5

我開發了這個解決方法httplib2的:

import httplib2 

# Start of the workaround for SSL3 
# This is a monkey patch/module function overriding 
# to allow pages that only work with SSL3 

# Build the appropriate socket wrapper for ssl 
try: 
    import ssl # python 2.6 
    httplib2.ssl_SSLError = ssl.SSLError 
    def _ssl_wrap_socket(sock, key_file, cert_file, 
         disable_validation, ca_certs): 
     if disable_validation: 
      cert_reqs = ssl.CERT_NONE 
     else: 
      cert_reqs = ssl.CERT_REQUIRED 
     # Our fix for sites the only accepts SSL3 
     try: 
      # Trying SSLv3 first 
      tempsock = ssl.wrap_socket(sock, keyfile=key_file, certfile=cert_file, 
             cert_reqs=cert_reqs, ca_certs=ca_certs, 
             ssl_version=ssl.PROTOCOL_SSLv3) 
     except ssl.SSLError, e: 
      tempsock = ssl.wrap_socket(sock, keyfile=key_file, certfile=cert_file, 
             cert_reqs=cert_reqs, ca_certs=ca_certs, 
             ssl_version=ssl.PROTOCOL_SSLv23) 
     return tempsock 
    httplib2._ssl_wrap_socket = _ssl_wrap_socket 
except (AttributeError, ImportError): 
    httplib2.ssl_SSLError = None 
    def _ssl_wrap_socket(sock, key_file, cert_file, 
         disable_validation, ca_certs): 
     if not disable_validation: 
      raise httplib2.CertificateValidationUnsupported(
        "SSL certificate validation is not supported without " 
        "the ssl module installed. To avoid this error, install " 
        "the ssl module, or explicity disable validation.") 
     ssl_sock = socket.ssl(sock, key_file, cert_file) 
     return httplib.FakeSocket(sock, ssl_sock) 
    httplib2._ssl_wrap_socket = _ssl_wrap_socket 

# End of the workaround for SSL3 

if __name__ == "__main__": 
    h1 = httplib2.Http() 
    resp, content = h1.request("YOUR_SSL3_ONLY_LINK_HERE", "GET") 
    print(content) 

此解決方案是基於解決方法,在這個錯誤報告http://bugs.python.org/issue11220提出的urllib2,

更新:爲httplib2提供解決方案。我沒有注意到你使用的是httplib2,我認爲它是urllib2。

0

請參考其他StackOverflow threads指定的解決方案。如提供的鏈接中用戶favoretti的響應中所述,指定TLS版本的方式將SSL版本強制爲TLSv1。

希望,這個工程

相關問題