web 客户端程序
'''
send request 'GET' to www.baidu.com
and save the receved content to a txt file
'''
from socket import *
import sys
header_send = b'GET / HTTP/1.1\r\nHost: www.baidu.com\r\nConnection: close\r\n\r\n'
HOST = 'www.baidu.com'
PORT = 80
BUFSIZ = 1024
# create a socket which support IPv4/IPv6
for res in getaddrinfo(HOST, PORT, AF_UNSPEC,
SOCK_STREAM, 0, AI_PASSIVE):
af, socktype, proto, canonname, sockaddr = res
try:
webClient = socket(af, socktype)
except OSError as msg:
webClient = None
continue
try:
webClient.connect(sockaddr)
except OSError as msg:
webClient.close()
webClient = None
continue
break
if webClient is None:
print('could not open socket')
sys.exit(1)
buf = [] # save the received content
wit