Hướng dẫn python ssl get_server_certificate timeout - hết thời gian chờ ssl get_server_cert của python

Bản gốc get_server_certificate trong ssl.py không hỗ trợ thời gian chờ ổ cắm,

def get_server_certificate(addr, ssl_version=PROTOCOL_TLS, ca_certs=None):
    """Retrieve the certificate from the server at the specified address,
    and return it as a PEM-encoded string.
    If 'ca_certs' is specified, validate the server cert against it.
    If 'ssl_version' is specified, use it in the connection attempt."""

host, port = addr
if ca_certs is not None:
    cert_reqs = CERT_REQUIRED
else:
    cert_reqs = CERT_NONE
context = _create_stdlib_context(ssl_version,
                                 cert_reqs=cert_reqs,
                                 cafile=ca_certs)
with  create_connection(addr) as sock:
    with context.wrap_socket(sock) as sslsock:
        dercert = sslsock.getpeercert(True)
return DER_cert_to_PEM_cert(dercert)

Nếu tham số thời gian chờ, bản demo mẫu có thể ở đây:

>>> import ssl
>>> ssl.get_server_certificate(("www.qq.com", 443), timeout=6)
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python2.7/ssl.py", line 1017, in get_server_certificate
    with closing(create_connection(addr, timeout)) as sock:
  File "/usr/lib/python2.7/socket.py", line 575, in create_connection
    raise err
socket.error: [Errno 101] Network is unreachable

Zackeryspytz

import ssl
from socket import *

urls = [i.strip().lower() for i in open("urls.txt")]
for urls in url:
    try:
        print ssl.get_server_certificate((url, 443))
    except error:
        print "No connection"

Từ khóa: + Patchnosy: + Zackeryspytz

pull_requests: + pull_request21325Stage: nhu cầu bản vá -> Đánh giá bản váOct 29, 2014 at 13:39

2018-02-26 & NBSP; 07: 58: 58Amistad

Giai đoạn: Đánh giá bản vá -> Nhu cầu vá12 gold badges46 silver badges73 bronze badges

2017-11-03 & NBSP; 21: 50: 38

SSL sockets provide the following methods of Socket Objects:

gettimeout(), settimeout(), setblocking()

Phiên bản: + Python 3.7Nosy: + Janssen, Christian.Heimes, Alex, Dstufft

import ssl
from socket import *

settimeout(10)

urls = [i.strip().lower() for i in open("urls.txt")]
for urls in url:
    try:
        print ssl.get_server_certificate((url, 443))
    except (error, timeout) as err:
        print "No connection: {0}".format(err)

Người được chuyển nhượng: Christian.heimescomponents: + SSL, - Thư viện (LIB) Giai đoạn: Đánh giá bản vá

Giai đoạn: Đánh giá bản vá -> Nhu cầu vá12 gold badges46 silver badges73 bronze badges

2017-11-03 & NBSP; 21: 50: 38Oct 29, 2014 at 14:41

Hướng dẫn python ssl get_server_certificate timeout - hết thời gian chờ ssl get_server_cert của python

Phiên bản: + Python 3.7Nosy: + Janssen, Christian.Heimes, Alex, DstufftCasualDemon

Người được chuyển nhượng: Christian.heimescomponents: + SSL, - Thư viện (LIB) Giai đoạn: Đánh giá bản vá2 gold badges17 silver badges34 bronze badges

5

Nixawk2017-10-26 04:11 by Nixawk, last changed 2022-04-11 14:58 by admin. This issue is now closed.

