TCP网络编程
import socket
sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sk.connect(('www.sina.com.cn', 80))
sk.send(b'GET / HTTP/1.1\r\nHost:www.sina.com.cn\r\nConnection: close\r\n\r\n')
data = []
while True:
chunk = sk.recv(1024)
if chunk:
data.append(chunk)
else:
break
print(data)
data = b''.join(data)
sk.close()
header, html = data.split(b'\r\n\r\n', 1)
print(header.decode('utf-8'))
with open('sina.html', 'wb') as f:
f.write(html)