Hướng dẫn python ssl get certificate chain - python ssl get chuỗi chứng chỉ

Nhờ câu trả lời đóng góp của Aleksi, tôi đã tìm thấy một yêu cầu lỗi/tính năng đã yêu cầu chính điều này: //bugs.python.org/issue18233. Mặc dù các thay đổi chưa được hoàn thành, nhưng chúng có một bản vá làm cho điều này có sẵn:

Đây là mã kiểm tra mà tôi đã đánh cắp từ một nguồn bị lãng quên và được lắp lại:

import socket

from ssl import wrap_socket, CERT_NONE, PROTOCOL_SSLv23
from ssl import SSLContext  # Modern SSL?
from ssl import HAS_SNI  # Has SNI?

from pprint import pprint

def ssl_wrap_socket[sock, keyfile=None, certfile=None, cert_reqs=None,
                    ca_certs=None, server_hostname=None,
                    ssl_version=None]:
    context = SSLContext[ssl_version]
    context.verify_mode = cert_reqs

    if ca_certs:
        try:
            context.load_verify_locations[ca_certs]
        # Py32 raises IOError
        # Py33 raises FileNotFoundError
        except Exception as e:  # Reraise as SSLError
            raise SSLError[e]

    if certfile:
        # FIXME: This block needs a test.
        context.load_cert_chain[certfile, keyfile]

    if HAS_SNI:  # Platform-specific: OpenSSL with enabled SNI
        return [context, context.wrap_socket[sock, server_hostname=server_hostname]]

    return [context, context.wrap_socket[sock]]

hostname = 'www.google.com'
print["Hostname: %s" % [hostname]]

s = socket.socket[socket.AF_INET, socket.SOCK_STREAM]
s.connect[[hostname, 443]]

[context, ssl_socket] = ssl_wrap_socket[s,
                                       ssl_version=2, 
                                       cert_reqs=2, 
                                       ca_certs='/usr/local/lib/python3.3/dist-packages/requests/cacert.pem', 
                                       server_hostname=hostname]

pprint[ssl_socket.getpeercertchain[]]

s.close[]

Output:

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]

Mã nguồn: lib/ssl.py Lib/ssl.py

Mô-đun này cung cấp quyền truy cập vào bảo mật lớp vận chuyển [thường được gọi là các cơ sở mã hóa và xác thực ngang hàng an toàn cho các ổ cắm và xác thực ngang hàng cho ổ cắm mạng, cả phía máy khách và phía máy chủ. Mô -đun này sử dụng thư viện OpenSSL. Nó có sẵn trên tất cả các hệ thống Unix, Windows, MacOS hiện đại và có thể là các nền tảng bổ sung, miễn là OpenSSL được cài đặt trên nền tảng đó.

Ghi chú

Một số hành vi có thể phụ thuộc vào nền tảng, vì các cuộc gọi được thực hiện cho API ổ cắm hệ điều hành. Phiên bản cài đặt của OpenSSL cũng có thể gây ra các biến thể trong hành vi. Ví dụ: TLSV1.3 với OpenSSL phiên bản 1.1.1.

Cảnh báo

Don Tiết sử dụng mô -đun này mà không cần đọc các cân nhắc bảo mật. Làm như vậy có thể dẫn đến một cảm giác bảo mật sai, vì các cài đặt mặc định của mô -đun SSL không nhất thiết phù hợp với ứng dụng của bạn.Security considerations. Doing so may lead to a false sense of security, as the default settings of the ssl module are not necessarily appropriate for your application.

Tính khả dụng: Không phải emscripten, không phải wasi.: not Emscripten, not WASI.

Mô -đun này không hoạt động hoặc không có sẵn trên các nền tảng Webassugging

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
5 và
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
6. Xem các nền tảng Webassugging để biết thêm thông tin.WebAssembly platforms for more information.

Phần này ghi lại các đối tượng và chức năng trong mô -đun

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
7; Để biết thêm thông tin chung về TLS, SSL và chứng chỉ, người đọc được giới thiệu đến các tài liệu trong phần See See See cũng ở phía dưới.

Mô-đun này cung cấp một lớp,

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
8, có nguồn gốc từ loại
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
9 và cung cấp một trình bao bọc giống như ổ cắm cũng mã hóa và giải mã dữ liệu đi qua ổ cắm với SSL. Nó hỗ trợ các phương thức bổ sung như
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
0, lấy lại chứng chỉ của phía bên kia của kết nối và
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
1, lấy lại mật mã được sử dụng cho kết nối an toàn.

Đối với các ứng dụng tinh vi hơn, lớp

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
2 giúp quản lý các cài đặt và chứng chỉ, sau đó có thể được kế thừa bởi các ổ cắm SSL được tạo thông qua phương thức
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
3.

Đã thay đổi trong phiên bản 3.5.3: Được cập nhật để hỗ trợ liên kết với OpenSSL 1.1.0Updated to support linking with OpenSSL 1.1.0

Đã thay đổi trong phiên bản 3.6: OpenSSL 0.9.8, 1.0.0 và 1.0.1 không còn được hỗ trợ và không còn được hỗ trợ. Trong tương lai, mô -đun SSL sẽ yêu cầu ít nhất OpenSSL 1.0.2 hoặc 1.1.0.OpenSSL 0.9.8, 1.0.0 and 1.0.1 are deprecated and no longer supported. In the future the ssl module will require at least OpenSSL 1.0.2 or 1.1.0.

Thay đổi trong phiên bản 3.10: PEP 644 đã được triển khai. Mô -đun SSL yêu cầu OpenSSL 1.1.1 hoặc mới hơn.PEP 644 has been implemented. The ssl module requires OpenSSL 1.1.1 or newer.

Việc sử dụng các hằng số và chức năng không dùng nữa dẫn đến cảnh báo phản đối.

Các chức năng, hằng số và ngoại lệ

Tạo ổ cắm tạo

Vì Python 3.2 và 2.7.9, nên sử dụng

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
3 của một ví dụ
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
5 để bọc các ổ cắm dưới dạng các đối tượng
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
6. Chức năng của người trợ giúp
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
7 trả về một bối cảnh mới với các cài đặt mặc định an toàn. Hàm
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
8 cũ không được chấp nhận vì nó không hiệu quả và không có hỗ trợ cho chỉ định tên máy chủ [SNI] và khớp tên máy chủ.

Ví dụ về ổ cắm máy khách với bối cảnh mặc định và ngăn xếp kép IPv4/IPv6:

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]

Ví dụ về ổ cắm máy khách với bối cảnh tùy chỉnh và IPv4:

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]

Ví dụ về ổ cắm máy chủ nghe trên localhost ipv4:

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...

Tạo ra bối cảnh

Một hàm tiện lợi giúp tạo các đối tượng

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
5 cho các mục đích chung.

ssl.create_default_context [mục đích = purda.server_auth, cafile = none, capath = none, cadata = none] ¶create_default_context[purpose=Purpose.SERVER_AUTH, cafile=None, capath=None, cadata=None]

Trả về một đối tượng

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
5 mới với các cài đặt mặc định cho mục đích đã cho. Các cài đặt được chọn bởi mô -đun
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
7 và thường biểu thị mức bảo mật cao hơn so với khi gọi trực tiếp vào hàm tạo
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
5.

CAFILE, CAPATH, CADATA đại diện cho các chứng chỉ CA tùy chọn để tin tưởng để xác minh chứng chỉ, như trong

ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
3. Nếu cả ba là
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
4, chức năng này có thể chọn tin tưởng vào chứng chỉ CA mặc định của hệ thống.

Các cài đặt là:

ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
5 hoặc
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
6,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
7 và
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
8 với các bộ mật mã mã hóa cao mà không có RC4 và không có các bộ mật mã không được xác thực. Vượt qua
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
9 dưới dạng mục đích đặt
>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
0 đến
>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
1 và tải chứng chỉ CA [khi ít nhất một trong CAFILE, CAPATH hoặc CADATA được cung cấp] hoặc sử dụng
>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
2 để tải chứng chỉ CA mặc định.

Khi

>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
3 được hỗ trợ và biến môi trường
>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
4 được đặt,
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
7 cho phép ghi nhật ký khóa.
>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
4 is set,
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
7 enables key logging.

Ghi chú

Một số hành vi có thể phụ thuộc vào nền tảng, vì các cuộc gọi được thực hiện cho API ổ cắm hệ điều hành. Phiên bản cài đặt của OpenSSL cũng có thể gây ra các biến thể trong hành vi. Ví dụ: TLSV1.3 với OpenSSL phiên bản 1.1.1.

Nếu ứng dụng của bạn cần cài đặt cụ thể, bạn nên tạo một

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
5 và tự mình áp dụng cài đặt.

Ghi chú

Nếu bạn thấy rằng khi một số máy khách hoặc máy chủ cũ hơn, cố gắng kết nối với

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
5 được tạo bởi chức năng này, họ sẽ gặp lỗi cho thấy giao thức hoặc bộ mật mã không phù hợp, thì có thể họ chỉ hỗ trợ SSL3.0 mà chức năng này loại trừ bằng cách sử dụng
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
8. SSL3.0 được coi là bị phá vỡ hoàn toàn. Nếu bạn vẫn muốn tiếp tục sử dụng chức năng này nhưng vẫn cho phép các kết nối SSL 3.0, bạn có thể kích hoạt lại chúng bằng cách sử dụng:

ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3

Mới trong phiên bản 3.4.

Đã thay đổi trong phiên bản 3.4.4: RC4 đã bị loại khỏi chuỗi mật mã mặc định.RC4 was dropped from the default cipher string.

Đã thay đổi trong phiên bản 3.6: Chacha20/poly1305 đã được thêm vào chuỗi mật mã mặc định.ChaCha20/Poly1305 was added to the default cipher string.

3DES đã bị loại khỏi chuỗi mật mã mặc định.

Đã thay đổi trong phiên bản 3.8: Hỗ trợ đăng nhập khóa thành

>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
4 đã được thêm vào.Support for key logging to
>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
4 was added.

Ngoại lệ ha

ngoại lệSl.sslerror¶ssl.SSLError

Nêu ra để báo hiệu một lỗi từ việc triển khai SSL cơ bản [hiện được cung cấp bởi thư viện OpenSSL]. Điều này biểu thị một số vấn đề trong lớp mã hóa và xác thực cấp cao hơn mà đã chồng lên nhau trên kết nối mạng cơ bản. Lỗi này là một kiểu con của

>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43
0. Mã lỗi và thông báo của các trường hợp
>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43
1 được cung cấp bởi thư viện OpenSSL.

thư viện¶

Một chuỗi ghi nhớ chỉ định mô hình con openSSL trong đó xảy ra lỗi, chẳng hạn như

>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43
2,
>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43
3 hoặc
>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43
4. Phạm vi của các giá trị có thể phụ thuộc vào phiên bản OpenSSL.

Mới trong phiên bản 3.3.

lý do¶

Một chuỗi ghi nhớ chỉ định lý do lỗi này xảy ra, ví dụ

>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43
5. Phạm vi của các giá trị có thể phụ thuộc vào phiên bản OpenSSL.

Mới trong phiên bản 3.3.

lý do¶ ssl.SSLZeroReturnError

Một chuỗi ghi nhớ chỉ định lý do lỗi này xảy ra, ví dụ

>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43
5. Phạm vi của các giá trị có thể phụ thuộc vào phiên bản OpenSSL.

Mới trong phiên bản 3.3.

lý do¶ssl.SSLWantReadError

Một chuỗi ghi nhớ chỉ định lý do lỗi này xảy ra, ví dụ

>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43
5. Phạm vi của các giá trị có thể phụ thuộc vào phiên bản OpenSSL.non-blocking SSL socket when trying to read or write data, but more data needs to be received on the underlying TCP transport before the request can be fulfilled.

Mới trong phiên bản 3.3.

lý do¶ssl.SSLWantWriteError

Một chuỗi ghi nhớ chỉ định lý do lỗi này xảy ra, ví dụ

>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43
5. Phạm vi của các giá trị có thể phụ thuộc vào phiên bản OpenSSL.non-blocking SSL socket when trying to read or write data, but more data needs to be sent on the underlying TCP transport before the request can be fulfilled.

Mới trong phiên bản 3.3.

lý do¶ ssl.SSLSyscallError

Một chuỗi ghi nhớ chỉ định lý do lỗi này xảy ra, ví dụ

>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43
5. Phạm vi của các giá trị có thể phụ thuộc vào phiên bản OpenSSL.

Mới trong phiên bản 3.3.

lý do¶ssl.SSLEOFError

Một chuỗi ghi nhớ chỉ định lý do lỗi này xảy ra, ví dụ

>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43
5. Phạm vi của các giá trị có thể phụ thuộc vào phiên bản OpenSSL.

Mới trong phiên bản 3.3.

lý do¶ ssl.SSLCertVerificationError

Một chuỗi ghi nhớ chỉ định lý do lỗi này xảy ra, ví dụ

>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43
5. Phạm vi của các giá trị có thể phụ thuộc vào phiên bản OpenSSL.

ngoại lệSl.sslzeroreturrorrorrorrorrorrorrorrorrorrorrorrorrorror

Một lớp con của
>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43
1 được nêu ra khi cố gắng đọc hoặc viết và kết nối SSL đã được đóng sạch sẽ. Lưu ý rằng điều này không có nghĩa là vận chuyển cơ bản [đọc TCP] đã bị đóng cửa.

ngoại lệSl.sslwantreaderrorror¶

Một lớp con của
>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43
1 được nâng lên bởi một ổ cắm SSL không chặn khi cố gắng đọc hoặc ghi dữ liệu, nhưng cần nhận được nhiều dữ liệu hơn trên vận chuyển TCP cơ bản trước khi yêu cầu có thể được thực hiện.

ngoại lệSl.sslwantwriteerrorrorrorrorrorrorrorrorrorrorrorrorror

Một lớp con của
>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43
1 được nâng lên bởi một ổ cắm SSL không chặn khi cố gắng đọc hoặc ghi dữ liệu, nhưng cần nhiều dữ liệu hơn trên vận chuyển TCP cơ bản trước khi yêu cầu có thể được thực hiện.
ssl.CertificateError

ngoại lệSl.ssssyscallerror¶

Một lớp con của
>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43
1 được nêu ra khi gặp lỗi hệ thống trong khi cố gắng thực hiện một thao tác trên ổ cắm SSL. Thật không may, không có cách nào dễ dàng để kiểm tra số ERRNO ban đầu.

ngoại lệSl.ssleoferror¶RAND_bytes[num]

Một lớp con của

>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43
1 được nâng lên khi kết nối SSL đã bị chấm dứt đột ngột. Nói chung, bạn không nên cố gắng sử dụng lại phương tiện vận chuyển cơ bản khi gặp lỗi này.

ngoại lệSL.SSLCERTVERENTERERRORROR EX

Một lớp con của

>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43
1 được nâng lên khi xác thực chứng chỉ đã thất bại.

Mới trong phiên bản 3.3.

lý do¶RAND_pseudo_bytes[num]

Một chuỗi ghi nhớ chỉ định lý do lỗi này xảy ra, ví dụ

>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43
5. Phạm vi của các giá trị có thể phụ thuộc vào phiên bản OpenSSL.

ngoại lệSl.sslzeroreturrorrorrorrorrorrorrorrorrorrorrorrorrorror

ngoại lệSL.SSLCERTVERENTERERRORROR EX

Mới trong phiên bản 3.3.

lý do¶RAND_status[]

Một chuỗi ghi nhớ chỉ định lý do lỗi này xảy ra, ví dụ

>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43
5. Phạm vi của các giá trị có thể phụ thuộc vào phiên bản OpenSSL.

ssl.rand_add [byte, entropy] ¶RAND_add[bytes, entropy]

Trộn các byte đã cho vào trình tạo số giả giả SSL. Entropy tham số [một float] là một giới hạn dưới trên entropy có trong chuỗi [vì vậy bạn luôn có thể sử dụng

>>> ssl.OPENSSL_VERSION
'OpenSSL 1.0.2k  26 Jan 2017'
4]. Xem RFC 1750 để biết thêm thông tin về các nguồn entropy.RFC 1750 for more information on sources of entropy.

Xử lý chứng chỉ

ssl.match_hostname [cert, hostname] ¶match_hostname[cert, hostname]

Xác minh rằng chứng nhận [ở định dạng được giải mã được trả về bởi

>>> ssl.OPENSSL_VERSION
'OpenSSL 1.0.2k  26 Jan 2017'
5] phù hợp với tên máy chủ đã cho. Các quy tắc được áp dụng là các quy tắc để kiểm tra danh tính của các máy chủ HTTPS như được nêu trong RFC 2818, RFC 5280 và RFC 6125. Ngoài HTTPS, chức năng này phải phù hợp để kiểm tra danh tính của máy chủ trong các giao thức dựa trên SSL khác nhau như FTP IMAP, Pops và những người khác.RFC 2818, RFC 5280 and RFC 6125. In addition to HTTPS, this function should be suitable for checking the identity of servers in various SSL-based protocols such as FTPS, IMAPS, POPS and others.

>>> ssl.OPENSSL_VERSION
'OpenSSL 1.0.2k  26 Jan 2017'
6 được nâng lên khi thất bại. Khi thành công, chức năng không trả lại gì:

>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'

Mới trong phiên bản 3.2.

Đã thay đổi trong phiên bản 3.3.3: Hàm hiện theo RFC 6125, Mục 6.4.3 và không khớp với nhiều ký tự đại diện [ví dụ:

>>> ssl.OPENSSL_VERSION
'OpenSSL 1.0.2k  26 Jan 2017'
7 hoặc
>>> ssl.OPENSSL_VERSION
'OpenSSL 1.0.2k  26 Jan 2017'
8] cũng như một ký tự đại diện bên trong đoạn tên miền quốc tế hóa [IDN]. Các nhãn A IDN như
>>> ssl.OPENSSL_VERSION
'OpenSSL 1.0.2k  26 Jan 2017'
9 vẫn được hỗ trợ, nhưng
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
00 không còn khớp với
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
01.The function now follows RFC 6125, section 6.4.3 and does neither match multiple wildcards [e.g.
>>> ssl.OPENSSL_VERSION
'OpenSSL 1.0.2k  26 Jan 2017'
7 or
>>> ssl.OPENSSL_VERSION
'OpenSSL 1.0.2k  26 Jan 2017'
8] nor a wildcard inside an internationalized domain names [IDN] fragment. IDN A-labels such as
>>> ssl.OPENSSL_VERSION
'OpenSSL 1.0.2k  26 Jan 2017'
9 are still supported, but
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
00 no longer matches
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
01.