tạo ra
Được tạo vào năm 2017-10-26 04:11 bởi Nixawk, lần cuối đã thay đổi 2022-04-11 14:58 bởi Quản trị viên. Vấn đề giờ đã kết thúc.Các tập tinTên tập tinTải lên
Sự mô tả Chỉnh sửa, 2017-10-26 04:11 ssl.py
Nixawk, 2017-10-26 04:11
Đã thêm hỗ trợ thời gian chờ cho func get_server_certicate trong mô -đun SSL.Kéo yêu cầuURLTải lên
Sự mô tảChỉnh sửa Chỉnh sửa, 2017-10-26 04:11
ssl.pyNixawk, 2017-10-26 04:11 Đã thêm hỗ trợ thời gian chờ cho func get_server_certicate trong mô -đun SSL., 2020-09-16 05:46
Kéo yêu cầuNixawk, 2017-10-26 04:11 Đã thêm hỗ trợ thời gian chờ cho func get_server_certicate trong mô -đun SSL., 2021-04-24 05:24
Kéo yêu cầu
URLTrạng thái*Liên kết
PR 4126
ĐóngPR 22270*
Hướng dẫn python ssl get_server_certificate timeout - hết thời gian chờ ssl get_server_cert của python
hợp nhất
Zackeryspytz, 2020-09-16 05:46
PR 25570Christian.Heimes, 2021-04-24 05:24Tin nhắn (4)
MSG305021 - (Xem)
Tác giả: Vex Woo (Nixawk) *PR 22270*
Hướng dẫn python ssl get_server_certificate timeout - hết thời gian chờ ssl get_server_cert của python
hợp nhất
Zackeryspytz, 2020-09-16 05:46
PR 25570
Christian.Heimes, 2021-04-24 05:24Tin nhắn (4) MSG305021 - (Xem)Tác giả: Vex Woo (Nixawk) *
Ngày: 2017-10-26 04:11
The original get_server_certificate in ssl.py does not support socket timeout,

def get_server_certificate(addr, ssl_version=PROTOCOL_TLS, ca_certs=None):
    """Retrieve the certificate from the server at the specified address,
    and return it as a PEM-encoded string.
    If 'ca_certs' is specified, validate the server cert against it.
    If 'ssl_version' is specified, use it in the connection attempt."""

    host, port = addr
    if ca_certs is not None:
        cert_reqs = CERT_REQUIRED
    else:
        cert_reqs = CERT_NONE
    context = _create_stdlib_context(ssl_version,
                                     cert_reqs=cert_reqs,
                                     cafile=ca_certs)
    with  create_connection(addr) as sock:
        with context.wrap_socket(sock) as sslsock:
            dercert = sslsock.getpeercert(True)
    return DER_cert_to_PEM_cert(dercert)

If a timeout parameter, a sample demo can be here:

>>> import ssl
>>> ssl.get_server_certificate(("www.qq.com", 443), timeout=6)
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python2.7/ssl.py", line 1017, in get_server_certificate
    with closing(create_connection(addr, timeout)) as sock:
  File "/usr/lib/python2.7/socket.py", line 575, in create_connection
    raise err
socket.error: [Errno 101] Network is unreachable
bộ MSG312879 - (Xem)
Tác giả: Christian Heimes (Christian.Heimes) *Christian.HeimesbộNgày: 2018/02-26 07:58
It's too late to land a new feature in 3.7.
Christian.Heimesbộ MSG391758 - (Xem)
Tác giả: Miss-Islington (Miss-Islington)Christian.HeimesbộNgày: 2021-04-24 04:46
stage: patch review -> resolved
resolution: fixed
versions: + Python 3.10, - Python 3.8
________số 8 MSG391760 - (Xem)bộNgày: 2021-04-24 05:54
messages: + msg391758
New changeset f05c2aed7e25087122613b51f152919c79641f66 by Christian Heimes in branch 'master':
bpo-31870: Fix test_get_server_certificate_timeout on Windows (GH-25570)
https://github.com/python/cpython/commit/f05c2aed7e25087122613b51f152919c79641f66
Lịch sửbộNgày
nosy: + ZackerySpytz

pull_requests: + pull_request21325Stage: nhu cầu bản vá -> Đánh giá bản vá
stage: needs patch -> patch review

2018-02-26 & NBSP; 07: 58: 58Christian.HeimesbộGiai đoạn: Đánh giá bản vá -> Nhu cầu vá
messages: + msg312879
versions: + Python 3.8, - Python 3.7
2017-11-03 & NBSP; 21: 50: 38Christian.HeimesbộGiai đoạn: Đánh giá bản vá -> Nhu cầu vá
nosy: + janssen, christian.heimes, alex, dstufft

2017-11-03 & NBSP; 21: 50: 38
components: + SSL, - Library (Lib)
stage: patch review

Phiên bản: + Python 3.7Nosy: + Janssen, Christian.Heimes, Alex, DstufftNgười được chuyển nhượng: Christian.heimescomponents: + SSL, - Thư viện (LIB) Giai đoạn: Đánh giá bản vá2017-10-26 & NBSP; 04: 11: 25