httplib2 ssl.c:499错误

来源:互联网 发布:修改数据导入考勤机 编辑:程序博客网 时间:2024/06/05 18:02
httplib2 ssl.c:499错误


#现象:使用httplib2访问https报_ssl.c:499错误
import httplib2
h1 = httplib2.Http(disable_ssl_certificate_validation=True)
resp,content = h1.request("https://172.16.20.107:15000/")
print content


Traceback (most recent call last):
  File "E:\Python-Eclipse\FoscamCloudAPI\test.py", line 10, in <module>
    resp,content = h1.request("https://172.16.20.107:15000/")
  File "C:\Python27\lib\site-packages\httplib2\__init__.py", line 1595, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "C:\Python27\lib\site-packages\httplib2\__init__.py", line 1337, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "C:\Python27\lib\site-packages\httplib2\__init__.py", line 1259, in _conn_request
    conn.connect()
  File "C:\Python27\lib\site-packages\httplib2\__init__.py", line 1023, in connect
    self.disable_ssl_certificate_validation, self.ca_certs)
  File "C:\Python27\lib\site-packages\httplib2\__init__.py", line 80, in _ssl_wrap_socket
    cert_reqs=cert_reqs, ca_certs=ca_certs)
  File "C:\Python27\lib\ssl.py", line 342, in wrap_socket
    ciphers=ciphers)
  File "C:\Python27\lib\ssl.py", line 121, in __init__
    self.do_handshake()
  File "C:\Python27\lib\ssl.py", line 281, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [Errno 8] _ssl.c:499: EOF occurred in violation of protocol


#原因:httplib库默认只使用SSLv23
def wrap_socket(sock, keyfile=None, certfile=None,
                server_side=False, cert_reqs=CERT_NONE,
                ssl_version=PROTOCOL_SSLv23, ca_certs=None,
                do_handshake_on_connect=True,
                suppress_ragged_eofs=True, ciphers=None):


    return SSLSocket(sock, keyfile=keyfile, certfile=certfile,
                     server_side=server_side, cert_reqs=cert_reqs,
                     ssl_version=ssl_version, ca_certs=ca_certs,
                     do_handshake_on_connect=do_handshake_on_connect,
                     suppress_ragged_eofs=suppress_ragged_eofs,
                     ciphers=ciphers)
                     
#解决办法:修改httplib2库中的代码
try:
    import ssl # python 2.6
    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
        # 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)
        return ssl.wrap_socket(sock, keyfile=key_file, certfile=cert_file,
                               cert_reqs=cert_reqs, ca_certs=ca_certs,ssl_version=ssl.PROTOCOL_SSLv3)
0 0
原创粉丝点击