Đã thay đổi trong phiên bản 3.5: Kết hợp các địa chỉ IP, khi có mặt trong trường pentalaltname của chứng chỉ, hiện được hỗ trợ.Matching of IP addresses, when present in the subjectAltName field of the certificate, is now supported.

Đã thay đổi trong phiên bản 3.7: Hàm không còn được sử dụng để kết nối TLS. Kết hợp tên máy chủ hiện được thực hiện bởi OpenSSL.The function is no longer used to TLS connections. Hostname matching is now performed by OpenSSL.

Cho phép ký tự đại diện khi nó là cùng bên trái và là nhân vật duy nhất trong phân khúc đó. Các ký tự đại diện một phần như

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
02 không còn được hỗ trợ.

Không dùng nữa kể từ phiên bản 3.7.

ssl.cert_time_to_seconds [cert_time] ¶cert_time_to_seconds[cert_time]

Trả lại thời gian tính bằng vài giây kể từ kỷ nguyên, được đưa ra chuỗi ____103 đại diện cho ngày không phải là trước hoặc không phải là người khác từ một chứng chỉ ở định dạng

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
04 Strptime [locale].

Đây là một ví dụ:

>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43

Ngày của Note trước khi không phải là người khác phải sử dụng GMT [RFC 5280].RFC 5280].

Đã thay đổi trong phiên bản 3.5: Giải thích thời gian đầu vào là thời gian trong UTC theo quy định của múi giờ GMT GMT trong chuỗi đầu vào. Timezone địa phương đã được sử dụng trước đây. Trả về một số nguyên [không có phân số thứ hai ở định dạng đầu vào]Interpret the input time as a time in UTC as specified by ‘GMT’ timezone in the input string. Local timezone was used previously. Return an integer [no fractions of a second in the input format]

ssl.get_server_certificate [addr, ssl_version = protocol_tls_client, Ca_certs = none [, thời gian chờ]] ¶get_server_certificate[addr, ssl_version=PROTOCOL_TLS_CLIENT, ca_certs=None[, timeout]]

Với địa chỉ

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
05 của máy chủ được bảo vệ SSL, dưới dạng cặp [tên máy chủ, số cổng], lấy chứng chỉ máy chủ và trả về nó dưới dạng chuỗi được mã hóa PEM. Nếu
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
06 được chỉ định, hãy sử dụng phiên bản đó của giao thức SSL để cố gắng kết nối với máy chủ. Nếu
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
07 được chỉ định, nó phải là một tệp chứa danh sách các chứng chỉ gốc, định dạng tương tự như được sử dụng cho cùng một tham số trong
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
3. Cuộc gọi sẽ cố gắng xác nhận chứng chỉ máy chủ so với bộ chứng chỉ gốc đó và sẽ thất bại nếu lần thử xác thực không thành công. Một thời gian chờ có thể được chỉ định với tham số
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
09.

Đã thay đổi trong phiên bản 3.3: Hàm này hiện tương thích IPv6.This function is now IPv6-compatible.

Đã thay đổi trong phiên bản 3.5: SSL_Version mặc định được thay đổi từ

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
10 thành
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
11 để tương thích tối đa với các máy chủ hiện đại.The default ssl_version is changed from
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
10 to
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
11 for maximum compatibility with modern servers.

Thay đổi trong phiên bản 3.10: Tham số thời gian chờ đã được thêm vào.The timeout parameter was added.

SSL.DER_CERT_TO_PEM_CERT [DER_CERT_BYTES] ¶DER_cert_to_PEM_cert[DER_cert_bytes]

Đưa ra một chứng chỉ dưới dạng blob được mã hóa bởi các byte, trả về phiên bản chuỗi được mã hóa PEM của cùng một chứng chỉ.

SSL.PEM_CERT_TO_DER_CERT [PEM_CERT_STRING] ¶PEM_cert_to_DER_cert[PEM_cert_string]

Đưa ra một chứng chỉ dưới dạng chuỗi PEM ASCII, trả về một chuỗi byte được mã hóa der cho cùng chứng chỉ đó.

ssl.get_default_verify_paths [] ¶get_default_verify_paths[]

Trả về một tuple có tên với các đường dẫn đến CAFILE và CAPATH mặc định của OpenSSL. Các đường dẫn giống như được sử dụng bởi

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
12. Giá trị trả về là một tuple có tên
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
13:named tuple
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
13:

  • Hostname: www.google.com
    [{'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'Google Inc'],],
                 [['commonName', 'Google Internet Authority G2'],]],
      'notAfter': 'Sep 11 11:04:38 2014 GMT',
      'notBefore': 'Sep 11 11:04:38 2013 GMT',
      'serialNumber': '50C71E48BCC50676',
      'subject': [[['countryName', 'US'],],
                  [['stateOrProvinceName', 'California'],],
                  [['localityName', 'Mountain View'],],
                  [['organizationName', 'Google Inc'],],
                  [['commonName', 'www.google.com'],]],
      'subjectAltName': [['DNS', 'www.google.com'],],
      'version': 3},
     {'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'GeoTrust Inc.'],],
                 [['commonName', 'GeoTrust Global CA'],]],
      'notAfter': 'Apr  4 15:15:55 2015 GMT',
      'notBefore': 'Apr  5 15:15:55 2013 GMT',
      'serialNumber': '023A69',
      'subject': [[['countryName', 'US'],],
                  [['organizationName', 'Google Inc'],],
                  [['commonName', 'Google Internet Authority G2'],]],
      'version': 3},
     {'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'Equifax'],],
                 [['organizationalUnitName',
                   'Equifax Secure Certificate Authority'],]],
      'notAfter': 'Aug 21 04:00:00 2018 GMT',
      'notBefore': 'May 21 04:00:00 2002 GMT',
      'serialNumber': '12BBE6',
      'subject': [[['countryName', 'US'],],
                  [['organizationName', 'GeoTrust Inc.'],],
                  [['commonName', 'GeoTrust Global CA'],]],
      'version': 3},
     {'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'Equifax'],],
                 [['organizationalUnitName',
                   'Equifax Secure Certificate Authority'],]],
      'notAfter': 'Aug 22 16:41:51 2018 GMT',
      'notBefore': 'Aug 22 16:41:51 1998 GMT',
      'serialNumber': '35DEF4CF',
      'subject': [[['countryName', 'US'],],
                  [['organizationName', 'Equifax'],],
                  [['organizationalUnitName',
                    'Equifax Secure Certificate Authority'],]],
      'version': 3}]
    
    14 - Đường dẫn đã được giải quyết đến CAFILE hoặc
    ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
    ctx.options &= ~ssl.OP_NO_SSLv3
    
    4 nếu tệp không tồn tại,

  • Hostname: www.google.com
    [{'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'Google Inc'],],
                 [['commonName', 'Google Internet Authority G2'],]],
      'notAfter': 'Sep 11 11:04:38 2014 GMT',
      'notBefore': 'Sep 11 11:04:38 2013 GMT',
      'serialNumber': '50C71E48BCC50676',
      'subject': [[['countryName', 'US'],],
                  [['stateOrProvinceName', 'California'],],
                  [['localityName', 'Mountain View'],],
                  [['organizationName', 'Google Inc'],],
                  [['commonName', 'www.google.com'],]],
      'subjectAltName': [['DNS', 'www.google.com'],],
      'version': 3},
     {'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'GeoTrust Inc.'],],
                 [['commonName', 'GeoTrust Global CA'],]],
      'notAfter': 'Apr  4 15:15:55 2015 GMT',
      'notBefore': 'Apr  5 15:15:55 2013 GMT',
      'serialNumber': '023A69',
      'subject': [[['countryName', 'US'],],
                  [['organizationName', 'Google Inc'],],
                  [['commonName', 'Google Internet Authority G2'],]],
      'version': 3},
     {'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'Equifax'],],
                 [['organizationalUnitName',
                   'Equifax Secure Certificate Authority'],]],
      'notAfter': 'Aug 21 04:00:00 2018 GMT',
      'notBefore': 'May 21 04:00:00 2002 GMT',
      'serialNumber': '12BBE6',
      'subject': [[['countryName', 'US'],],
                  [['organizationName', 'GeoTrust Inc.'],],
                  [['commonName', 'GeoTrust Global CA'],]],
      'version': 3},
     {'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'Equifax'],],
                 [['organizationalUnitName',
                   'Equifax Secure Certificate Authority'],]],
      'notAfter': 'Aug 22 16:41:51 2018 GMT',
      'notBefore': 'Aug 22 16:41:51 1998 GMT',
      'serialNumber': '35DEF4CF',
      'subject': [[['countryName', 'US'],],
                  [['organizationName', 'Equifax'],],
                  [['organizationalUnitName',
                    'Equifax Secure Certificate Authority'],]],
      'version': 3}]
    
    16 - Đường dẫn đã được giải quyết đến Capath hoặc
    ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
    ctx.options &= ~ssl.OP_NO_SSLv3
    
    4 nếu thư mục không tồn tại,

  • Hostname: www.google.com
    [{'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'Google Inc'],],
                 [['commonName', 'Google Internet Authority G2'],]],
      'notAfter': 'Sep 11 11:04:38 2014 GMT',
      'notBefore': 'Sep 11 11:04:38 2013 GMT',
      'serialNumber': '50C71E48BCC50676',
      'subject': [[['countryName', 'US'],],
                  [['stateOrProvinceName', 'California'],],
                  [['localityName', 'Mountain View'],],
                  [['organizationName', 'Google Inc'],],
                  [['commonName', 'www.google.com'],]],
      'subjectAltName': [['DNS', 'www.google.com'],],
      'version': 3},
     {'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'GeoTrust Inc.'],],
                 [['commonName', 'GeoTrust Global CA'],]],
      'notAfter': 'Apr  4 15:15:55 2015 GMT',
      'notBefore': 'Apr  5 15:15:55 2013 GMT',
      'serialNumber': '023A69',
      'subject': [[['countryName', 'US'],],
                  [['organizationName', 'Google Inc'],],
                  [['commonName', 'Google Internet Authority G2'],]],
      'version': 3},
     {'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'Equifax'],],
                 [['organizationalUnitName',
                   'Equifax Secure Certificate Authority'],]],
      'notAfter': 'Aug 21 04:00:00 2018 GMT',
      'notBefore': 'May 21 04:00:00 2002 GMT',
      'serialNumber': '12BBE6',
      'subject': [[['countryName', 'US'],],
                  [['organizationName', 'GeoTrust Inc.'],],
                  [['commonName', 'GeoTrust Global CA'],]],
      'version': 3},
     {'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'Equifax'],],
                 [['organizationalUnitName',
                   'Equifax Secure Certificate Authority'],]],
      'notAfter': 'Aug 22 16:41:51 2018 GMT',
      'notBefore': 'Aug 22 16:41:51 1998 GMT',
      'serialNumber': '35DEF4CF',
      'subject': [[['countryName', 'US'],],
                  [['organizationName', 'Equifax'],],
                  [['organizationalUnitName',
                    'Equifax Secure Certificate Authority'],]],
      'version': 3}]
    
    18 - Khóa môi trường OpenSSL chỉ vào CAFILE,

  • Hostname: www.google.com
    [{'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'Google Inc'],],
                 [['commonName', 'Google Internet Authority G2'],]],
      'notAfter': 'Sep 11 11:04:38 2014 GMT',
      'notBefore': 'Sep 11 11:04:38 2013 GMT',
      'serialNumber': '50C71E48BCC50676',
      'subject': [[['countryName', 'US'],],
                  [['stateOrProvinceName', 'California'],],
                  [['localityName', 'Mountain View'],],
                  [['organizationName', 'Google Inc'],],
                  [['commonName', 'www.google.com'],]],
      'subjectAltName': [['DNS', 'www.google.com'],],
      'version': 3},
     {'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'GeoTrust Inc.'],],
                 [['commonName', 'GeoTrust Global CA'],]],
      'notAfter': 'Apr  4 15:15:55 2015 GMT',
      'notBefore': 'Apr  5 15:15:55 2013 GMT',
      'serialNumber': '023A69',
      'subject': [[['countryName', 'US'],],
                  [['organizationName', 'Google Inc'],],
                  [['commonName', 'Google Internet Authority G2'],]],
      'version': 3},
     {'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'Equifax'],],
                 [['organizationalUnitName',
                   'Equifax Secure Certificate Authority'],]],
      'notAfter': 'Aug 21 04:00:00 2018 GMT',
      'notBefore': 'May 21 04:00:00 2002 GMT',
      'serialNumber': '12BBE6',
      'subject': [[['countryName', 'US'],],
                  [['organizationName', 'GeoTrust Inc.'],],
                  [['commonName', 'GeoTrust Global CA'],]],
      'version': 3},
     {'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'Equifax'],],
                 [['organizationalUnitName',
                   'Equifax Secure Certificate Authority'],]],
      'notAfter': 'Aug 22 16:41:51 2018 GMT',
      'notBefore': 'Aug 22 16:41:51 1998 GMT',
      'serialNumber': '35DEF4CF',
      'subject': [[['countryName', 'US'],],
                  [['organizationName', 'Equifax'],],
                  [['organizationalUnitName',
                    'Equifax Secure Certificate Authority'],]],
      'version': 3}]
    
    19 - Đường dẫn được mã hóa cứng đến CAFILE,

  • Hostname: www.google.com
    [{'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'Google Inc'],],
                 [['commonName', 'Google Internet Authority G2'],]],
      'notAfter': 'Sep 11 11:04:38 2014 GMT',
      'notBefore': 'Sep 11 11:04:38 2013 GMT',
      'serialNumber': '50C71E48BCC50676',
      'subject': [[['countryName', 'US'],],
                  [['stateOrProvinceName', 'California'],],
                  [['localityName', 'Mountain View'],],
                  [['organizationName', 'Google Inc'],],
                  [['commonName', 'www.google.com'],]],
      'subjectAltName': [['DNS', 'www.google.com'],],
      'version': 3},
     {'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'GeoTrust Inc.'],],
                 [['commonName', 'GeoTrust Global CA'],]],
      'notAfter': 'Apr  4 15:15:55 2015 GMT',
      'notBefore': 'Apr  5 15:15:55 2013 GMT',
      'serialNumber': '023A69',
      'subject': [[['countryName', 'US'],],
                  [['organizationName', 'Google Inc'],],
                  [['commonName', 'Google Internet Authority G2'],]],
      'version': 3},
     {'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'Equifax'],],
                 [['organizationalUnitName',
                   'Equifax Secure Certificate Authority'],]],
      'notAfter': 'Aug 21 04:00:00 2018 GMT',
      'notBefore': 'May 21 04:00:00 2002 GMT',
      'serialNumber': '12BBE6',
      'subject': [[['countryName', 'US'],],
                  [['organizationName', 'GeoTrust Inc.'],],
                  [['commonName', 'GeoTrust Global CA'],]],
      'version': 3},
     {'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'Equifax'],],
                 [['organizationalUnitName',
                   'Equifax Secure Certificate Authority'],]],
      'notAfter': 'Aug 22 16:41:51 2018 GMT',
      'notBefore': 'Aug 22 16:41:51 1998 GMT',
      'serialNumber': '35DEF4CF',
      'subject': [[['countryName', 'US'],],
                  [['organizationName', 'Equifax'],],
                  [['organizationalUnitName',
                    'Equifax Secure Certificate Authority'],]],
      'version': 3}]
    
    20 - Khóa môi trường OpenSSL chỉ vào Capath,

  • Hostname: www.google.com
    [{'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'Google Inc'],],
                 [['commonName', 'Google Internet Authority G2'],]],
      'notAfter': 'Sep 11 11:04:38 2014 GMT',
      'notBefore': 'Sep 11 11:04:38 2013 GMT',
      'serialNumber': '50C71E48BCC50676',
      'subject': [[['countryName', 'US'],],
                  [['stateOrProvinceName', 'California'],],
                  [['localityName', 'Mountain View'],],
                  [['organizationName', 'Google Inc'],],
                  [['commonName', 'www.google.com'],]],
      'subjectAltName': [['DNS', 'www.google.com'],],
      'version': 3},
     {'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'GeoTrust Inc.'],],
                 [['commonName', 'GeoTrust Global CA'],]],
      'notAfter': 'Apr  4 15:15:55 2015 GMT',
      'notBefore': 'Apr  5 15:15:55 2013 GMT',
      'serialNumber': '023A69',
      'subject': [[['countryName', 'US'],],
                  [['organizationName', 'Google Inc'],],
                  [['commonName', 'Google Internet Authority G2'],]],
      'version': 3},
     {'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'Equifax'],],
                 [['organizationalUnitName',
                   'Equifax Secure Certificate Authority'],]],
      'notAfter': 'Aug 21 04:00:00 2018 GMT',
      'notBefore': 'May 21 04:00:00 2002 GMT',
      'serialNumber': '12BBE6',
      'subject': [[['countryName', 'US'],],
                  [['organizationName', 'GeoTrust Inc.'],],
                  [['commonName', 'GeoTrust Global CA'],]],
      'version': 3},
     {'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'Equifax'],],
                 [['organizationalUnitName',
                   'Equifax Secure Certificate Authority'],]],
      'notAfter': 'Aug 22 16:41:51 2018 GMT',
      'notBefore': 'Aug 22 16:41:51 1998 GMT',
      'serialNumber': '35DEF4CF',
      'subject': [[['countryName', 'US'],],
                  [['organizationName', 'Equifax'],],
                  [['organizationalUnitName',
                    'Equifax Secure Certificate Authority'],]],
      'version': 3}]
    
    21 - Đường dẫn mã hóa cứng đến thư mục Capath

Mới trong phiên bản 3.4.

ssl.enum_certificates [store_name] ¶enum_certificates[store_name]

Truy xuất chứng chỉ từ cửa hàng chứng chỉ hệ thống windows. Store_name có thể là một trong số

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
22,
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
23 hoặc
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
24. Windows cũng có thể cung cấp các cửa hàng chứng chỉ bổ sung.

Hàm trả về một danh sách các bộ dữ liệu [cert_bytes, expoding_type, tin cậy]. Mã hóa_type chỉ định mã hóa Cert_Bytes. Đó là

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
25 cho dữ liệu X.509 ASN.1 hoặc
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
26 cho dữ liệu PKCS#7 ASN.1. Tin tưởng chỉ định mục đích của chứng chỉ là một tập hợp OID hoặc chính xác
>>> ssl.enum_certificates["CA"]
[[b'data...', 'x509_asn', {'1.3.6.1.5.5.7.3.1', '1.3.6.1.5.5.7.3.2'}],
 [b'data...', 'x509_asn', True]]
7 nếu chứng chỉ đáng tin cậy cho tất cả các mục đích.

Example:

>>> ssl.enum_certificates["CA"]
[[b'data...', 'x509_asn', {'1.3.6.1.5.5.7.3.1', '1.3.6.1.5.5.7.3.2'}],
 [b'data...', 'x509_asn', True]]

