简体   繁体   English

如何使用python获取ssl证书详细信息

[英]how to get ssl certificate details using python

I am using the following code to validate the ssl certificate status, Can we get more details about certificate like common name (CN),expiry date and issuer using request module or urllib我正在使用以下代码来验证 ssl 证书状态,我们能否使用请求模块或 urllib 获取有关证书的更多详细信息,例如通用名称 (CN)、到期日期和颁发者

import requests

def check_ssl(url):
    try:
        req = requests.get(url, verify=True)
        print url + ' has a valid SSL certificate!'
    except requests.exceptions.SSLError:
        print url + ' has INVALID SSL certificate!'

check_ssl('https://google.com')
check_ssl('https://example.com')
import socket
import ssl
import datetime

domains_url = [
"devnote.in",
"devnote_wrong.in",
"stackoverflow.com",
"stackoverflow.com/status/404"
]

def ssl_expiry_datetime(hostname):
    ssl_dateformat = r'%b %d %H:%M:%S %Y %Z'

    context = ssl.create_default_context()
    context.check_hostname = False

    conn = context.wrap_socket(
        socket.socket(socket.AF_INET),
        server_hostname=hostname,
    )
    # 5 second timeout
    conn.settimeout(5.0)

    conn.connect((hostname, 443))
    ssl_info = conn.getpeercert()
    # Python datetime object
    return datetime.datetime.strptime(ssl_info['notAfter'], ssl_dateformat)

if __name__ == "__main__":
    for value in domains_url:
        now = datetime.datetime.now()
        try:
            expire = ssl_expiry_datetime(value)
            diff = expire - now
            print ("Domain name: {} Expiry Date: {} Expiry Day: {}".format(value,expire.strftime("%Y-%m-%d"),diff.days))
        except Exception as e:
            print (e)

#Output :

Domain name: devnote.in Expiry Date: 2020-11-11 Expiry Day: 46
[Errno -2] Name or service not known
Domain name: stackoverflow.com Expiry Date: 2020-11-05 Expiry Day: 41
[Errno -2] Name or service not known

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 selenium python 获取网站 SSL 证书到期、颁发者等的详细信息? - How to fetch details of website SSL certificate expiry, issuer etc using selenium python? 如何在 Selenium (Python) 中获取网页的 SSL 详细信息 - How to get SSL details of a webpage in Selenium (Python) 如何使用python ssl模块获取证书的签名密钥长度 - How to get the certificate's signature key length using python ssl module 如何获得 Python 请求以信任自签名 SSL 证书? - How to get Python requests to trust a self signed SSL certificate? "如何从 python 中的请求中获取响应 SSL 证书?" - How to get response SSL certificate from requests in python? 使用 Selenium 从网站提取 SSL 证书详细信息 - Chrome 驱动程序 - Extract SSL certificate details from website using Selenium - Chrome driver 如何使用存储在 python 中的字符串变量中的证书打开 ssl 套接字 - How to open ssl socket using certificate stored in string variables in python 如何使用 python 解码 SSL 证书? - How can I decode a SSL certificate using python? 如何使用 selenium 和 python 在 IE 中处理 SSL 证书? - How to handle SSL Certificate in IE using selenium with python? 使用 get() 时如何修复 SSL 证书错误? - How do I fix a SSL Certificate error when using a get()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM