建立一个基本的socket,然后再升级为secure socket进行https通信,例子代码:
import socket
import ssl
hostname = 'www.baidu.com'
context = ssl.create_default_context()
with socket.create_connection((hostname, 443)) as sock:
with context.wrap_socket(sock, server_hostname=hostname) as ssock:
print(ssock.version())
ssock.write(b'GET / HTTP/1.1\r\nHost: www.baidu.com\r\n\r\n')
res = ssock.read()
print(res)