Tính khả dụng: Windows.: Windows.

Mới trong phiên bản 3.4.

ssl.enum_crls [store_name] ¶enum_crls[store_name]

Truy xuất CRL từ Cửa hàng Cert System Windows. Store_name có thể là một trong số

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
22,
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
23 hoặc
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
24. Windows cũng có thể cung cấp các cửa hàng chứng chỉ bổ sung.

Hàm trả về một danh sách các bộ dữ liệu [cert_bytes, expoding_type, tin cậy]. Mã hóa_type chỉ định mã hóa Cert_Bytes. Đó là

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
25 cho dữ liệu X.509 ASN.1 hoặc
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
26 cho dữ liệu PKCS#7 ASN.1.

Tính khả dụng: Windows.: Windows.

Mới trong phiên bản 3.4.

ssl.wrap_socket [sock, keyfile = none, certfile = none, server_sidewrap_socket[sock, keyfile=None, certfile=None, server_side=False, cert_reqs=CERT_NONE, ssl_version=PROTOCOL_TLS, ca_certs=None, do_handshake_on_connect=True, suppress_ragged_eofs=True, ciphers=None]

Lấy một ví dụ

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
33 của
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
9 và trả về một ví dụ là
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
8, một kiểu con của
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
9, bao bọc ổ cắm cơ bản trong bối cảnh SSL.
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
33 phải là ổ cắm
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
38; Các loại ổ cắm khác không được hỗ trợ.

Trong nội bộ, chức năng tạo ra

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
5 với giao thức SSL_Version và
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
40 được đặt thành cert_reqs. Nếu các tham số KEYFILE, CERTFILE, CA_CERTS hoặc mật mã được đặt, thì các giá trị được truyền đến
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
41,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
3 và
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
43.

Các đối số server_side, do_handshake_on_connect và urpress_ragged_eofs có ý nghĩa tương tự như

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
3.

Đã không dùng từ phiên bản 3.7: Kể từ Python 3.2 và 2.7.9, nên sử dụng

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
3 thay vì
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
8. Hàm cấp cao nhất bị giới hạn và tạo ra một ổ cắm máy khách không an toàn mà không cần chỉ định tên máy chủ hoặc khớp tên máy chủ.Since Python 3.2 and 2.7.9, it is recommended to use the
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
3 instead of
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
8. The top-level function is limited and creates an insecure client socket without server name indication or hostname matching.

Hằng số trong

ssl.cert_none¶CERT_NONE

Giá trị có thể cho

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
47 hoặc tham số
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
48 thành
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
8. Ngoại trừ
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
5, nó là chế độ mặc định. Với ổ cắm phía khách hàng, bất kỳ chứng chỉ nào cũng được chấp nhận. Các lỗi xác thực, chẳng hạn như chứng nhận không tin cậy hoặc hết hạn, bị bỏ qua và không phá thai bắt tay TLS/SSL.

Trong chế độ máy chủ, không có chứng chỉ nào được yêu cầu từ máy khách, vì vậy khách hàng không gửi bất kỳ cho xác thực CERT của khách hàng.

Xem các cuộc thảo luận về các cân nhắc bảo mật dưới đây.Security considerations below.

ssl.cert_optional¶CERT_OPTIONAL

Giá trị có thể cho

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
47 hoặc tham số
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
48 thành
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
8. Trong chế độ máy khách,
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
54 có ý nghĩa tương tự như
>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
1. Thay vào đó, bạn nên sử dụng
>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
1 cho ổ cắm phía máy khách.

Trong chế độ máy chủ, yêu cầu chứng chỉ máy khách được gửi đến máy khách. Khách hàng có thể bỏ qua yêu cầu hoặc gửi chứng chỉ để thực hiện xác thực CERT của TLS Client Cert. Nếu khách hàng chọn gửi chứng chỉ, nó đã được xác minh. Bất kỳ lỗi xác minh ngay lập tức hủy bỏ cái bắt tay TLS.

Việc sử dụng cài đặt này đòi hỏi một bộ chứng chỉ CA hợp lệ phải được thông qua, đến

ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
3 hoặc là giá trị của tham số
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
07 thành
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
8.

ssl.cert_required¶CERT_REQUIRED

Giá trị có thể cho

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
47 hoặc tham số
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
48 thành
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
8. Trong chế độ này, các chứng chỉ được yêu cầu từ phía bên kia của kết nối ổ cắm; Một
>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43
1 sẽ được nêu ra nếu không có chứng chỉ nào được cung cấp hoặc nếu xác thực của nó không thành công. Chế độ này không đủ để xác minh chứng chỉ ở chế độ máy khách vì nó không khớp với tên máy chủ.
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
64 cũng phải được kích hoạt để xác minh tính xác thực của chứng chỉ.
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
5 sử dụng
>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
1 và cho phép
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
64 theo mặc định.not sufficient to verify a certificate in client mode as it does not match hostnames.
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
64 must be enabled as well to verify the authenticity of a cert.
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
5 uses
>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
1 and enables
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
64 by default.

Với ổ cắm máy chủ, chế độ này cung cấp xác thực chứng chỉ máy khách TLS bắt buộc. Yêu cầu chứng chỉ máy khách được gửi cho khách hàng và khách hàng phải cung cấp chứng chỉ hợp lệ và đáng tin cậy.

Việc sử dụng cài đặt này đòi hỏi một bộ chứng chỉ CA hợp lệ phải được thông qua, đến

ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
3 hoặc là giá trị của tham số
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
07 thành
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
8.

ssl.cert_required¶ssl.VerifyMode

Giá trị có thể cho

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
47 hoặc tham số
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
48 thành
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
8. Trong chế độ này, các chứng chỉ được yêu cầu từ phía bên kia của kết nối ổ cắm; Một
>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43
1 sẽ được nêu ra nếu không có chứng chỉ nào được cung cấp hoặc nếu xác thực của nó không thành công. Chế độ này không đủ để xác minh chứng chỉ ở chế độ máy khách vì nó không khớp với tên máy chủ.
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
64 cũng phải được kích hoạt để xác minh tính xác thực của chứng chỉ.
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
5 sử dụng
>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
1 và cho phép
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
64 theo mặc định.

Với ổ cắm máy chủ, chế độ này cung cấp xác thực chứng chỉ máy khách TLS bắt buộc. Yêu cầu chứng chỉ máy khách được gửi cho khách hàng và khách hàng phải cung cấp chứng chỉ hợp lệ và đáng tin cậy.

classSSSL.verifyMode¶VERIFY_DEFAULT

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
71 Bộ sưu tập hằng số cert_*.

Mới trong phiên bản 3.4.

Mới trong phiên bản 3.6.VERIFY_CRL_CHECK_LEAF

ssl.verify_default¶

Mới trong phiên bản 3.4.

Giá trị có thể cho
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
72. Trong chế độ này, danh sách thu hồi chứng chỉ [CRLS] không được kiểm tra. Theo mặc định, OpenSSL không yêu cầu cũng không xác minh CRL.
VERIFY_CRL_CHECK_CHAIN

ssl.verify_crl_check_leaf¶

Mới trong phiên bản 3.4.

Giá trị có thể cho
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
72. Trong chế độ này, chỉ có chứng chỉ ngang hàng được kiểm tra nhưng không có chứng chỉ CA trung gian nào. Chế độ yêu cầu CRL hợp lệ được ký bởi nhà phát hành Peer Cert [tổ tiên trực tiếp CA]. Nếu không có CRL thích hợp nào được tải với
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
74, xác thực sẽ không thành công.
VERIFY_X509_STRICT

ssl.verify_crl_check_chain¶

Mới trong phiên bản 3.4.

Giá trị có thể cho
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
72. Trong chế độ này, CRLS của tất cả các chứng chỉ trong chuỗi cert Peer được kiểm tra.
VERIFY_ALLOW_PROXY_CERTS

ssl.verify_x509_strict¶

Giá trị có thể cho

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
72 để vô hiệu hóa các cách giải quyết cho chứng chỉ X.509 bị hỏng.

ssl.verify_x509_trusted_first¶VERIFY_X509_TRUSTED_FIRST

Giá trị có thể cho

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
72. Nó hướng dẫn OpenSSL thích chứng chỉ đáng tin cậy khi xây dựng chuỗi ủy thác để xác nhận chứng chỉ. Cờ này được bật theo mặc định.

Mới trong phiên bản 3.4.4.

ssl.verify_x509_partial_chain¶VERIFY_X509_PARTIAL_CHAIN

Giá trị có thể cho

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
72. Nó hướng dẫn OpenSSL chấp nhận CAS trung gian trong cửa hàng ủy thác được coi là người khai thác tin cậy, giống như các chứng chỉ CA gốc tự ký. Điều này cho phép có thể tin tưởng các chứng chỉ được cấp bởi CA trung gian mà không phải tin tưởng Root Ca.

Mới trong phiên bản 3.10.

classSSSL.verifyflags¶ssl.VerifyFlags

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
80 Bộ sưu tập hằng số của Verify_*.

Mới trong phiên bản 3.6.

ssl.protocol_tls¶PROTOCOL_TLS

Chọn phiên bản giao thức cao nhất mà cả hỗ trợ máy khách và máy chủ. Mặc dù tên, tùy chọn này có thể chọn cả hai giao thức của SSL SSL và TLS.

Mới trong phiên bản 3.6.

ssl.protocol_tls¶TLS clients and servers require different default settings for secure communication. The generic TLS protocol constant is deprecated in favor of

ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
5 and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
6.

Chọn phiên bản giao thức cao nhất mà cả hỗ trợ máy khách và máy chủ. Mặc dù tên, tùy chọn này có thể chọn cả hai giao thức của SSL SSL và TLS.PROTOCOL_TLS_CLIENT

Không dùng nữa kể từ phiên bản 3.10: Máy khách và máy chủ TLS yêu cầu các cài đặt mặc định khác nhau để giao tiếp an toàn. Hằng số giao thức TLS chung không được dùng để có lợi cho

ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
5 và
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
6.

Mới trong phiên bản 3.6.

ssl.protocol_tls¶PROTOCOL_TLS_SERVER

Chọn phiên bản giao thức cao nhất mà cả hỗ trợ máy khách và máy chủ. Mặc dù tên, tùy chọn này có thể chọn cả hai giao thức của SSL SSL và TLS.

Mới trong phiên bản 3.6.

ssl.protocol_tls¶PROTOCOL_SSLv23

Chọn phiên bản giao thức cao nhất mà cả hỗ trợ máy khách và máy chủ. Mặc dù tên, tùy chọn này có thể chọn cả hai giao thức của SSL SSL và TLS.

Không dùng nữa kể từ phiên bản 3.10: Máy khách và máy chủ TLS yêu cầu các cài đặt mặc định khác nhau để giao tiếp an toàn. Hằng số giao thức TLS chung không được dùng để có lợi cho

ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
5 và
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
6.Use
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
11 instead.

ssl.protocol_tls_client¶PROTOCOL_SSLv2

Tự động đàm phán phiên bản giao thức cao nhất mà cả hỗ trợ máy khách và máy chủ và định cấu hình các kết nối phía máy khách bối cảnh. Giao thức cho phép

>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
1 và
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
64 theo mặc định.

ssl.protocol_tls_server¶

Tự động đàm phán phiên bản giao thức cao nhất mà cả hỗ trợ máy khách và máy chủ và định cấu hình các kết nối phía máy chủ ngữ cảnh.

ssl.protocol_sslv23¶

Bí danh cho

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
11.OpenSSL has removed support for SSLv2.

Thay thế từ phiên bản 3.6: sử dụng
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
11 thay thế.
PROTOCOL_SSLv3

ssl.protocol_sslv2¶

Chọn SSL phiên bản 2 làm giao thức mã hóa kênh.

Tự động đàm phán phiên bản giao thức cao nhất mà cả hỗ trợ máy khách và máy chủ và định cấu hình các kết nối phía máy chủ ngữ cảnh.

ssl.protocol_sslv23¶

Bí danh cho
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
11.
PROTOCOL_TLSv1

Thay thế từ phiên bản 3.6: sử dụng

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
11 thay thế.

ssl.protocol_sslv2¶OpenSSL has deprecated all version specific protocols.

Chọn SSL phiên bản 2 làm giao thức mã hóa kênh.PROTOCOL_TLSv1_1

Giao thức này không có sẵn nếu OpenSSL được biên dịch với tùy chọn

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
87.

Cảnh báo

ssl.protocol_sslv2¶OpenSSL has deprecated all version specific protocols.

Chọn SSL phiên bản 2 làm giao thức mã hóa kênh.PROTOCOL_TLSv1_2

Giao thức này không có sẵn nếu OpenSSL được biên dịch với tùy chọn

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
87.

Cảnh báo

ssl.protocol_sslv2¶OpenSSL has deprecated all version specific protocols.

Chọn SSL phiên bản 2 làm giao thức mã hóa kênh.OP_ALL

Giao thức này không có sẵn nếu OpenSSL được biên dịch với tùy chọn

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
87.

Cảnh báo

Phiên bản SSL 2 không an toàn. Việc sử dụng nó rất nản lòng.OP_NO_SSLv2

Không dùng nữa kể từ phiên bản 3.6: OpenSSL đã loại bỏ hỗ trợ cho SSLV2.

Cảnh báo

Phiên bản SSL 2 không an toàn. Việc sử dụng nó rất nản lòng.SSLv2 is deprecated

Không dùng nữa kể từ phiên bản 3.6: OpenSSL đã loại bỏ hỗ trợ cho SSLV2.OP_NO_SSLv3

ssl.protocol_sslv3¶

Cảnh báo

Phiên bản SSL 2 không an toàn. Việc sử dụng nó rất nản lòng.SSLv3 is deprecated

Không dùng nữa kể từ phiên bản 3.6: OpenSSL đã loại bỏ hỗ trợ cho SSLV2.OP_NO_TLSv1

ssl.protocol_sslv3¶

Cảnh báo

Phiên bản SSL 2 không an toàn. Việc sử dụng nó rất nản lòng.OP_NO_TLSv1_1

Không dùng nữa kể từ phiên bản 3.6: OpenSSL đã loại bỏ hỗ trợ cho SSLV2.

Cảnh báo

Phiên bản SSL 2 không an toàn. Việc sử dụng nó rất nản lòng.The option is deprecated since OpenSSL 1.1.0.

Không dùng nữa kể từ phiên bản 3.6: OpenSSL đã loại bỏ hỗ trợ cho SSLV2.OP_NO_TLSv1_2

ssl.protocol_sslv3¶

Cảnh báo

Phiên bản SSL 2 không an toàn. Việc sử dụng nó rất nản lòng.The option is deprecated since OpenSSL 1.1.0.

Không dùng nữa kể từ phiên bản 3.6: OpenSSL đã loại bỏ hỗ trợ cho SSLV2.OP_NO_TLSv1_3

ssl.protocol_sslv3¶

Mới trong phiên bản 3.7.

Không dùng nữa kể từ phiên bản 3.7: Tùy chọn được không dùng nữa kể từ khi OpenSSL 1.1.0. Nó đã được thêm vào 2.7.15, 3.6.3 và 3.7.0 để tương thích ngược với OpenSSL 1.0.2.The option is deprecated since OpenSSL 1.1.0. It was added to 2.7.15, 3.6.3 and 3.7.0 for backwards compatibility with OpenSSL 1.0.2.

SSL.OP_NO_RENEGOTIATION¶OP_NO_RENEGOTIATION

Vô hiệu hóa tất cả các cuộc đàm phán lại trong TLSV1.2 trở lên. Không gửi tin nhắn HelloreQuest và bỏ qua các yêu cầu đàm phán lại thông qua ClientHello.

Tùy chọn này chỉ có sẵn với OpenSSL 1.1.0h trở lên.

Mới trong phiên bản 3.7.

SSL.OP_CIPHER_SERVER_PREFERTEANT¶OP_CIPHER_SERVER_PREFERENCE

Sử dụng ưu tiên đặt hàng mật mã của máy chủ, thay vì máy khách. Tùy chọn này không có tác dụng đối với ổ cắm máy khách và ổ cắm máy chủ SSLV2.

Mới trong phiên bản 3.3.

ssl.op_single_dh_use¶OP_SINGLE_DH_USE

Ngăn chặn sử dụng lại cùng một khóa DH cho các phiên SSL riêng biệt. Điều này cải thiện bí mật về phía trước nhưng đòi hỏi nhiều tài nguyên tính toán hơn. Tùy chọn này chỉ áp dụng cho ổ cắm máy chủ.

Mới trong phiên bản 3.3.

ssl.op_single_dh_use¶OP_SINGLE_ECDH_USE

Ngăn chặn sử dụng lại cùng một khóa DH cho các phiên SSL riêng biệt. Điều này cải thiện bí mật về phía trước nhưng đòi hỏi nhiều tài nguyên tính toán hơn. Tùy chọn này chỉ áp dụng cho ổ cắm máy chủ.

Mới trong phiên bản 3.3.

ssl.op_single_dh_use¶OP_ENABLE_MIDDLEBOX_COMPAT

Ngăn chặn sử dụng lại cùng một khóa DH cho các phiên SSL riêng biệt. Điều này cải thiện bí mật về phía trước nhưng đòi hỏi nhiều tài nguyên tính toán hơn. Tùy chọn này chỉ áp dụng cho ổ cắm máy chủ.

ssl.op_single_ecdh_use¶

Ngăn chặn sử dụng lại cùng một khóa ECDH cho các phiên SSL riêng biệt. Điều này cải thiện bí mật về phía trước nhưng đòi hỏi nhiều tài nguyên tính toán hơn. Tùy chọn này chỉ áp dụng cho ổ cắm máy chủ.

ssl.op_enable_middlebox_compat¶OP_NO_COMPRESSION

Gửi thông báo thay đổi giả thông [CCS] trong TLS 1.3 Handshake để làm cho kết nối TLS 1.3 trông giống như kết nối TLS 1.2.

Mới trong phiên bản 3.3.

ssl.op_single_dh_use¶ssl.Options

Ngăn chặn sử dụng lại cùng một khóa DH cho các phiên SSL riêng biệt. Điều này cải thiện bí mật về phía trước nhưng đòi hỏi nhiều tài nguyên tính toán hơn. Tùy chọn này chỉ áp dụng cho ổ cắm máy chủ.

ssl.op_single_ecdh_use¶OP_NO_TICKET

Ngăn chặn sử dụng lại cùng một khóa ECDH cho các phiên SSL riêng biệt. Điều này cải thiện bí mật về phía trước nhưng đòi hỏi nhiều tài nguyên tính toán hơn. Tùy chọn này chỉ áp dụng cho ổ cắm máy chủ.

ssl.op_enable_middlebox_compat¶

Gửi thông báo thay đổi giả thông [CCS] trong TLS 1.3 Handshake để làm cho kết nối TLS 1.3 trông giống như kết nối TLS 1.2.OP_IGNORE_UNEXPECTED_EOF

Tùy chọn này chỉ có sẵn với OpenSSL 1.1.1 trở lên.

Mới trong phiên bản 3.8.

ssl.op_no_compression¶

Tắt nén trên kênh SSL. Điều này rất hữu ích nếu giao thức ứng dụng hỗ trợ sơ đồ nén của chính nó.HAS_ALPN

classSSSSL.Options¶RFC 7301.

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
80 Bộ sưu tập hằng số op_*.

ssl.op_no_ticket¶HAS_NEVER_CHECK_COMMON_NAME

Ngăn chặn phía khách hàng yêu cầu một vé phiên.

Mới trong phiên bản 3.7.

Mới trong phiên bản 3.6.HAS_ECDH

ssl.op_ignore_unexpected_eof¶

Mới trong phiên bản 3.3.

ssl.op_single_dh_use¶HAS_SNI

Ngăn chặn sử dụng lại cùng một khóa DH cho các phiên SSL riêng biệt. Điều này cải thiện bí mật về phía trước nhưng đòi hỏi nhiều tài nguyên tính toán hơn. Tùy chọn này chỉ áp dụng cho ổ cắm máy chủ.RFC 6066].

