2015-05-26 27 views
5

Tôi cố gắng tạo kết nối HTTPS cục bộ thành api XMLRPC. Kể từ khi tôi nâng cấp lên python 2.7.9 mà enable by default certificates verification, tôi nhận được một lỗi CERTIFICATE_VERIFY_FAILED khi tôi sử dụng API của tôitắt xác minh chứng chỉ mặc định trong python 2.7.9

>>> test=xmlrpclib.ServerProxy('https://admin:[email protected]:9999/API',verbose=False, use_datetime=True) 
>>> test.list_satellites() 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/local/lib/python2.7/xmlrpclib.py", line 1233, in __call__ 
    return self.__send(self.__name, args) 
    File "/usr/local/lib/python2.7/xmlrpclib.py", line 1591, in __request 
    verbose=self.__verbose 
    File "/usr/local/lib/python2.7/xmlrpclib.py", line 1273, in request 
    return self.single_request(host, handler, request_body, verbose) 
    File "/usr/local/lib/python2.7/xmlrpclib.py", line 1301, in single_request 
    self.send_content(h, request_body) 
    File "/usr/local/lib/python2.7/xmlrpclib.py", line 1448, in send_content 
    connection.endheaders(request_body) 
    File "/usr/local/lib/python2.7/httplib.py", line 997, in endheaders 
    self._send_output(message_body) 
    File "/usr/local/lib/python2.7/httplib.py", line 850, in _send_output 
    self.send(msg) 
    File "/usr/local/lib/python2.7/httplib.py", line 812, in send 
    self.connect() 
    File "/usr/local/lib/python2.7/httplib.py", line 1212, in connect 
    server_hostname=server_hostname) 
    File "/usr/local/lib/python2.7/ssl.py", line 350, in wrap_socket 
    _context=self) 
    File "/usr/local/lib/python2.7/ssl.py", line 566, in __init__ 
    self.do_handshake() 
    File "/usr/local/lib/python2.7/ssl.py", line 788, in do_handshake 
    self._sslobj.do_handshake() 
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581) 
>>> import ssl 
>>> ssl._create_default_https_context = ssl._create_unverified_context 
>>> test.list_satellites() 
[{'paired': True, 'serial': '...', 'enabled': True, 'id': 1, 'date_paired': datetime.datetime(2015, 5, 26, 16, 17, 6)}] 

Liệu tồn tại một cách pythonic để vô hiệu hóa xác minh chứng chỉ mặc định trong python 2.7.9?

tôi không thực sự biết nếu nó là tốt để thay đổi thuộc tính SSL toàn cầu "private" (ssl._create_default_https_context = ssl._create_unverified_context)

Trả lời

16

Bạn phải cung cấp một bối cảnh SSL chưa được xác minh, được xây dựng bằng tay hoặc sử dụng chức năng _create_unverified_context tin() từ ssl mô-đun:

import xmlrpclib 
import ssl 

test = xmlrpclib.ServerProxy('https://admin:[email protected]:9999/API', 
          verbose=False, use_datetime=True, 
          context=ssl._create_unverified_context()) 
test.list_satellites() 

Lưu ý: mã này chỉ làm việc với python> = 2.7.9 (context tham số đã được bổ sung bằng Python 2.7.9)

Nếu bạn muốn có một mã số tương thích với Pyth trước trên phiên bản, bạn phải sử dụng transport tham số:

import xmlrpclib 
import ssl 

context = hasattr(ssl, '_create_unverified_context') and ssl._create_unverified_context() \ 
      or None 
test = xmlrpclib.ServerProxy('https://admin:[email protected]:9999/API', 
          verbose=False, use_datetime=True, 
          transport=xmlrpclib.SafeTransport(use_datetime=True, 
                   context=context)) 
test.list_satellites() 
+2

Ít nhất cho Pytho n 2.7.6 giải pháp thứ hai không hoạt động, hoặc 'ssl' không có hàm' _create_unverified_context() 'và từ những gì tôi có thể thấy trong mã nguồn, không phải Python 2.7.8. – Adaephon

+0

Vâng, bạn thực sự là @Adaephon, nhưng tôi không có khả năng thử nghiệm với phiên bản Python cũ hơn khi tôi viết những dòng này. Tôi nghĩ rằng cách chính xác để xử lý tính tương thích, và không sử dụng chức năng riêng tư, là làm cho bối cảnh _unverified_ của chúng ta bằng tay. Giải pháp đơn giản là kiểm tra sự tồn tại của chức năng này. –

-1

Với Python 2.6.6 ví dụ:

s = xmlrpclib.ServerProxy('https://admin:[email protected]:9999/API', transport=None, encoding=None, verbose=0,allow_none=0, use_datetime=0) 

Nó làm việc cho tôi ...

1

Có thể vô hiệu hóa xác nhận bằng các API ssl công khai hiện có trên Python 2.7.9+:

import xmlrpclib 
import ssl 

ssl_ctx = ssl.create_default_context() 
ssl_ctx.check_hostname = False 
ssl_ctx.verify_mode = ssl.CERT_NONE 
test = xmlrpclib.ServerProxy('https://admin:[email protected]:9999/API', 
          verbose=False, use_datetime=True, 
          context=ssl_ctx) 
test.list_satellites() 
Các vấn đề liên quan