azm*_*euk 7 python ssl-certificate python-asyncio aiohttp
我知道如何使用pyopenssl获取证书信息,例如到期日期,但是可以使用aiohttp 响应对象吗?
Bob*_*rph 11
我在aiohttp的文档中找不到它,但你可以使用ssl获取证书和OpenSSL来获取它不是日期,并将它与你当前的日期进行比较,以确定它是否已过期.更多详细信息 如何在python中导入OpenSSL以及在 下面执行您需要的代码片段您需要事先安装OpenSSL然而
pip install pyopenssl
import OpenSSL
import ssl, socket
cert=ssl.get_server_certificate(('www.google.com', 443))
x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
x509.get_notAfter()
Run Code Online (Sandbox Code Playgroud)
对于使用SNI的站点,请参阅以下答案,了解如何获取具有SNI的站点的证书ssl.get_server_certificate(服务器名称指示)
先前的答案是正确的,但是,您也可以使用套接字库(这是对python 3.7的测试)
from urllib.request import Request, urlopen, ssl, socket
from urllib.error import URLError, HTTPError
import json
#some site without http/https in the path
base_url = 'CHANGE_ME_TO_YOUR_SITE'
port = '443'
hostname = base_url
context = ssl.create_default_context()
with socket.create_connection((hostname, port)) as sock:
with context.wrap_socket(sock, server_hostname=hostname) as ssock:
print(ssock.version())
data = json.dumps(ssock.getpeercert())
# print(ssock.getpeercert())
print (data)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14358 次 |
| 最近记录: |