ssl.op_single_ecdh_use¶

Ngăn chặn sử dụng lại cùng một khóa ECDH cho các phiên SSL riêng biệt. Điều này cải thiện bí mật về phía trước nhưng đòi hỏi nhiều tài nguyên tính toán hơn. Tùy chọn này chỉ áp dụng cho ổ cắm máy chủ.HAS_NPN

ssl.op_enable_middlebox_compat¶

Mới trong phiên bản 3.3.

ssl.op_single_dh_use¶HAS_SSLv2

Ngăn chặn sử dụng lại cùng một khóa DH cho các phiên SSL riêng biệt. Điều này cải thiện bí mật về phía trước nhưng đòi hỏi nhiều tài nguyên tính toán hơn. Tùy chọn này chỉ áp dụng cho ổ cắm máy chủ.

Mới trong phiên bản 3.7.

ssl.op_single_ecdh_use¶HAS_SSLv3

Ngăn chặn sử dụng lại cùng một khóa ECDH cho các phiên SSL riêng biệt. Điều này cải thiện bí mật về phía trước nhưng đòi hỏi nhiều tài nguyên tính toán hơn. Tùy chọn này chỉ áp dụng cho ổ cắm máy chủ.

Mới trong phiên bản 3.7.

ssl.op_enable_middlebox_compat¶HAS_TLSv1

Gửi thông báo thay đổi giả thông [CCS] trong TLS 1.3 Handshake để làm cho kết nối TLS 1.3 trông giống như kết nối TLS 1.2.

Mới trong phiên bản 3.7.

Tùy chọn này chỉ có sẵn với OpenSSL 1.1.1 trở lên.HAS_TLSv1_1

Mới trong phiên bản 3.8.

Mới trong phiên bản 3.7.

ssl.op_no_compression¶HAS_TLSv1_2

Tắt nén trên kênh SSL. Điều này rất hữu ích nếu giao thức ứng dụng hỗ trợ sơ đồ nén của chính nó.

Mới trong phiên bản 3.7.

classSSSSL.Options¶HAS_TLSv1_3

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
80 Bộ sưu tập hằng số op_*.

Mới trong phiên bản 3.7.

ssl.op_no_ticket¶CHANNEL_BINDING_TYPES

Ngăn chặn phía khách hàng yêu cầu một vé phiên.

Mới trong phiên bản 3.3.

ssl.op_single_dh_use¶OPENSSL_VERSION

Ngăn chặn sử dụng lại cùng một khóa DH cho các phiên SSL riêng biệt. Điều này cải thiện bí mật về phía trước nhưng đòi hỏi nhiều tài nguyên tính toán hơn. Tùy chọn này chỉ áp dụng cho ổ cắm máy chủ.

>>> ssl.OPENSSL_VERSION
'OpenSSL 1.0.2k  26 Jan 2017'

ssl.op_single_ecdh_use¶

Ngăn chặn sử dụng lại cùng một khóa ECDH cho các phiên SSL riêng biệt. Điều này cải thiện bí mật về phía trước nhưng đòi hỏi nhiều tài nguyên tính toán hơn. Tùy chọn này chỉ áp dụng cho ổ cắm máy chủ.OPENSSL_VERSION_INFO

ssl.op_enable_middlebox_compat¶

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
0

ssl.op_single_ecdh_use¶

Ngăn chặn sử dụng lại cùng một khóa ECDH cho các phiên SSL riêng biệt. Điều này cải thiện bí mật về phía trước nhưng đòi hỏi nhiều tài nguyên tính toán hơn. Tùy chọn này chỉ áp dụng cho ổ cắm máy chủ.OPENSSL_VERSION_NUMBER

ssl.op_enable_middlebox_compat¶

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
1

ssl.op_single_ecdh_use¶

Ngăn chặn sử dụng lại cùng một khóa ECDH cho các phiên SSL riêng biệt. Điều này cải thiện bí mật về phía trước nhưng đòi hỏi nhiều tài nguyên tính toán hơn. Tùy chọn này chỉ áp dụng cho ổ cắm máy chủ.ALERT_DESCRIPTION_HANDSHAKE_FAILUREssl.ALERT_DESCRIPTION_INTERNAL_ERRORALERT_DESCRIPTION_*

ssl.op_enable_middlebox_compat¶RFC 5246 and others. The IANA TLS Alert Registry contains this list and references to the RFCs where their meaning is defined.

Gửi thông báo thay đổi giả thông [CCS] trong TLS 1.3 Handshake để làm cho kết nối TLS 1.3 trông giống như kết nối TLS 1.2.

Tùy chọn này chỉ có sẵn với OpenSSL 1.1.1 trở lên.

Mới trong phiên bản 3.8.ssl.AlertDescription

ssl.op_no_compression¶

ssl.op_enable_middlebox_compat¶

Gửi thông báo thay đổi giả thông [CCS] trong TLS 1.3 Handshake để làm cho kết nối TLS 1.3 trông giống như kết nối TLS 1.2.SERVER_AUTH

Tùy chọn này chỉ có sẵn với OpenSSL 1.1.1 trở lên.

Mới trong phiên bản 3.4.

Mục đích.Client_Auth¶¶CLIENT_AUTH

Tùy chọn cho

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
7 và
>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
2. Giá trị này chỉ ra rằng bối cảnh có thể được sử dụng để xác thực các máy khách web [do đó, nó sẽ được sử dụng để tạo ổ cắm phía máy chủ].

Mới trong phiên bản 3.4.

classSSSL.sslerrornumber¶ ssl.SSLErrorNumber

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
71 Bộ sưu tập hằng số SSL_ERROR_*.

Mới trong phiên bản 3.6.

classSSSL.tlSversion¶ ssl.TLSVersion

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
71 Bộ sưu tập các phiên bản SSL và TLS cho
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
08 và
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
09.

Mới trong phiên bản 3.7.

TLSversion.Minimum_Supported¶ tlSversion.maximum_supported¶MINIMUM_SUPPORTEDTLSVersion.MAXIMUM_SUPPORTED

Phiên bản SSL hoặc TLS tối thiểu hoặc tối đa. Đây là những hằng số ma thuật. Giá trị của chúng don don phản ánh các phiên bản TLS/SSL thấp nhất và cao nhất.

TLSversion.sslv3¶ tlsversion.tlsv1¶ tlsversion.tlsv1_1¶ tlsversion.tlsv1_2¶ tlsversion.tlsv1_3¶SSLv3TLSVersion.TLSv1TLSVersion.TLSv1_1TLSVersion.TLSv1_2TLSVersion.TLSv1_3

SSL 3.0 đến TLS 1.3.

SSL ổ cắm

classSSSL.SSLSocket [socket.socket] ¶ssl.SSLSocket[socket.socket]

Các ổ cắm SSL cung cấp các phương thức sau của các đối tượng ổ cắm:Socket Objects:

  • import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    10

  • import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    11

  • import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    12

  • import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    13

  • import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    14

  • import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    15

  • import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    16,
    import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    17

  • import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    18,
    import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    19

  • import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    20,
    import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    21,
    import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    22

  • import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    23

  • import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    24

  • import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    25,
    import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    26 [nhưng không cho phép đối số không khác
    import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    27]

  • import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    28,
    import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    29 [có cùng giới hạn]

  • import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    30 [nhưng
    import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    31 sẽ chỉ được sử dụng cho các ổ cắm văn bản đơn giản, nếu không
    import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    28 sẽ được sử dụng]

  • import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    33

Tuy nhiên, do giao thức SSL [và TLS] có khung riêng của TCP, nên sự trừu tượng của SSL ổ cắm có thể, một số khía cạnh, có thể chuyển hướng từ đặc điểm kỹ thuật của ổ cắm cấp độ OS bình thường. Xem đặc biệt các ghi chú trên ổ cắm không chặn.notes on non-blocking sockets.

Các phiên bản của

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
6 phải được tạo bằng phương pháp
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
3.

Thay đổi trong phiên bản 3.5: Phương pháp

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
30 đã được thêm vào.The
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
30 method was added.

Đã thay đổi trong phiên bản 3.5:

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
33 không đặt lại thời gian chờ ổ cắm mỗi lần được nhận hoặc gửi. Thời gian chờ ổ cắm hiện nay là tổng thời gian tắt tối đa.The
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
33 does not reset the socket timeout each time bytes are received or sent. The socket timeout is now to maximum total duration of the shutdown.

Đã không dùng từ Phiên bản 3.6: Không dùng để tạo phiên bản

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
6 trực tiếp, sử dụng
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
3 để bọc ổ cắm.It is deprecated to create a
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
6 instance directly, use
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
3 to wrap a socket.

Đã thay đổi trong phiên bản 3.7:

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
6 phải được tạo bằng
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
8. Trong các phiên bản trước, có thể tạo các trường hợp trực tiếp. Điều này chưa bao giờ được ghi nhận hoặc chính thức được hỗ trợ.
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
6 instances must to created with
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
8. In earlier versions, it was possible to create instances directly. This was never documented or officially supported.

Đã thay đổi trong phiên bản 3.10: Python hiện sử dụng

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
42 và
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
43 trong nội bộ. Các chức năng hỗ trợ đọc và ghi dữ liệu lớn hơn 2 GB. Viết dữ liệu có độ dài không còn thất bại với lỗi vi phạm giao thức.Python now uses
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
42 and
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
43 internally. The functions support reading and writing of data larger than 2 GB. Writing zero-length data no longer fails with a protocol violation error.

Các ổ cắm SSL cũng có các phương thức và thuộc tính bổ sung sau:

Sslsocket.read [len = 1024, buffer = none] ¶read[len=1024, buffer=None]

Đọc lên đến LEN byte dữ liệu từ ổ cắm SSL và trả về kết quả dưới dạng ví dụ

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
44. Nếu bộ đệm được chỉ định, sau đó đọc vào bộ đệm thay thế và trả về số byte đọc.

Tăng

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
45 hoặc
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
46 nếu ổ cắm không chặn và đọc sẽ chặn.non-blocking and the read would block.

Vì bất cứ lúc nào cũng có thể đàm phán lại, một cuộc gọi đến

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
47 cũng có thể gây ra các hoạt động ghi.

Đã thay đổi trong phiên bản 3.5: Thời gian chờ ổ cắm không còn đặt lại mỗi lần byte được nhận hoặc gửi. Thời gian chờ ổ cắm hiện nay là tổng thời lượng tối đa để đọc lên đến LEN byte.The socket timeout is no more reset each time bytes are received or sent. The socket timeout is now to maximum total duration to read up to len bytes.

Không dùng nữa kể từ phiên bản 3.6: Sử dụng

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
25 thay vì
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
47.Use
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
25 instead of
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
47.

SSLSocket.Write [BUF] ¶write[buf]

Viết BUF vào ổ cắm SSL và trả về số byte được viết. Đối số BUF phải là một đối tượng hỗ trợ giao diện bộ đệm.

Tăng

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
45 hoặc
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
46 nếu ổ cắm không chặn và việc ghi sẽ chặn.non-blocking and the write would block.

Vì bất cứ lúc nào cũng có thể đàm phán lại, một cuộc gọi đến

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
52 cũng có thể gây ra các hoạt động đọc.

Đã thay đổi trong phiên bản 3.5: Thời gian chờ ổ cắm không còn đặt lại mỗi lần byte được nhận hoặc gửi. Thời gian chờ ổ cắm hiện là tổng thời lượng tối đa để viết BUF.The socket timeout is no more reset each time bytes are received or sent. The socket timeout is now to maximum total duration to write buf.

Không dùng nữa kể từ phiên bản 3.6: Sử dụng

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
28 thay vì
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
52.Use
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
28 instead of
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
52.

Ghi chú

Các phương pháp

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
47 và
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
52 là các phương pháp cấp thấp đọc và ghi dữ liệu cấp độ ứng dụng, không được mã hóa và giải mã/mã hóa nó thành dữ liệu cấp độ dây được mã hóa. Các phương pháp này yêu cầu kết nối SSL hoạt động, tức là bắt tay đã được hoàn thành và
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
57 không được gọi.

Thông thường, bạn nên sử dụng các phương thức API ổ cắm như

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
25 và
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
28 thay vì các phương thức này.

Sslsocket.do_handshake [] ¶do_handshake[]

Thực hiện bắt tay thiết lập SSL.

Đã thay đổi trong phiên bản 3.5: Thời gian chờ ổ cắm không còn đặt lại mỗi lần byte được nhận hoặc gửi. Thời gian chờ ổ cắm hiện nay là tổng thời lượng tối đa của bắt tay.The socket timeout is no more reset each time bytes are received or sent. The socket timeout is now to maximum total duration of the handshake.

Đã thay đổi trong phiên bản 3.7: Tên máy chủ hoặc địa chỉ IP được OpenSSL khớp trong quá trình bắt tay. Hàm

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
60 không còn được sử dụng. Trong trường hợp OpenSSL từ chối tên máy chủ hoặc địa chỉ IP, cái bắt tay bị hủy bỏ sớm và thông báo cảnh báo TLS được gửi đến ngang hàng.Hostname or IP address is matched by OpenSSL during handshake. The function
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
60 is no longer used. In case OpenSSL refuses a hostname or IP address, the handshake is aborted early and a TLS alert message is send to the peer.

Sslsocket.getpeercert [Binary_form = false] ¶getpeercert[binary_form=False]

Nếu không có chứng chỉ cho người ngang hàng ở đầu kia của kết nối, hãy trả về

ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
4. Nếu cái bắt tay SSL vẫn chưa được thực hiện, hãy tăng
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
62.

Nếu tham số

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
63 là
>>> ssl.OPENSSL_VERSION
'OpenSSL 1.0.2k  26 Jan 2017'
1 và chứng chỉ được nhận từ ngang hàng, phương thức này sẽ trả về một ví dụ
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
65. Nếu chứng chỉ không được xác nhận, Dict là trống. Nếu chứng chỉ được xác nhận, nó sẽ trả về một số phím với một số khóa, trong số đó
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
66 [hiệu trưởng mà chứng chỉ được cấp] và
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
67 [hiệu trưởng cấp chứng chỉ]. Nếu chứng chỉ chứa một thể hiện của tiện ích mở rộng tên thay thế chủ đề [xem RFC 3280], cũng sẽ có khóa
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
68 trong từ điển.RFC 3280], there will also be a
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
68 key in the dictionary.

Các trường

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
66 và
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
67 là các bộ dữ liệu chứa chuỗi các tên phân biệt tương đối [RDN] được đưa ra trong cấu trúc dữ liệu của chứng chỉ cho các trường tương ứng và mỗi RDN là một chuỗi các cặp giá trị tên. Đây là một ví dụ trong thế giới thực:

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
2

Ghi chú

Để xác thực chứng chỉ cho một dịch vụ cụ thể, bạn có thể sử dụng hàm

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
60.

Nếu tham số

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
63 là
>>> ssl.enum_certificates["CA"]
[[b'data...', 'x509_asn', {'1.3.6.1.5.5.7.3.1', '1.3.6.1.5.5.7.3.2'}],
 [b'data...', 'x509_asn', True]]
7 và chứng chỉ được cung cấp, phương thức này sẽ trả về dạng mã hóa der của toàn bộ chứng chỉ dưới dạng chuỗi byte hoặc
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
4 nếu người ngang hàng không cung cấp chứng chỉ. Việc ngang hàng có cung cấp chứng chỉ hay không phụ thuộc vào vai trò của ổ cắm SSL:

  • Đối với ổ cắm SSL của máy khách, máy chủ sẽ luôn cung cấp chứng chỉ, bất kể cần xác thực;

  • Đối với ổ cắm SSL của máy chủ, máy khách sẽ chỉ cung cấp chứng chỉ khi được máy chủ yêu cầu; Do đó,

    context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
    context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        sock.bind[['127.0.0.1', 8443]]
        sock.listen[5]
        with context.wrap_socket[sock, server_side=True] as ssock:
            conn, addr = ssock.accept[]
            ...
    
    0 sẽ trả về
    ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
    ctx.options &= ~ssl.OP_NO_SSLv3
    
    4 nếu bạn đã sử dụng
    import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    77 [thay vì
    Hostname: www.google.com
    [{'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'Google Inc'],],
                 [['commonName', 'Google Internet Authority G2'],]],
      'notAfter': 'Sep 11 11:04:38 2014 GMT',
      'notBefore': 'Sep 11 11:04:38 2013 GMT',
      'serialNumber': '50C71E48BCC50676',
      'subject': [[['countryName', 'US'],],
                  [['stateOrProvinceName', 'California'],],
                  [['localityName', 'Mountain View'],],
                  [['organizationName', 'Google Inc'],],
                  [['commonName', 'www.google.com'],]],
      'subjectAltName': [['DNS', 'www.google.com'],],
      'version': 3},
     {'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'GeoTrust Inc.'],],
                 [['commonName', 'GeoTrust Global CA'],]],
      'notAfter': 'Apr  4 15:15:55 2015 GMT',
      'notBefore': 'Apr  5 15:15:55 2013 GMT',
      'serialNumber': '023A69',
      'subject': [[['countryName', 'US'],],
                  [['organizationName', 'Google Inc'],],
                  [['commonName', 'Google Internet Authority G2'],]],
      'version': 3},
     {'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'Equifax'],],
                 [['organizationalUnitName',
                   'Equifax Secure Certificate Authority'],]],
      'notAfter': 'Aug 21 04:00:00 2018 GMT',
      'notBefore': 'May 21 04:00:00 2002 GMT',
      'serialNumber': '12BBE6',
      'subject': [[['countryName', 'US'],],
                  [['organizationName', 'GeoTrust Inc.'],],
                  [['commonName', 'GeoTrust Global CA'],]],
      'version': 3},
     {'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'Equifax'],],
                 [['organizationalUnitName',
                   'Equifax Secure Certificate Authority'],]],
      'notAfter': 'Aug 22 16:41:51 2018 GMT',
      'notBefore': 'Aug 22 16:41:51 1998 GMT',
      'serialNumber': '35DEF4CF',
      'subject': [[['countryName', 'US'],],
                  [['organizationName', 'Equifax'],],
                  [['organizationalUnitName',
                    'Equifax Secure Certificate Authority'],]],
      'version': 3}]
    
    54 hoặc
    >>> cert = {'subject': [[['commonName', 'example.com'],],]}
    >>> ssl.match_hostname[cert, "example.com"]
    >>> ssl.match_hostname[cert, "example.org"]
    Traceback [most recent call last]:
      File "", line 1, in 
      File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
    ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
    
    1].

