我正在尝试使用python2.7.15从(IP,destport443)获取SNI,我使用的是OpenSSL的最新版本和ssl模块。在
这是我的代码:
import OpenSSL as OSsl #This two modules are imported for the only purpose of getting the SNI using function defined by us down here getSNI
import ssl
ip = "52.85.25.17"
dport = "443"
#With this function we get the Server Name Identification for the trasmissions with Secure Socket Layer identified by the port 443. We only care about the destinationIP and the destinationPort
def getSNI(ip, dport):
if dport != "443":
commonName = "Not SSL"
print commonName
else:
server_certificate = ssl.get_server_certificate((ip, dport))
x509 = OSsl.crypto.load_certificate(OSsl.crypto.FILETYPE_PEM, server_certificate) #x509 is referred to the standard used for PKI (Public Key Infrastructure) used in this case for ciphering our informations about certificate
#FILETYPE_PEM serializes data to a Base64-Encoded
#getting the informations about Certificate
certInfo = x509.get_subject()
commonName = certInfo.commonName
print (commonName)
return commonName
getSNI(ip,dport)
这是可行的,但是对于指定的地址(在我发布的代码片段中),我得到以下错误:
^{pr2}$我已经升级了所有的模块和包,我读了很多关于这个主题的问题,我不知道如何解决这个问题
编辑1:执行whois时,我发现这个IpAddress与Amazon相连,那么Amazon和SNI有什么特别的问题吗?在
SNI的重点是可能存在多个域,这些域被解析为具体的IP地址。所以,您提供的IP(
52.85.25.17
)就是这样的地址之一。服务器无法决定您请求的是哪个域的证书,因此它以错误的方式终止连接。在附录1。捕捉SSLError异常
您可以通过以下方式捕获
ssl.SSLError
:相关问题 更多 >
编程相关推荐