Python 3.7.2 使用SSL庫作爲HTTPS客戶端訪問服務器例子

import socket,ssl,sys

HOST, PORT = '14.215.177.38', 443
REQUESTSTR="GET / HTTP/1.1\r\nHost: www.baidu.com\r\nConnection: close\r\n\r\n"

print ("Creating socket..."),
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print ("done.")
 
wrappedSocket = ssl.wrap_socket(sock, keyfile=None, certfile=None, server_side=False, cert_reqs=ssl.CERT_NONE, ssl_version=ssl.PROTOCOL_SSLv23)
 
print ("Connecting to remote host..."),
wrappedSocket.connect((HOST, PORT))
print ("done.")
 
print ("Requesting document..."),
wrappedSocket.sendall(bytes(REQUESTSTR,encoding="utf8"))
print ("done.")
 
while True:
    buf = wrappedSocket.recv(4096)
    print (buf)
    if not buf:
        wrappedSocket.close()
        break
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章