Đã thay đổi trong phiên bản 3.2: Từ điển được trả lại bao gồm các mục bổ sung như

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
67 và
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
81.The returned dictionary includes additional items such as
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
67 and
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
81.

Đã thay đổi trong phiên bản 3.4:

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
62 được nâng lên khi bắt tay được thực hiện. Từ điển được trả lại bao gồm các mục mở rộng x509v3 bổ sung như
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
83,
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
84 và
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
85 URI.
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
62 is raised when the handshake isn’t done. The returned dictionary includes additional X509v3 extension items such as
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
83,
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
84 and
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
85 URIs.

Đã thay đổi trong phiên bản 3.9: Chuỗi địa chỉ IPv6 không còn có dòng mới.IPv6 address strings no longer have a trailing new line.

Sslsocket.code []cipher[]

Trả về một tuple ba giá trị chứa tên của mật mã đang được sử dụng, phiên bản của giao thức SSL xác định việc sử dụng nó và số lượng bit bí mật đang được sử dụng. Nếu không có kết nối nào được thiết lập, trả về

ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
4.

Sslsocket.shared_ciphers [] ¶shared_ciphers[]

Trả về danh sách các mật mã được khách hàng chia sẻ trong thời gian bắt tay. Mỗi mục của danh sách trả về là một bộ ba giá trị chứa tên của mật mã, phiên bản của giao thức SSL xác định việc sử dụng nó và số lượng bit bí mật mà mật mã sử dụng.

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
87 Trả về
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
4 Nếu không có kết nối nào được thiết lập hoặc ổ cắm là ổ cắm của máy khách.

Mới trong phiên bản 3.5.

Sslsocket.compression []compression[]

Trả về thuật toán nén đang được sử dụng như một chuỗi hoặc

ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
4 nếu kết nối được nén.

Nếu giao thức cấp cao hơn hỗ trợ cơ chế nén của chính nó, bạn có thể sử dụng

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
90 để vô hiệu hóa nén cấp độ SSL.

Mới trong phiên bản 3.3.

Sslsocket.get_channel_binding [cb_type = 'tls-unique'] ¶get_channel_binding[cb_type='tls-unique']

Nhận dữ liệu liên kết kênh cho kết nối hiện tại, dưới dạng đối tượng byte. Trả về

ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
4 nếu không được kết nối hoặc cái bắt tay chưa được hoàn thành.

Tham số CB_TYPE cho phép lựa chọn loại liên kết kênh mong muốn. Các loại liên kết kênh hợp lệ được liệt kê trong danh sách

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
92. Hiện tại chỉ có liên kết kênh ‘TLS-Unique, được xác định bởi RFC 5929, được hỗ trợ.
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
62 sẽ được nâng lên nếu yêu cầu một loại ràng buộc kênh không được hỗ trợ.RFC 5929, is supported.
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
62 will be raised if an unsupported channel binding type is requested.

Mới trong phiên bản 3.3.

Sslsocket.get_channel_binding [cb_type = 'tls-unique'] ¶selected_alpn_protocol[]

Nhận dữ liệu liên kết kênh cho kết nối hiện tại, dưới dạng đối tượng byte. Trả về

ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
4 nếu không được kết nối hoặc cái bắt tay chưa được hoàn thành.

Mới trong phiên bản 3.5.

Sslsocket.compression []selected_npn_protocol[]

Trả về thuật toán nén đang được sử dụng như một chuỗi hoặc

ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
4 nếu kết nối được nén.

Mới trong phiên bản 3.3.

Sslsocket.get_channel_binding [cb_type = 'tls-unique'] ¶NPN has been superseded by ALPN

Nhận dữ liệu liên kết kênh cho kết nối hiện tại, dưới dạng đối tượng byte. Trả về
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
4 nếu không được kết nối hoặc cái bắt tay chưa được hoàn thành.
unwrap[]

Tham số CB_TYPE cho phép lựa chọn loại liên kết kênh mong muốn. Các loại liên kết kênh hợp lệ được liệt kê trong danh sách

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
92. Hiện tại chỉ có liên kết kênh ‘TLS-Unique, được xác định bởi RFC 5929, được hỗ trợ.
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
62 sẽ được nâng lên nếu yêu cầu một loại ràng buộc kênh không được hỗ trợ.

Sslsocket.verify_client_post_handshake [] ¶verify_client_post_handshake[]

Yêu cầu Xác thực sau tay cầm [PHA] từ máy khách TLS 1.3. PHA chỉ có thể được bắt đầu cho kết nối TLS 1.3 từ ổ cắm phía máy chủ, sau khi bắt tay TLS ban đầu và với PHA được bật ở cả hai bên, xem

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
98.

Phương pháp không thực hiện trao đổi chứng chỉ ngay lập tức. Phía máy chủ gửi một certeraterequest trong sự kiện ghi tiếp theo và mong đợi khách hàng trả lời bằng chứng chỉ trong sự kiện đọc tiếp theo.

Nếu bất kỳ điều kiện tiên quyết nào được đáp ứng [ví dụ: không phải TLS 1.3, PHA không được bật], một

>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43
1 sẽ được nâng lên.

Ghi chú

Chỉ có sẵn với OpenSSL 1.1.1 và TLS 1.3 được bật. Không có hỗ trợ TLS 1.3, phương pháp tăng

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
00.

Mới trong phiên bản 3.8.

SSLSocket.Version []version[]

Trả về phiên bản giao thức SSL thực tế được đàm phán bởi kết nối dưới dạng chuỗi hoặc

ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
4 nếu không có kết nối an toàn được thiết lập. Theo văn bản này, các giá trị hoàn trả có thể bao gồm
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
02,
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
03,
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
04,
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
05 và
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
06. Các phiên bản OpenSSL gần đây có thể xác định nhiều giá trị trả về hơn.

Mới trong phiên bản 3.5.

Sslsocket.pends [] ¶pending[]

Trả về số lượng byte đã được giải mã có sẵn để đọc, đang chờ kết nối.

SSLSocket.Context¶context

Đối tượng

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
5 ổ cắm SSL này được gắn với. Nếu ổ cắm SSL được tạo bằng hàm
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
8 không dùng [chứ không phải
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
3], thì đây là đối tượng bối cảnh tùy chỉnh được tạo cho ổ cắm SSL này.

Mới trong phiên bản 3.2.

Sslsocket.server_side¶server_side

Một boolean là

>>> ssl.enum_certificates["CA"]
[[b'data...', 'x509_asn', {'1.3.6.1.5.5.7.3.1', '1.3.6.1.5.5.7.3.2'}],
 [b'data...', 'x509_asn', True]]
7 cho ổ cắm phía máy chủ và
>>> ssl.OPENSSL_VERSION
'OpenSSL 1.0.2k  26 Jan 2017'
1 cho ổ cắm phía máy khách.

Mới trong phiên bản 3.2.

Sslsocket.server_side¶server_hostname

Một boolean là

>>> ssl.enum_certificates["CA"]
[[b'data...', 'x509_asn', {'1.3.6.1.5.5.7.3.1', '1.3.6.1.5.5.7.3.2'}],
 [b'data...', 'x509_asn', True]]
7 cho ổ cắm phía máy chủ và
>>> ssl.OPENSSL_VERSION
'OpenSSL 1.0.2k  26 Jan 2017'
1 cho ổ cắm phía máy khách.

Mới trong phiên bản 3.2.

Sslsocket.server_side¶The attribute is now always ASCII text. When

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
14 is an internationalized domain name [IDN], this attribute now stores the A-label form [
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
15], rather than the U-label form [
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
16].

Một boolean là
>>> ssl.enum_certificates["CA"]
[[b'data...', 'x509_asn', {'1.3.6.1.5.5.7.3.1', '1.3.6.1.5.5.7.3.2'}],
 [b'data...', 'x509_asn', True]]
7 cho ổ cắm phía máy chủ và
>>> ssl.OPENSSL_VERSION
'OpenSSL 1.0.2k  26 Jan 2017'
1 cho ổ cắm phía máy khách.
session

Sslsocket.server_hostname¶

Tên máy chủ của máy chủ: Loại

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
12 hoặc
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
4 cho ổ cắm phía máy chủ hoặc nếu tên máy chủ không được chỉ định trong hàm tạo.

Đã thay đổi trong phiên bản 3.7: Thuộc tính bây giờ luôn luôn là văn bản ASCII. Khi
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
14 là một tên miền quốc tế hóa [IDN], thuộc tính này hiện lưu trữ biểu mẫu A-Label [
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
15], thay vì dạng nhãn U [
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
16].
session_reused

Tên máy chủ của máy chủ: Loại

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
12 hoặc
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
4 cho ổ cắm phía máy chủ hoặc nếu tên máy chủ không được chỉ định trong hàm tạo.

Đã thay đổi trong phiên bản 3.7: Thuộc tính bây giờ luôn luôn là văn bản ASCII. Khi
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
14 là một tên miền quốc tế hóa [IDN], thuộc tính này hiện lưu trữ biểu mẫu A-Label [
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
15], thay vì dạng nhãn U [
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
16].

Mới trong phiên bản 3.2.

Sslsocket.server_side¶

Một boolean là
>>> ssl.enum_certificates["CA"]
[[b'data...', 'x509_asn', {'1.3.6.1.5.5.7.3.1', '1.3.6.1.5.5.7.3.2'}],
 [b'data...', 'x509_asn', True]]
7 cho ổ cắm phía máy chủ và
>>> ssl.OPENSSL_VERSION
'OpenSSL 1.0.2k  26 Jan 2017'
1 cho ổ cắm phía máy khách.
ssl.SSLContext[protocol=None]

Sslsocket.server_hostname¶

Tên máy chủ của máy chủ: Loại

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
12 hoặc
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
4 cho ổ cắm phía máy chủ hoặc nếu tên máy chủ không được chỉ định trong hàm tạo.

Đã thay đổi trong phiên bản 3.7: Thuộc tính bây giờ luôn luôn là văn bản ASCII. Khi

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
14 là một tên miền quốc tế hóa [IDN], thuộc tính này hiện lưu trữ biểu mẫu A-Label [
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
15], thay vì dạng nhãn U [
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
16].server

SSLv2

SSLv3

Sslsocket.session¶ 3

TLSv1

TLSv1.1

TLSv1.2

SSLv2

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
17 cho kết nối SSL này. Phiên này có sẵn cho các ổ cắm phía máy khách và máy chủ sau khi bắt tay TLS đã được thực hiện. Đối với ổ cắm máy khách, phiên có thể được đặt trước khi
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
18 được gọi để sử dụng lại một phiên.

Mới trong phiên bản 3.6.

Sslsocket.session_reuse¶

Mới trong phiên bản 3.6.

Mới trong phiên bản 3.6.

Mới trong phiên bản 3.6.

SSLv3

Mới trong phiên bản 3.6.

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
17 cho kết nối SSL này. Phiên này có sẵn cho các ổ cắm phía máy khách và máy chủ sau khi bắt tay TLS đã được thực hiện. Đối với ổ cắm máy khách, phiên có thể được đặt trước khi
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
18 được gọi để sử dụng lại một phiên.

Mới trong phiên bản 3.6.

Mới trong phiên bản 3.6.

Mới trong phiên bản 3.6.

Mới trong phiên bản 3.6.

Sslsocket.session_reuse¶

Sslsocket.session_reuse¶

Mới trong phiên bản 3.6.

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
17 cho kết nối SSL này. Phiên này có sẵn cho các ổ cắm phía máy khách và máy chủ sau khi bắt tay TLS đã được thực hiện. Đối với ổ cắm máy khách, phiên có thể được đặt trước khi
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
18 được gọi để sử dụng lại một phiên.

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
17 cho kết nối SSL này. Phiên này có sẵn cho các ổ cắm phía máy khách và máy chủ sau khi bắt tay TLS đã được thực hiện. Đối với ổ cắm máy khách, phiên có thể được đặt trước khi
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
18 được gọi để sử dụng lại một phiên.

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
17 cho kết nối SSL này. Phiên này có sẵn cho các ổ cắm phía máy khách và máy chủ sau khi bắt tay TLS đã được thực hiện. Đối với ổ cắm máy khách, phiên có thể được đặt trước khi
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
18 được gọi để sử dụng lại một phiên.

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
17 cho kết nối SSL này. Phiên này có sẵn cho các ổ cắm phía máy khách và máy chủ sau khi bắt tay TLS đã được thực hiện. Đối với ổ cắm máy khách, phiên có thể được đặt trước khi
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
18 được gọi để sử dụng lại một phiên.

TLSv1

Mới trong phiên bản 3.6.

Mới trong phiên bản 3.6.

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
17 cho kết nối SSL này. Phiên này có sẵn cho các ổ cắm phía máy khách và máy chủ sau khi bắt tay TLS đã được thực hiện. Đối với ổ cắm máy khách, phiên có thể được đặt trước khi
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
18 được gọi để sử dụng lại một phiên.

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
17 cho kết nối SSL này. Phiên này có sẵn cho các ổ cắm phía máy khách và máy chủ sau khi bắt tay TLS đã được thực hiện. Đối với ổ cắm máy khách, phiên có thể được đặt trước khi
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
18 được gọi để sử dụng lại một phiên.

Mới trong phiên bản 3.6.

Mới trong phiên bản 3.6.

TLSv1.1

Mới trong phiên bản 3.6.

Mới trong phiên bản 3.6.

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
17 cho kết nối SSL này. Phiên này có sẵn cho các ổ cắm phía máy khách và máy chủ sau khi bắt tay TLS đã được thực hiện. Đối với ổ cắm máy khách, phiên có thể được đặt trước khi
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
18 được gọi để sử dụng lại một phiên.

Mới trong phiên bản 3.6.

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
17 cho kết nối SSL này. Phiên này có sẵn cho các ổ cắm phía máy khách và máy chủ sau khi bắt tay TLS đã được thực hiện. Đối với ổ cắm máy khách, phiên có thể được đặt trước khi
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
18 được gọi để sử dụng lại một phiên.

Mới trong phiên bản 3.6.

TLSv1.2

Mới trong phiên bản 3.6.

Mới trong phiên bản 3.6.

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
17 cho kết nối SSL này. Phiên này có sẵn cho các ổ cắm phía máy khách và máy chủ sau khi bắt tay TLS đã được thực hiện. Đối với ổ cắm máy khách, phiên có thể được đặt trước khi
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
18 được gọi để sử dụng lại một phiên.

Mới trong phiên bản 3.6.

Mới trong phiên bản 3.6.

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
17 cho kết nối SSL này. Phiên này có sẵn cho các ổ cắm phía máy khách và máy chủ sau khi bắt tay TLS đã được thực hiện. Đối với ổ cắm máy khách, phiên có thể được đặt trước khi
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
18 được gọi để sử dụng lại một phiên.

Mới trong phiên bản 3.6.

1[1,2][1,2]

Sslsocket.session_reuse¶

2[1,2][1,2]

Bối cảnh SSL

3[1,2][1,2]

Bối cảnh SSL chứa các dữ liệu khác nhau tồn tại lâu hơn so với các kết nối SSL đơn, chẳng hạn như tùy chọn cấu hình SSL, chứng chỉ và khóa riêng. Nó cũng quản lý bộ nhớ cache của các phiên SSL cho các ổ cắm phía máy chủ, để tăng tốc các kết nối lặp đi lặp lại từ cùng một máy khách.

classSSSL.SSLContext [giao thức = none] ¶The default cipher suites now include only secure AES and ChaCha20 ciphers with forward secrecy and security level 2. RSA and DH keys with less than 2048 bits and ECC keys with less than 224 bits are prohibited.

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
11,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
5, and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
6 use TLS 1.2 as minimum TLS version.

Tạo bối cảnh SSL mới. Bạn có thể vượt qua giao thức phải là một trong các hằng số

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
19 được xác định trong mô -đun này. Tham số chỉ định phiên bản nào của giao thức SSL để sử dụng. Thông thường, máy chủ chọn một phiên bản giao thức cụ thể và máy khách phải thích ứng với sự lựa chọn của máy chủ. Hầu hết các phiên bản không thể tương tác với các phiên bản khác. Nếu không được chỉ định, mặc định là
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
11; Nó cung cấp khả năng tương thích nhất với các phiên bản khác.

Ở đây, một bảng hiển thị phiên bản nào trong máy khách [phía dưới bên] có thể kết nối với phiên bản nào trong máy chủ [dọc theo đầu]:cert_store_stats[]

Máy khách / Máy chủ

TLS 3

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
3

Vâng

khôngload_cert_chain[certfile, keyfile=None, password=None]

Không 1Certificates for more information on how the certificate is stored in the certfile.

Đối số mật khẩu có thể là một chức năng để gọi để lấy mật khẩu để giải mã khóa riêng. Nó sẽ chỉ được gọi nếu khóa riêng được mã hóa và mật khẩu là cần thiết. Nó sẽ được gọi không có đối số, và nó sẽ trả về một chuỗi, byte hoặc bytearray. Nếu giá trị trả về là một chuỗi, nó sẽ được mã hóa dưới dạng UTF-8 trước khi sử dụng nó để giải mã khóa. Ngoài ra, một chuỗi, byte hoặc giá trị bytearray có thể được cung cấp trực tiếp dưới dạng đối số mật khẩu. Nó sẽ bị bỏ qua nếu khóa riêng không được mã hóa và không cần mật khẩu.

Nếu đối số mật khẩu không được chỉ định và cần có mật khẩu, cơ chế nhắc mật khẩu tích hợp của OpenSSL, sẽ được sử dụng để tương tác nhắc người dùng về mật khẩu.

