【问题标题】:How to get subject dn with python如何使用 python 获取主题 dn
【发布时间】:2021-04-18 14:41:48
【问题描述】:

用 python 获取 SSL 的主题 DN 的好方法是什么?

这包括以下内容:

  • 国家/地区名称
  • 州名
  • 地区名称
  • 组织名称
  • 通用名

【问题讨论】:

    标签: python ssl python-sockets


    【解决方案1】:

    类似:

    import ssl
    import socket
    from pprint import pprint
    
    
    hostname = 'www.example.com'
    context = ssl.create_default_context()
    
    with socket.create_connection((hostname, 443)) as sock:
        with context.wrap_socket(sock, server_hostname=hostname) as ssock:
            pprint(ssock.getpeercert()['subject'])
    

    输出:

    ((('countryName', 'US'),),
     (('stateOrProvinceName', 'California'),),
     (('localityName', 'Los Angeles'),),
     (('organizationName', 'Internet Corporation for Assigned Names and Numbers'),),
     (('commonName', 'www.example.org'),))
    

    在此处阅读有关getpeercert() 的更多信息:https://docs.python.org/3/library/ssl.html#ssl.SSLSocket.getpeercert

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-26
      相关资源
      最近更新 更多