我有此示例代码来测量TLS握手执行时间。我得到的结果是:3.43 ms
。通过重复1000次,我得到了1 ms
以下的最短时间。我使用具有公共IP地址的设备。我也不想关心证书/主机验证。只是握手(客户端问候,服务器问候等,直到握手结束)。我使用域名连接到服务器(例如google.com)。
我的问题:我得到的表演时间结果似乎很小。我是否缺少某些参数,或者使用我的参数进行TLS握手是否正常?
python代码是:
import socket, ssl
import time
import datetime
context = ssl.SSLContext()
context.options |= ssl.OP_NO_TICKET #diable session ticket
context.verify_mode = ssl.CERT_NONE # disable cert. validation
context.check_hostname = False # disable host name checking
dn = "google.com" # example of domain to connect to
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # create socket
s.settimeout(5) # set timeout
sslSocket = context.wrap_socket(s, server_hostname = dn) # wrap socket into TLS context
try:
begin = time.process_time() # start timer
sslSocket.connect((dn, 443)) # TLS socket connection
perf = time.process_time() - begin # end timer
print ("Success! Performance time for TLS connection is: ", perf*1000) #convert from sec. to ms by multiplying with 1000
except (ssl.SSLError, ssl.SSLEOFError, ssl.CertificateError,ssl.SSLSyscallError, ssl.SSLWantWriteError, ssl.SSLWantReadError,ssl.SSLZeroReturnError) as e1:
sslSocket.close() # close the socket
except Exception as e2:
sslSocket.close() # close the socket