Một

>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43
1 được nâng lên nếu khóa riêng không phù hợp với chứng chỉ.

Đã thay đổi trong phiên bản 3.3: Mật khẩu đối số tùy chọn mới.New optional argument password.

SslContext.load_default_certs [urimed = purda.server_auth] ¶load_default_certs[purpose=Purpose.SERVER_AUTH]

Tải một bộ chứng chỉ chứng nhận mặc định của người Viking [CA] từ các vị trí mặc định. Trên Windows, nó tải CA Certs từ các cửa hàng hệ thống

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
22 và
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
23. Trên tất cả các hệ thống, nó gọi
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
12. Trong tương lai, phương thức cũng có thể tải chứng chỉ CA từ các vị trí khác.

Cờ mục đích chỉ định loại chứng chỉ CA nào được tải. Các cài đặt mặc định

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
34 Chứng chỉ tải, được gắn cờ và tin cậy cho xác thực máy chủ web TLS [ổ cắm phía máy khách].
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
35 Tải chứng chỉ CA để xác minh chứng chỉ máy khách ở phía máy chủ.

Mới trong phiên bản 3.4.

SslContext.load_verify_locations [CAFILE = none, capath = none, cadata = none] ¶load_verify_locations[cafile=None, capath=None, cadata=None]

Tải một tập hợp các chứng chỉ của Cơ quan chứng nhận [CA] [CA] được sử dụng để xác nhận các chứng chỉ đồng nghiệp khác khi

>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
0 không phải là
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
77. Ít nhất một trong số Cafile hoặc Capath phải được chỉ định.

Phương pháp này cũng có thể tải Danh sách thu hồi chứng nhận [CRLS] ở định dạng PEM hoặc DER. Để sử dụng CRLS,

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
72 phải được cấu hình đúng.

Chuỗi CAFILE, nếu có, là đường dẫn đến một tệp chứng chỉ CA được nối ở định dạng PEM. Xem thảo luận về chứng chỉ để biết thêm thông tin về cách sắp xếp các chứng chỉ trong tệp này.Certificates for more information about how to arrange the certificates in this file.

Chuỗi Capath, nếu có, là đường dẫn đến một thư mục chứa một số chứng chỉ CA ở định dạng PEM, theo bố cục cụ thể của OpenSSL.

Đối tượng CADATA, nếu có, là chuỗi ASCII của một hoặc nhiều chứng chỉ được mã hóa PEM hoặc đối tượng giống như byte của các chứng chỉ được mã hóa DER. Giống như với các dòng Capath thêm xung quanh các chứng chỉ được mã hóa PEM bị bỏ qua nhưng ít nhất một chứng chỉ phải có.bytes-like object of DER-encoded certificates. Like with capath extra lines around PEM-encoded certificates are ignored but at least one certificate must be present.

Thay đổi trong phiên bản 3.4: Cadata đối số tùy chọn mớiNew optional argument cadata

SslContext.get_ca_certs [binary_form = false] ¶get_ca_certs[binary_form=False]

Nhận một danh sách các chứng chỉ chứng nhận được tải trên mạng [CA]. Nếu tham số

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
63 là
>>> ssl.OPENSSL_VERSION
'OpenSSL 1.0.2k  26 Jan 2017'
1, mỗi mục nhập danh sách là một chế độ giống như đầu ra của
>>> ssl.OPENSSL_VERSION
'OpenSSL 1.0.2k  26 Jan 2017'
5. Mặt khác, phương thức trả về một danh sách các chứng chỉ được mã hóa der. Danh sách được trả lại không chứa chứng chỉ từ Capath trừ khi chứng chỉ được yêu cầu và tải bởi kết nối SSL.

Ghi chú

Giấy chứng nhận trong thư mục Capath aren được tải trừ khi chúng được sử dụng ít nhất một lần.

Mới trong phiên bản 3.4.

SslContext.load_verify_locations [CAFILE = none, capath = none, cadata = none] ¶get_ciphers[]

Tải một tập hợp các chứng chỉ của Cơ quan chứng nhận [CA] [CA] được sử dụng để xác nhận các chứng chỉ đồng nghiệp khác khi

>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
0 không phải là
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
77. Ít nhất một trong số Cafile hoặc Capath phải được chỉ định.

Example:

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
4

Phương pháp này cũng có thể tải Danh sách thu hồi chứng nhận [CRLS] ở định dạng PEM hoặc DER. Để sử dụng CRLS,

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
72 phải được cấu hình đúng.

Chuỗi CAFILE, nếu có, là đường dẫn đến một tệp chứng chỉ CA được nối ở định dạng PEM. Xem thảo luận về chứng chỉ để biết thêm thông tin về cách sắp xếp các chứng chỉ trong tệp này.set_default_verify_paths[]

Chuỗi Capath, nếu có, là đường dẫn đến một thư mục chứa một số chứng chỉ CA ở định dạng PEM, theo bố cục cụ thể của OpenSSL.

Đối tượng CADATA, nếu có, là chuỗi ASCII của một hoặc nhiều chứng chỉ được mã hóa PEM hoặc đối tượng giống như byte của các chứng chỉ được mã hóa DER. Giống như với các dòng Capath thêm xung quanh các chứng chỉ được mã hóa PEM bị bỏ qua nhưng ít nhất một chứng chỉ phải có.set_ciphers[ciphers]

Thay đổi trong phiên bản 3.4: Cadata đối số tùy chọn mới

Ghi chú

Giấy chứng nhận trong thư mục Capath aren được tải trừ khi chúng được sử dụng ít nhất một lần.

SslContext.get_ciphers [] ¶

Nhận một danh sách các mật mã được kích hoạt. Danh sách là theo thứ tự ưu tiên mật mã. Xem
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
43.
set_alpn_protocols[protocols]

Mới trong phiên bản 3.6.RFC 7301. After a successful handshake, the

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
47 method will return the agreed-upon protocol.

Phương pháp này sẽ tăng

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
00 nếu
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
49 là
>>> ssl.OPENSSL_VERSION
'OpenSSL 1.0.2k  26 Jan 2017'
1.

Mới trong phiên bản 3.5.

SslContext.set_npn_protocols [giao thức] ¶set_npn_protocols[protocols]

Chỉ định giao thức nào ổ cắm nên quảng cáo trong thời gian bắt tay SSL/TLS. Nó phải là một danh sách các chuỗi, như

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
51, được đặt hàng theo sở thích. Việc lựa chọn một giao thức sẽ xảy ra trong thời gian bắt tay, và sẽ diễn ra theo đàm phán giao thức lớp ứng dụng. Sau khi bắt tay thành công, phương pháp
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
52 sẽ trả về giao thức đã thỏa thuận.

Phương pháp này sẽ tăng

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
00 nếu
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
54 là
>>> ssl.OPENSSL_VERSION
'OpenSSL 1.0.2k  26 Jan 2017'
1.

Mới trong phiên bản 3.3.

Không dùng nữa kể từ phiên bản 3.10: NPN đã được thay thế bởi ALPNNPN has been superseded by ALPN

Sslcontext.sni_callback¶sni_callback

Đăng ký một chức năng gọi lại sẽ được gọi sau khi nhận được thông báo Hello Hello Hello đã nhận được tin nhắn SSL/TLS khi máy khách TLS chỉ định chỉ báo tên máy chủ. Cơ chế chỉ báo tên máy chủ được chỉ định trong RFC 6066 Phần 3 - Chỉ báo tên máy chủ.RFC 6066 section 3 - Server Name Indication.

Chỉ có thể đặt lại một cuộc gọi lại cho mỗi

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
5. Nếu SNI_Callback được đặt thành
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
4 thì cuộc gọi lại bị tắt. Gọi chức năng này một thời gian tiếp theo sẽ vô hiệu hóa cuộc gọi lại đã đăng ký trước đó.

Hàm gọi lại sẽ được gọi với ba đối số; Đầu tiên là

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
8, thứ hai là một chuỗi đại diện cho tên máy chủ mà máy khách đang có ý định giao tiếp [hoặc
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
4 nếu máy khách TLS Hello không chứa tên máy chủ] và đối số thứ ba là
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
5 ban đầu. Đối số tên máy chủ là văn bản. Đối với tên miền quốc tế hóa, tên máy chủ là nhãn A IDN [
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
15].

Một cách sử dụng điển hình của cuộc gọi lại này là thay đổi thuộc tính ____ 38

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
63 thành một đối tượng mới của loại
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
5 đại diện cho chuỗi chứng chỉ phù hợp với tên máy chủ.

Do giai đoạn đàm phán sớm của kết nối TLS, chỉ các phương thức và thuộc tính hạn chế có thể sử dụng được như

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
47 và
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
63. Các phương thức
>>> ssl.OPENSSL_VERSION
'OpenSSL 1.0.2k  26 Jan 2017'
5,
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
44 và
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
69 yêu cầu kết nối TLS đã tiến triển ngoài ứng dụng khách TLS Hello và do đó sẽ không trả về các giá trị có ý nghĩa cũng như không thể được gọi là an toàn.

Hàm SNI_Callback phải trả về

ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
4 để cho phép đàm phán TLS tiếp tục. Nếu yêu cầu lỗi TLS, có thể trả về hằng số
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
71. Các giá trị trả về khác sẽ dẫn đến lỗi TLS gây tử vong với
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
72.

Nếu một ngoại lệ được nâng lên từ hàm SNI_Callback, kết nối TLS sẽ chấm dứt với thông báo cảnh báo TLS gây tử vong

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
73.

Phương pháp này sẽ tăng

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
00 nếu thư viện OpenSSL có openSSL_NO_TLSEXT được xác định khi nó được xây dựng.

Mới trong phiên bản 3.7.

SslContext.set_servername_callback [server_name_callback] ¶set_servername_callback[server_name_callback]

Đây là một API kế thừa được giữ lại để tương thích ngược. Khi có thể, bạn nên sử dụng

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
75 thay thế. Server_name_callback đã cho tương tự như SNI_Callback, ngoại trừ khi tên máy chủ máy chủ là tên miền quốc tế hóa được mã hóa IDN, server_name_callback nhận được nhãn U được giải mã [
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
16].

Nếu có lỗi giải mã trên tên máy chủ, kết nối TLS sẽ chấm dứt với thông báo cảnh báo TLS

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
72 Fatal TLS cho máy khách.

Mới trong phiên bản 3.4.

SslContext.load_dh_params [dhfile] ¶load_dh_params[dhfile]

Tải các tham số tạo khóa cho trao đổi khóa Diffie-Hellman [DH]. Sử dụng DH Key Exchange cải thiện bí mật về phía trước với chi phí tài nguyên tính toán [cả trên máy chủ và trên máy khách]. Tham số DHFILE phải là đường dẫn đến một tệp chứa các tham số DH ở định dạng PEM.

Cài đặt này không áp dụng cho ổ cắm khách hàng. Bạn cũng có thể sử dụng tùy chọn

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
78 để cải thiện bảo mật hơn nữa.

Mới trong phiên bản 3.3.

Không dùng nữa kể từ phiên bản 3.10: NPN đã được thay thế bởi ALPNset_ecdh_curve[curve_name]

Sslcontext.sni_callback¶

Đăng ký một chức năng gọi lại sẽ được gọi sau khi nhận được thông báo Hello Hello Hello đã nhận được tin nhắn SSL/TLS khi máy khách TLS chỉ định chỉ báo tên máy chủ. Cơ chế chỉ báo tên máy chủ được chỉ định trong RFC 6066 Phần 3 - Chỉ báo tên máy chủ.

Chỉ có thể đặt lại một cuộc gọi lại cho mỗi

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
5. Nếu SNI_Callback được đặt thành
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
4 thì cuộc gọi lại bị tắt. Gọi chức năng này một thời gian tiếp theo sẽ vô hiệu hóa cuộc gọi lại đã đăng ký trước đó.

Mới trong phiên bản 3.3.

Không dùng nữa kể từ phiên bản 3.10: NPN đã được thay thế bởi ALPNwrap_socket[sock, server_side=False, do_handshake_on_connect=True, suppress_ragged_eofs=True, server_hostname=None, session=None]

Sslcontext.sni_callback¶

Tham số

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
86 là Boolean xác định xem hành vi phía máy chủ hay phía máy khách có mong muốn từ ổ cắm này hay không.

Đối với ổ cắm phía khách hàng, việc xây dựng bối cảnh là lười biếng; Nếu ổ cắm cơ bản được kết nối, việc xây dựng bối cảnh sẽ được thực hiện sau khi

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
13 được gọi trên ổ cắm. Đối với ổ cắm phía máy chủ, nếu ổ cắm không có ngang hàng từ xa, nó được coi là ổ cắm nghe và gói SSL phía máy chủ được tự động thực hiện trên các kết nối máy khách được chấp nhận thông qua phương thức
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
10. Phương pháp có thể tăng
>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds["Jan  5 09:34:43 2018 GMT"]
>>> timestamp  
1515144883
>>> from datetime import datetime
>>> print[datetime.utcfromtimestamp[timestamp]]  
2018-01-05 09:34:43
1.

Trên các kết nối máy khách, tham số tùy chọn server_hostname chỉ định tên máy chủ của dịch vụ mà chúng tôi đang kết nối. Điều này cho phép một máy chủ duy nhất lưu trữ nhiều dịch vụ dựa trên SSL với các chứng chỉ riêng biệt, khá giống với máy chủ ảo HTTP. Chỉ định server_hostname sẽ tăng

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
62 nếu server_side là đúng.

Tham số

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
91 chỉ định có tự động thực hiện bắt tay SSL sau khi thực hiện
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
92 hay liệu chương trình ứng dụng sẽ gọi nó một cách rõ ràng, bằng cách gọi phương thức
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
93. Gọi
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
93 rõ ràng cung cấp cho chương trình kiểm soát hành vi chặn của ổ cắm I/O liên quan đến bắt tay.

Tham số

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
95 chỉ định cách phương thức
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
96 sẽ báo hiệu EOF bất ngờ từ đầu kia của kết nối. Nếu được chỉ định là
>>> ssl.enum_certificates["CA"]
[[b'data...', 'x509_asn', {'1.3.6.1.5.5.7.3.1', '1.3.6.1.5.5.7.3.2'}],
 [b'data...', 'x509_asn', True]]
7 [mặc định], nó sẽ trả về một EOF bình thường [một đối tượng Byte trống] để đáp ứng với các lỗi EOF bất ngờ được nêu từ ổ cắm bên dưới; Nếu
>>> ssl.OPENSSL_VERSION
'OpenSSL 1.0.2k  26 Jan 2017'
1, nó sẽ nâng các ngoại lệ trở lại cho người gọi.

Phiên, xem

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
99.

Đã thay đổi trong phiên bản 3.5: Luôn cho phép server_hostname được truyền, ngay cả khi OpenSSL không có SNI.Always allow a server_hostname to be passed, even if OpenSSL does not have SNI.

Thay đổi trong phiên bản 3.6: Đối số phiên đã được thêm vào.session argument was added.

SslContext.sslsocket_class¶sslsocket_class

Loại trả về của

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
3, mặc định là
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
6. Thuộc tính có thể được ghi đè trên ví dụ của lớp để trả về một lớp con tùy chỉnh của
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
6.

Mới trong phiên bản 3.7.

SslContext.wrap_bio [đến, gửi đi, server_side = false, server_hostname = none, session = none] ¶wrap_bio[incoming, outgoing, server_side=False, server_hostname=None, session=None]

Bao bọc các đối tượng sinh học đến và gửi đi và trả về một thể hiện là

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
03 [mặc định
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
04]. Các thói quen SSL sẽ đọc dữ liệu đầu vào từ sinh học đến và ghi dữ liệu vào tiểu sử đi.

Các tham số server_side, server_hostname và phiên có cùng ý nghĩa như trong

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
3.

Thay đổi trong phiên bản 3.6: Đối số phiên đã được thêm vào.session argument was added.

SslContext.sslsocket_class¶sslobject_class

Loại trả về của

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
3, mặc định là
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
6. Thuộc tính có thể được ghi đè trên ví dụ của lớp để trả về một lớp con tùy chỉnh của
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
6.

Mới trong phiên bản 3.7.

SslContext.wrap_bio [đến, gửi đi, server_side = false, server_hostname = none, session = none] ¶session_stats[]

Bao bọc các đối tượng sinh học đến và gửi đi và trả về một thể hiện là

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
03 [mặc định
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
04]. Các thói quen SSL sẽ đọc dữ liệu đầu vào từ sinh học đến và ghi dữ liệu vào tiểu sử đi.

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
5

Các tham số server_side, server_hostname và phiên có cùng ý nghĩa như trong
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
3.
check_hostname

SslContext.sslobject_class¶

Example:

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
6

Loại trả về của

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
06, mặc định là
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
04. Thuộc tính có thể được ghi đè trên ví dụ của lớp để trả về một lớp con tùy chỉnh của
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
04.

SslContext.session_stats [] ¶keylog_filename

Nhận số liệu thống kê về các phiên SSL được tạo hoặc quản lý bởi bối cảnh này. Một từ điển được trả về, ánh xạ tên của từng phần thông tin theo giá trị số của chúng. Ví dụ: đây là tổng số lượt truy cập và bỏ lỡ trong bộ đệm phiên vì ngữ cảnh được tạo:

SslContext.check_hostname¶

Có phù hợp với tên máy chủ Peer Cert trong
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
93. Bối cảnh từ ngữ ____ ____60 phải được đặt thành
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
54 hoặc
>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
1 và bạn phải chuyển server_hostname sang
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
8 để khớp tên máy chủ. Kích hoạt kiểm tra tên máy chủ tự động đặt
>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
0 từ
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
77 đến
>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
1. Nó không thể được đặt trở lại
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
77 miễn là kiểm tra tên máy chủ được bật. Giao thức
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
5 cho phép kiểm tra tên máy chủ theo mặc định. Với các giao thức khác, kiểm tra tên máy chủ phải được bật rõ ràng.
maximum_version

Mới trong phiên bản 3.4.

SSLContext.Keylog_Filename¶

Mới trong phiên bản 3.7.

SslContext.wrap_bio [đến, gửi đi, server_side = false, server_hostname = none, session = none] ¶minimum_version

Bao bọc các đối tượng sinh học đến và gửi đi và trả về một thể hiện là

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
03 [mặc định
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
04]. Các thói quen SSL sẽ đọc dữ liệu đầu vào từ sinh học đến và ghi dữ liệu vào tiểu sử đi.

Mới trong phiên bản 3.7.

SslContext.num_tickets¶num_tickets

Kiểm soát số lượng vé phiên TLS 1.3 của bối cảnh

ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
6. Cài đặt không có tác động đến các kết nối TLS 1.0 đến 1.2.

Mới trong phiên bản 3.8.

SSLContext.Options¶options

Một số nguyên đại diện cho tập hợp các tùy chọn SSL được bật trên ngữ cảnh này. Giá trị mặc định là

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
34, nhưng bạn có thể chỉ định các tùy chọn khác như
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
7 bằng cách kết hợp chúng lại với nhau.

Đã thay đổi trong phiên bản 3.6:

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
40 Trả về cờ
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
37:
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
40 returns
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
37 flags:

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
7

SSLContext.Post_Handshake_Auth¶¶post_handshake_auth

Bật xác thực máy khách TLS 1.3 Handshake. Post-Handshake Auth bị vô hiệu hóa theo mặc định và máy chủ chỉ có thể yêu cầu chứng chỉ máy khách TLS trong thời gian bắt tay ban đầu. Khi được bật, máy chủ có thể yêu cầu chứng chỉ máy khách TLS bất cứ lúc nào sau khi bắt tay.

Khi được bật trên ổ cắm phía máy khách, máy khách báo hiệu máy chủ hỗ trợ xác thực sau tay cầm.

Khi được bật trên ổ cắm phía máy chủ,

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
47 cũng phải được đặt thành
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
54 hoặc
>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
1. Trao đổi chứng nhận khách hàng thực tế bị trì hoãn cho đến khi
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
41 được gọi và một số I/O được thực hiện.

Mới trong phiên bản 3.8.

SSLContext.Options¶protocol

Một số nguyên đại diện cho tập hợp các tùy chọn SSL được bật trên ngữ cảnh này. Giá trị mặc định là

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
34, nhưng bạn có thể chỉ định các tùy chọn khác như
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
7 bằng cách kết hợp chúng lại với nhau.

Đã thay đổi trong phiên bản 3.6:
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
40 Trả về cờ
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
37:
hostname_checks_common_name

SSLContext.Post_Handshake_Auth¶¶

Mới trong phiên bản 3.7.

Bật xác thực máy khách TLS 1.3 Handshake. Post-Handshake Auth bị vô hiệu hóa theo mặc định và máy chủ chỉ có thể yêu cầu chứng chỉ máy khách TLS trong thời gian bắt tay ban đầu. Khi được bật, máy chủ có thể yêu cầu chứng chỉ máy khách TLS bất cứ lúc nào sau khi bắt tay.The flag had no effect with OpenSSL before version 1.1.1k. Python 3.8.9, 3.9.3, and 3.10 include workarounds for previous versions.

Khi được bật trên ổ cắm phía máy khách, máy khách báo hiệu máy chủ hỗ trợ xác thực sau tay cầm.security_level

Khi được bật trên ổ cắm phía máy chủ,

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
47 cũng phải được đặt thành
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
54 hoặc
>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
1. Trao đổi chứng nhận khách hàng thực tế bị trì hoãn cho đến khi
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
41 được gọi và một số I/O được thực hiện.

SslContext.protocol¶

Phiên bản giao thức được chọn khi xây dựng bối cảnh. Thuộc tính này chỉ đọc.verify_flags

SslContext.hostname_checks_common_name¶

Liệu

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
64 có trở lại để xác minh tên chung của CERT CERT trong trường hợp không có phần mở rộng tên thay thế chủ đề [mặc định: true].

Đã thay đổi trong phiên bản 3.10: Cờ không có tác dụng với OpenSSL trước phiên bản 1.1.1k. Python 3.8.9, 3.9.3 và 3.10 bao gồm các cách giải quyết cho các phiên bản trước.verify_mode

SslContext.security_level¶

Một số nguyên đại diện cho cấp độ bảo mật cho bối cảnh. Thuộc tính này chỉ đọc.

Mới trong phiên bản 3.10.only with the other part.

SslContext.verify_flags¶

Các cờ cho các hoạt động xác minh chứng chỉ. Bạn có thể đặt cờ như

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
43 bằng cách kết hợp chúng lại với nhau. Theo mặc định, OpenSSL không yêu cầu cũng không xác minh danh sách thu hồi chứng chỉ [CRLS].

Mới trong phiên bản 3.4.RFC 1422], which is a base-64 encoded form wrapped with a header line and a footer line:

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
8

SslContext.verify_mode¶

Có nên cố gắng xác minh các chứng chỉ đồng nghiệp khác và cách cư xử nếu xác minh không thành công. Thuộc tính này phải là một trong

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
77,
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
54 hoặc
>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
1.

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
9

CA chứng chỉ

Nếu bạn sẽ yêu cầu xác thực phía bên kia của chứng chỉ kết nối, bạn cần cung cấp tệp CA CA Certs, chứa đầy chuỗi chứng chỉ cho mỗi công ty phát hành mà bạn sẵn sàng tin tưởng. Một lần nữa, tập tin này chỉ chứa các chuỗi này được nối với nhau. Để xác thực, Python sẽ sử dụng chuỗi đầu tiên mà nó tìm thấy trong tệp phù hợp. Tệp chứng chỉ nền tảng có thể được sử dụng bằng cách gọi

>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
2, điều này được thực hiện tự động bằng
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
7.

Khóa kết hợp và chứng chỉ

Thông thường khóa riêng được lưu trữ trong cùng một tệp với chứng chỉ; Trong trường hợp này, chỉ có tham số

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
49 thành
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
41 và
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
8 cần được thông qua. Nếu khóa riêng được lưu trữ bằng chứng chỉ, nó sẽ đến trước chứng chỉ đầu tiên trong chuỗi chứng chỉ:

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
0

Giấy chứng nhận tự ký công

Nếu bạn định tạo một máy chủ cung cấp dịch vụ kết nối được mã hóa SSL, bạn sẽ cần phải có chứng chỉ cho dịch vụ đó. Có nhiều cách để có được chứng chỉ phù hợp, chẳng hạn như mua một từ cơ quan chứng nhận. Một thực tế phổ biến khác là tạo chứng chỉ tự ký. Cách đơn giản nhất để làm điều này là với gói OpenSSL, sử dụng một cái gì đó như sau:

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
1

Nhược điểm của chứng chỉ tự ký là đó là chứng chỉ gốc của riêng nó và không ai khác sẽ có nó trong bộ nhớ cache của các chứng chỉ gốc đã biết [và đáng tin cậy].

Ví dụ;

Kiểm tra hỗ trợ SSL

Để kiểm tra sự hiện diện của hỗ trợ SSL trong cài đặt Python, mã người dùng nên sử dụng thành ngữ sau:

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
2

Hoạt động phía máy khách

Ví dụ này tạo ra bối cảnh SSL với các cài đặt bảo mật được đề xuất cho ổ cắm máy khách, bao gồm xác minh chứng chỉ tự động:

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
3

Nếu bạn thích tự điều chỉnh cài đặt bảo mật, bạn có thể tạo ngữ cảnh từ đầu [nhưng hãy cẩn thận rằng bạn có thể không nhận được cài đặt đúng]:

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
4

.

Giao thức

ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
5 định cấu hình ngữ cảnh để xác thực CERT và xác minh tên máy chủ.
>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
0 được đặt thành
>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
1 và
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
64 được đặt thành
>>> ssl.enum_certificates["CA"]
[[b'data...', 'x509_asn', {'1.3.6.1.5.5.7.3.1', '1.3.6.1.5.5.7.3.2'}],
 [b'data...', 'x509_asn', True]]
7. Tất cả các giao thức khác tạo ra bối cảnh SSL với mặc định không an toàn.

Khi bạn sử dụng bối cảnh để kết nối với máy chủ,

>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
1 và
Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
64 xác thực chứng chỉ máy chủ: nó đảm bảo rằng chứng chỉ máy chủ đã được ký với một trong các chứng chỉ CA, kiểm tra chữ ký cho tính chính xác và xác minh các thuộc tính khác như tính hợp lệ và nhận dạng của Tên máy chủ:

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
5

Sau đó, bạn có thể lấy chứng chỉ:

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
6

Kiểm tra trực quan cho thấy chứng chỉ xác định dịch vụ mong muốn [nghĩa là máy chủ HTTPS

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
60]:

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
7

Bây giờ kênh SSL đã được thiết lập và chứng chỉ được xác minh, bạn có thể tiến hành nói chuyện với máy chủ:

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
8

Xem các cuộc thảo luận về các cân nhắc bảo mật dưới đây.Security considerations below.

Hoạt động phía máy chủ

Đối với hoạt động của máy chủ, thông thường, bạn sẽ cần phải có chứng chỉ máy chủ và khóa riêng, mỗi trong một tệp. Trước tiên, bạn sẽ tạo một bối cảnh giữ khóa và chứng chỉ, để khách hàng có thể kiểm tra tính xác thực của bạn. Sau đó, bạn sẽ mở một ổ cắm, liên kết nó với một cổng, gọi

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
23 trên đó và bắt đầu chờ khách hàng kết nối:

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
9

Khi khách hàng kết nối, bạn sẽ gọi

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
10 trên ổ cắm để lấy ổ cắm mới từ đầu kia và sử dụng phương thức bối cảnh
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
3 để tạo ổ cắm SSL phía máy chủ cho kết nối:

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
0

Sau đó, bạn sẽ đọc dữ liệu từ

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
64 và làm điều gì đó với nó cho đến khi bạn kết thúc với máy khách [hoặc khách hàng đã kết thúc với bạn]:

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
1

Và quay lại để lắng nghe các kết nối máy khách mới [tất nhiên, một máy chủ thực có thể sẽ xử lý từng kết nối máy khách trong một luồng riêng biệt hoặc đặt các ổ cắm ở chế độ không chặn và sử dụng vòng lặp sự kiện].non-blocking mode and use an event loop].

Ghi chú về ổ cắm không chặn

Các ổ cắm SSL hoạt động hơi khác so với ổ cắm thông thường ở chế độ không chặn. Khi làm việc với các ổ cắm không chặn, do đó có một số điều bạn cần nhận thức được:

  • Hầu hết các phương thức

    context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
    context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        sock.bind[['127.0.0.1', 8443]]
        sock.listen[5]
        with context.wrap_socket[sock, server_side=True] as ssock:
            conn, addr = ssock.accept[]
            ...
    
    6 sẽ tăng
    import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    46 hoặc
    import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    45 thay vì
    context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
    context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        sock.bind[['127.0.0.1', 8443]]
        sock.listen[5]
        with context.wrap_socket[sock, server_side=True] as ssock:
            conn, addr = ssock.accept[]
            ...
    
    68 nếu hoạt động I/O sẽ chặn.
    import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    45 sẽ được nâng lên nếu một hoạt động đọc trên ổ cắm bên dưới là cần thiết và
    import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    46 cho một thao tác ghi trên ổ cắm bên dưới. Lưu ý rằng các nỗ lực ghi vào ổ cắm SSL có thể yêu cầu đọc từ ổ cắm bên dưới và cố gắng đọc từ ổ cắm SSL có thể yêu cầu ghi trước cho ổ cắm bên dưới.

    Đã thay đổi trong phiên bản 3.5: Trong các phiên bản Python trước đó, phương thức

    context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
    context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        sock.bind[['127.0.0.1', 8443]]
        sock.listen[5]
        with context.wrap_socket[sock, server_side=True] as ssock:
            conn, addr = ssock.accept[]
            ...
    
    71 đã trả về 0 thay vì tăng
    import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    46 hoặc
    import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    45.In earlier Python versions, the
    context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
    context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        sock.bind[['127.0.0.1', 8443]]
        sock.listen[5]
        with context.wrap_socket[sock, server_side=True] as ssock:
            conn, addr = ssock.accept[]
            ...
    
    71 method returned zero instead of raising
    import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    46 or
    import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    45.

  • Gọi

    context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
    context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        sock.bind[['127.0.0.1', 8443]]
        sock.listen[5]
        with context.wrap_socket[sock, server_side=True] as ssock:
            conn, addr = ssock.accept[]
            ...
    
    74 cho bạn biết rằng ổ cắm cấp hệ điều hành có thể được đọc từ [hoặc viết thành], nhưng nó không ngụ ý rằng có đủ dữ liệu ở lớp SSL trên. Ví dụ, chỉ một phần của khung SSL mới có thể đến. Do đó, bạn phải sẵn sàng xử lý các thất bại
    hostname = 'www.python.org'
    # PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
    context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
    context.load_verify_locations['path/to/cabundle.pem']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    96 và
    context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
    context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        sock.bind[['127.0.0.1', 8443]]
        sock.listen[5]
        with context.wrap_socket[sock, server_side=True] as ssock:
            conn, addr = ssock.accept[]
            ...
    
    71 và thử lại sau một cuộc gọi khác đến
    context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
    context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        sock.bind[['127.0.0.1', 8443]]
        sock.listen[5]
        with context.wrap_socket[sock, server_side=True] as ssock:
            conn, addr = ssock.accept[]
            ...
    
    74.

  • Ngược lại, vì lớp SSL có khung riêng, một ổ cắm SSL vẫn có thể có sẵn dữ liệu để đọc mà không cần

    context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
    context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        sock.bind[['127.0.0.1', 8443]]
        sock.listen[5]
        with context.wrap_socket[sock, server_side=True] as ssock:
            conn, addr = ssock.accept[]
            ...
    
    74 biết về nó. Do đó, trước tiên bạn nên gọi
    hostname = 'www.python.org'
    # PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
    context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
    context.load_verify_locations['path/to/cabundle.pem']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    96 để thoát bất kỳ dữ liệu có khả năng nào và sau đó chỉ chặn trong cuộc gọi
    context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
    context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        sock.bind[['127.0.0.1', 8443]]
        sock.listen[5]
        with context.wrap_socket[sock, server_side=True] as ssock:
            conn, addr = ssock.accept[]
            ...
    
    74 nếu vẫn cần thiết.

    [Tất nhiên, các điều khoản tương tự được áp dụng khi sử dụng các nguyên thủy khác như

    context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
    context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        sock.bind[['127.0.0.1', 8443]]
        sock.listen[5]
        with context.wrap_socket[sock, server_side=True] as ssock:
            conn, addr = ssock.accept[]
            ...
    
    81 hoặc các điều khoản trong mô -đun
    context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
    context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        sock.bind[['127.0.0.1', 8443]]
        sock.listen[5]
        with context.wrap_socket[sock, server_side=True] as ssock:
            conn, addr = ssock.accept[]
            ...
    
    82]

  • Bản thân SSL sẽ không chặn: Phương pháp

    hostname = 'www.python.org'
    # PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
    context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
    context.load_verify_locations['path/to/cabundle.pem']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    93 phải được thử lại cho đến khi nó trả về thành công. Dưới đây là bản tóm tắt sử dụng
    context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
    context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        sock.bind[['127.0.0.1', 8443]]
        sock.listen[5]
        with context.wrap_socket[sock, server_side=True] as ssock:
            conn, addr = ssock.accept[]
            ...
    
    74 để chờ đợi sự sẵn sàng của ổ cắm:

    hostname = 'www.python.org'
    # PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
    context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
    context.load_verify_locations['path/to/cabundle.pem']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    2

Hỗ trợ sinh học bộ nhớ

Mới trong phiên bản 3.5.

Kể từ khi mô -đun SSL được giới thiệu trong Python 2.6, lớp

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
6 đã cung cấp hai lĩnh vực chức năng liên quan nhưng khác biệt:

  • Xử lý giao thức SSL

  • Mạng IO

API IO mạng giống hệt với quy mô được cung cấp bởi

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
9, từ đó
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
6 cũng kế thừa. Điều này cho phép sử dụng ổ cắm SSL làm thay thế cho ổ cắm thông thường, giúp việc thêm hỗ trợ SSL rất dễ dàng vào một ứng dụng hiện có.

Kết hợp xử lý giao thức SSL và IO mạng thường hoạt động tốt, nhưng có một số trường hợp nó không có. Một ví dụ là các khung IO Async muốn sử dụng mô hình ghép kênh IO khác với mô hình mô tả tệp [dựa trên] mô tả tệp [dựa trên tính sẵn sàng] được giả định bởi

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
9 và bởi các thói quen IO của OpenSSL OpenSSL. Điều này chủ yếu phù hợp cho các nền tảng như Windows nơi mô hình này không hiệu quả. Với mục đích này, một biến thể phạm vi giảm của
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
6 được gọi là
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
04 được cung cấp.

classSSSL.SSLOBject¶ ssl.SSLObject

Một biến thể phạm vi giảm của

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
6 đại diện cho một thể hiện giao thức SSL không chứa bất kỳ phương thức IO mạng nào. Lớp này thường được sử dụng bởi các tác giả khung muốn thực hiện IO không đồng bộ cho SSL thông qua bộ đệm bộ nhớ.

Lớp này thực hiện một giao diện trên đỉnh của một đối tượng SSL cấp thấp được thực hiện bởi OpenSSL. Đối tượng này nắm bắt trạng thái của kết nối SSL nhưng không cung cấp bất kỳ IO mạng nào. IO cần được thực hiện thông qua các đối tượng Bio Bio riêng biệt là lớp trừu tượng IO OpenSSL.

Lớp học này không có nhà xây dựng công cộng. Một thể hiện

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
04 phải được tạo bằng phương pháp
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
93. Phương pháp này sẽ tạo phiên bản
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
04 và liên kết nó với một cặp BIOS. Sinh học đến được sử dụng để truyền dữ liệu từ Python sang thể hiện giao thức SSL, trong khi tiểu sử đi được sử dụng để truyền dữ liệu theo cách khác.

Các phương pháp sau đây có sẵn:

  • context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
    context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        sock.bind[['127.0.0.1', 8443]]
        sock.listen[5]
        with context.wrap_socket[sock, server_side=True] as ssock:
            conn, addr = ssock.accept[]
            ...
    
    95

  • hostname = 'www.python.org'
    # PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
    context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
    context.load_verify_locations['path/to/cabundle.pem']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    86

  • hostname = 'www.python.org'
    # PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
    context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
    context.load_verify_locations['path/to/cabundle.pem']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    14

  • hostname = 'www.python.org'
    # PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
    context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
    context.load_verify_locations['path/to/cabundle.pem']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    99

  • context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
    context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        sock.bind[['127.0.0.1', 8443]]
        sock.listen[5]
        with context.wrap_socket[sock, server_side=True] as ssock:
            conn, addr = ssock.accept[]
            ...
    
    99

  • import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    47

  • import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    52

  • context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
    context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        sock.bind[['127.0.0.1', 8443]]
        sock.listen[5]
        with context.wrap_socket[sock, server_side=True] as ssock:
            conn, addr = ssock.accept[]
            ...
    
    0

  • ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
    ctx.options &= ~ssl.OP_NO_SSLv3
    
    03

  • ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
    ctx.options &= ~ssl.OP_NO_SSLv3
    
    04

  • context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
    context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        sock.bind[['127.0.0.1', 8443]]
        sock.listen[5]
        with context.wrap_socket[sock, server_side=True] as ssock:
            conn, addr = ssock.accept[]
            ...
    
    1

  • import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    87

  • ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
    ctx.options &= ~ssl.OP_NO_SSLv3
    
    07

  • ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
    ctx.options &= ~ssl.OP_NO_SSLv3
    
    08

  • hostname = 'www.python.org'
    # PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
    context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
    context.load_verify_locations['path/to/cabundle.pem']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    18

  • ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
    ctx.options &= ~ssl.OP_NO_SSLv3
    
    10

  • ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
    ctx.options &= ~ssl.OP_NO_SSLv3
    
    11

  • ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
    ctx.options &= ~ssl.OP_NO_SSLv3
    
    12

  • ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
    ctx.options &= ~ssl.OP_NO_SSLv3
    
    13

Khi so sánh với

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
6, đối tượng này thiếu các tính năng sau:

  • Bất kỳ hình thức mạng IO;

    import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    25 và
    import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    28 Đọc và chỉ ghi cho bộ đệm
    ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
    ctx.options &= ~ssl.OP_NO_SSLv3
    
    17 bên dưới.

  • Không có máy móc DO_HANDSHAKE_ON_CONNECT. Bạn phải luôn gọi thủ công

    hostname = 'www.python.org'
    # PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
    context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
    context.load_verify_locations['path/to/cabundle.pem']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    18 để bắt đầu bắt tay.

  • Không có cách xử lý của esspress_ragged_eofs. Tất cả các điều kiện cuối tệp đang vi phạm giao thức đều được báo cáo thông qua ngoại lệ

    ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
    ctx.options &= ~ssl.OP_NO_SSLv3
    
    19.

  • Cuộc gọi phương thức

    ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
    ctx.options &= ~ssl.OP_NO_SSLv3
    
    11 không trả về bất cứ điều gì, không giống như một ổ cắm SSL nơi nó trả về ổ cắm bên dưới.

  • Referback server_name_callback được chuyển đến

    import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    00 sẽ nhận được một thể hiện
    context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
    context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        sock.bind[['127.0.0.1', 8443]]
        sock.listen[5]
        with context.wrap_socket[sock, server_side=True] as ssock:
            conn, addr = ssock.accept[]
            ...
    
    04 thay vì một thể hiện
    context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
    context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        sock.bind[['127.0.0.1', 8443]]
        sock.listen[5]
        with context.wrap_socket[sock, server_side=True] as ssock:
            conn, addr = ssock.accept[]
            ...
    
    6 làm tham số đầu tiên của nó.

Một số ghi chú liên quan đến việc sử dụng

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
04:

  • Tất cả IO trên

    context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
    context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        sock.bind[['127.0.0.1', 8443]]
        sock.listen[5]
        with context.wrap_socket[sock, server_side=True] as ssock:
            conn, addr = ssock.accept[]
            ...
    
    04 là không chặn. Điều này có nghĩa là ví dụ
    import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    47 sẽ tăng
    import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    45 nếu nó cần nhiều dữ liệu hơn so với sinh học đến có sẵn.non-blocking. This means that for example
    import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    47 will raise an
    import socket
    import ssl
    
    hostname = 'www.python.org'
    context = ssl.create_default_context[]
    
    with socket.create_connection[[hostname, 443]] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    45 if it needs more data than the incoming BIO has available.

  • Không có cuộc gọi cấp độ mô-đun

    context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
    context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        sock.bind[['127.0.0.1', 8443]]
        sock.listen[5]
        with context.wrap_socket[sock, server_side=True] as ssock:
            conn, addr = ssock.accept[]
            ...
    
    93 như có cho
    context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
    context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        sock.bind[['127.0.0.1', 8443]]
        sock.listen[5]
        with context.wrap_socket[sock, server_side=True] as ssock:
            conn, addr = ssock.accept[]
            ...
    
    8. Một
    context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
    context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        sock.bind[['127.0.0.1', 8443]]
        sock.listen[5]
        with context.wrap_socket[sock, server_side=True] as ssock:
            conn, addr = ssock.accept[]
            ...
    
    04 luôn được tạo thông qua
    context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
    context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        sock.bind[['127.0.0.1', 8443]]
        sock.listen[5]
        with context.wrap_socket[sock, server_side=True] as ssock:
            conn, addr = ssock.accept[]
            ...
    
    5.

Đã thay đổi trong phiên bản 3.7:

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
04 Các phiên bản phải được tạo bằng
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
93. Trong các phiên bản trước, có thể tạo các trường hợp trực tiếp. Điều này chưa bao giờ được ghi nhận hoặc chính thức được hỗ trợ.
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
04 instances must to created with
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
93. In earlier versions, it was possible to create instances directly. This was never documented or officially supported.

Một SSLOBject giao tiếp với thế giới bên ngoài bằng cách sử dụng bộ đệm bộ nhớ. Lớp

ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
17 cung cấp bộ đệm bộ nhớ có thể được sử dụng cho mục đích này. Nó kết thúc một đối tượng Bio bộ nhớ OpenSSL [IO cơ bản]:

ClassSSSL.Memorybio¶ssl.MemoryBIO

Bộ đệm bộ nhớ có thể được sử dụng để truyền dữ liệu giữa Python và phiên bản giao thức SSL.

chưa giải quyết¶

Trả về số byte hiện tại trong bộ đệm bộ nhớ.

eof¶

Một boolean cho biết liệu sinh học bộ nhớ có dòng điện ở vị trí cuối tập tin hay không.

Đọc [n = -1] ¶[n=- 1]

Đọc tối đa n byte từ bộ đệm bộ nhớ. Nếu N không được chỉ định hoặc âm, tất cả các byte được trả về.

Viết [BUF] ¶[buf]

Viết các byte từ BUF vào tiểu sử bộ nhớ. Đối số BUF phải là một đối tượng hỗ trợ giao thức bộ đệm.

Giá trị trả về là số byte được viết, luôn bằng chiều dài của BUF.

write_eof [] ¶[]

Viết một điểm đánh dấu EOF vào tiểu sử bộ nhớ. Sau khi phương pháp này được gọi, việc gọi

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
52 là bất hợp pháp. Thuộc tính
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
36 sẽ trở thành đúng sau khi tất cả dữ liệu hiện tại trong bộ đệm đã được đọc.

Phiên SSL

Mới trong phiên bản 3.6.

classSSSL.sslsession¶ssl.SSLSession

Đối tượng phiên được sử dụng bởi

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
99.

Id¶ Time¶ Timeout¶ Ticket_Lifetime_Hint¶s Has_Ticket¶timetimeoutticket_lifetime_hinthas_ticket

Cân nhắc về Bảo mật¶

Mặc định tốt nhất

Để sử dụng cho khách hàng, nếu bạn không có bất kỳ yêu cầu đặc biệt nào cho chính sách bảo mật của mình, bạn nên sử dụng chức năng

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
7 để tạo bối cảnh SSL của mình. Nó sẽ tải các chứng chỉ CA đáng tin cậy của hệ thống, bật xác thực chứng chỉ và kiểm tra tên máy chủ và cố gắng chọn cài đặt giao thức và mật mã bảo mật hợp lý.client use, if you don’t have any special requirements for your security policy, it is highly recommended that you use the
context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
7 function to create your SSL context. It will load the system’s trusted CA certificates, enable certificate validation and hostname checking, and try to choose reasonably secure protocol and cipher settings.

Ví dụ: đây là cách bạn sẽ sử dụng lớp

ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
39 để tạo kết nối an toàn, đáng tin cậy đến máy chủ SMTP:

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
3

Nếu cần có chứng chỉ máy khách cho kết nối, nó có thể được thêm vào với

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
41.

Ngược lại, nếu bạn tạo bối cảnh SSL bằng cách tự gọi Trình xây dựng

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
5, nó sẽ không có xác thực chứng chỉ cũng như kiểm tra tên máy chủ được bật theo mặc định. Nếu bạn làm như vậy, xin vui lòng đọc các đoạn dưới đây để đạt được mức độ bảo mật tốt.

Cài đặt thủ công

Xác minh chứng chỉ

Khi gọi trực tiếp cho hàm tạo

context = ssl.SSLContext[ssl.PROTOCOL_TLS_SERVER]
context.load_cert_chain['/path/to/certchain.pem', '/path/to/private.key']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    sock.bind[['127.0.0.1', 8443]]
    sock.listen[5]
    with context.wrap_socket[sock, server_side=True] as ssock:
        conn, addr = ssock.accept[]
        ...
5,
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
77 là mặc định. Vì nó không xác thực người ngang hàng khác, nên nó có thể không an toàn, đặc biệt là ở chế độ máy khách, nơi hầu hết thời gian bạn muốn đảm bảo tính xác thực của máy chủ mà bạn đang nói chuyện. Do đó, khi ở chế độ máy khách, rất khuyến khích sử dụng
>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
1. Tuy nhiên, bản thân nó không đủ; Bạn cũng phải kiểm tra xem chứng chỉ máy chủ có thể thu được bằng cách gọi
>>> ssl.OPENSSL_VERSION
'OpenSSL 1.0.2k  26 Jan 2017'
5 không, phù hợp với dịch vụ mong muốn. Đối với nhiều giao thức và ứng dụng, dịch vụ có thể được xác định bởi tên máy chủ; Trong trường hợp này, hàm
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
60 có thể được sử dụng. Kiểm tra chung này được tự động thực hiện khi
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
47 được bật.

Đã thay đổi trong phiên bản 3.7: Các kết hợp tên máy chủ hiện được thực hiện bởi OpenSSL. Python không còn sử dụng

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
60.Hostname matchings is now performed by OpenSSL. Python no longer uses
import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context[]

with socket.create_connection[[hostname, 443]] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
60.

Trong chế độ máy chủ, nếu bạn muốn xác thực máy khách của mình bằng lớp SSL [thay vì sử dụng cơ chế xác thực cấp cao hơn], bạn cũng sẽ phải chỉ định

>>> cert = {'subject': [[['commonName', 'example.com'],],]}
>>> ssl.match_hostname[cert, "example.com"]
>>> ssl.match_hostname[cert, "example.org"]
Traceback [most recent call last]:
  File "", line 1, in 
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
1 và kiểm tra tương tự chứng chỉ máy khách.

Phiên bản giao thức

Các phiên bản SSL 2 và 3 được coi là không an toàn và do đó nguy hiểm khi sử dụng. Nếu bạn muốn khả năng tương thích tối đa giữa máy khách và máy chủ, nên sử dụng

ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
5 hoặc
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
6 làm phiên bản giao thức. SSLV2 và SSLV3 bị tắt theo mặc định.

hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
context.load_verify_locations['path/to/cabundle.pem']

with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
    with context.wrap_socket[sock, server_hostname=hostname] as ssock:
        print[ssock.version[]]
4

Bối cảnh SSL được tạo ở trên sẽ chỉ cho phép các kết nối TLSV1.2 trở lên [nếu được hỗ trợ bởi hệ thống của bạn] đến máy chủ.

ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
5 ngụ ý xác thực chứng chỉ và kiểm tra tên máy chủ theo mặc định. Bạn phải tải chứng chỉ vào bối cảnh.

Lựa chọn mật mã

Nếu bạn có các yêu cầu bảo mật nâng cao, việc tinh chỉnh các mật mã được kích hoạt khi đàm phán một phiên SSL là có thể thông qua phương pháp

Hostname: www.google.com
[{'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Google Inc'],],
             [['commonName', 'Google Internet Authority G2'],]],
  'notAfter': 'Sep 11 11:04:38 2014 GMT',
  'notBefore': 'Sep 11 11:04:38 2013 GMT',
  'serialNumber': '50C71E48BCC50676',
  'subject': [[['countryName', 'US'],],
              [['stateOrProvinceName', 'California'],],
              [['localityName', 'Mountain View'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'www.google.com'],]],
  'subjectAltName': [['DNS', 'www.google.com'],],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'GeoTrust Inc.'],],
             [['commonName', 'GeoTrust Global CA'],]],
  'notAfter': 'Apr  4 15:15:55 2015 GMT',
  'notBefore': 'Apr  5 15:15:55 2013 GMT',
  'serialNumber': '023A69',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Google Inc'],],
              [['commonName', 'Google Internet Authority G2'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 21 04:00:00 2018 GMT',
  'notBefore': 'May 21 04:00:00 2002 GMT',
  'serialNumber': '12BBE6',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'GeoTrust Inc.'],],
              [['commonName', 'GeoTrust Global CA'],]],
  'version': 3},
 {'issuer': [[['countryName', 'US'],],
             [['organizationName', 'Equifax'],],
             [['organizationalUnitName',
               'Equifax Secure Certificate Authority'],]],
  'notAfter': 'Aug 22 16:41:51 2018 GMT',
  'notBefore': 'Aug 22 16:41:51 1998 GMT',
  'serialNumber': '35DEF4CF',
  'subject': [[['countryName', 'US'],],
              [['organizationName', 'Equifax'],],
              [['organizationalUnitName',
                'Equifax Secure Certificate Authority'],]],
  'version': 3}]
43. Bắt đầu từ Python 3.2.3, mô -đun SSL vô hiệu hóa một số mật mã yếu nhất định theo mặc định, nhưng bạn có thể muốn hạn chế hơn nữa lựa chọn mật mã. Hãy chắc chắn đọc tài liệu OpenSSL, về định dạng danh sách mật mã. Nếu bạn muốn kiểm tra các mật mã nào được bật bởi một danh sách mật mã đã cho, hãy sử dụng lệnh
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
54 hoặc lệnh
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
55 trên hệ thống của bạn.

Nhiều quá trình

Nếu sử dụng mô-đun này như là một phần của ứng dụng đa xử lý [ví dụ, ví dụ, các mô-đun

ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
56 hoặc
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
57], hãy lưu ý rằng trình tạo số ngẫu nhiên nội bộ của OpenSSL không xử lý đúng các quy trình. Các ứng dụng phải thay đổi trạng thái PRNG của quy trình mẹ nếu họ sử dụng bất kỳ tính năng SSL nào với
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
58. Bất kỳ cuộc gọi thành công nào của
>>> ssl.enum_certificates["CA"]
[[b'data...', 'x509_asn', {'1.3.6.1.5.5.7.3.1', '1.3.6.1.5.5.7.3.2'}],
 [b'data...', 'x509_asn', True]]
5,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
60 hoặc
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
ctx.options &= ~ssl.OP_NO_SSLv3
61 là đủ.

TLS 1.3¶

Mới trong phiên bản 3.7.

Giao thức TLS 1.3 hoạt động hơi khác so với phiên bản TLS/SSL trước đó. Một số tính năng TLS 1.3 mới chưa có sẵn.

  • TLS 1.3 sử dụng một bộ các bộ mật mã khác nhau. Tất cả các bộ mật mã AES-GCM và Chacha20 đều được bật theo mặc định. Phương pháp

    Hostname: www.google.com
    [{'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'Google Inc'],],
                 [['commonName', 'Google Internet Authority G2'],]],
      'notAfter': 'Sep 11 11:04:38 2014 GMT',
      'notBefore': 'Sep 11 11:04:38 2013 GMT',
      'serialNumber': '50C71E48BCC50676',
      'subject': [[['countryName', 'US'],],
                  [['stateOrProvinceName', 'California'],],
                  [['localityName', 'Mountain View'],],
                  [['organizationName', 'Google Inc'],],
                  [['commonName', 'www.google.com'],]],
      'subjectAltName': [['DNS', 'www.google.com'],],
      'version': 3},
     {'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'GeoTrust Inc.'],],
                 [['commonName', 'GeoTrust Global CA'],]],
      'notAfter': 'Apr  4 15:15:55 2015 GMT',
      'notBefore': 'Apr  5 15:15:55 2013 GMT',
      'serialNumber': '023A69',
      'subject': [[['countryName', 'US'],],
                  [['organizationName', 'Google Inc'],],
                  [['commonName', 'Google Internet Authority G2'],]],
      'version': 3},
     {'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'Equifax'],],
                 [['organizationalUnitName',
                   'Equifax Secure Certificate Authority'],]],
      'notAfter': 'Aug 21 04:00:00 2018 GMT',
      'notBefore': 'May 21 04:00:00 2002 GMT',
      'serialNumber': '12BBE6',
      'subject': [[['countryName', 'US'],],
                  [['organizationName', 'GeoTrust Inc.'],],
                  [['commonName', 'GeoTrust Global CA'],]],
      'version': 3},
     {'issuer': [[['countryName', 'US'],],
                 [['organizationName', 'Equifax'],],
                 [['organizationalUnitName',
                   'Equifax Secure Certificate Authority'],]],
      'notAfter': 'Aug 22 16:41:51 2018 GMT',
      'notBefore': 'Aug 22 16:41:51 1998 GMT',
      'serialNumber': '35DEF4CF',
      'subject': [[['countryName', 'US'],],
                  [['organizationName', 'Equifax'],],
                  [['organizationalUnitName',
                    'Equifax Secure Certificate Authority'],]],
      'version': 3}]
    
    43 không thể kích hoạt hoặc vô hiệu hóa bất kỳ mật mã TLS 1.3 nào, nhưng
    ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
    ctx.options &= ~ssl.OP_NO_SSLv3
    
    54 trả về chúng.

  • Vé phiên không còn được gửi như một phần của bắt tay ban đầu và được xử lý khác nhau.

    ctx = ssl.create_default_context[Purpose.CLIENT_AUTH]
    ctx.options &= ~ssl.OP_NO_SSLv3
    
    64 và
    hostname = 'www.python.org'
    # PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
    context = ssl.SSLContext[ssl.PROTOCOL_TLS_CLIENT]
    context.load_verify_locations['path/to/cabundle.pem']
    
    with socket.socket[socket.AF_INET, socket.SOCK_STREAM, 0] as sock:
        with context.wrap_socket[sock, server_hostname=hostname] as ssock:
            print[ssock.version[]]
    
    17 không tương thích với TLS 1.3.

  • Chứng chỉ phía máy khách cũng không còn được xác minh trong thời gian bắt tay ban đầu.Một máy chủ có thể yêu cầu chứng chỉ bất cứ lúc nào.Khách hàng xử lý các yêu cầu chứng chỉ trong khi họ gửi hoặc nhận dữ liệu ứng dụng từ máy chủ.

  • TLS 1.3 Các tính năng như dữ liệu ban đầu, yêu cầu chứng nhận máy khách TLS hoãn lại, cấu hình thuật toán chữ ký và việc tái sử dụng vẫn chưa được hỗ trợ.

Bài Viết Liên Quan

Chủ Đề