标签:com write connect 连接服务器 top stream 创建 编程基础 连接
# TCP编程
# - 创建socket
# - 连接服务器
# -发送数据
# - 接受数据
# - 关闭socket
#
import socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((‘www.baidu.com‘,80))
s.send(b‘GET/HTTP/1.1/r\nHost:www.baidu.com\r\nCpnnection:close\r\n\r\n‘)
buf = []
while True:
d = s.recv(1024)
if d:
buf.append(d)
else:
break
data = b‘‘.join(buf)
s.close()
header, html = data.split((b‘\r\n\r\n‘),1)
print(header.decode(‘UTF-8‘))
with open(r‘C:\Users\dhf\Desktop\baidu.html‘,‘wb‘) as b:
b.write(html)
标签:com write connect 连接服务器 top stream 创建 编程基础 连接
原文地址:https://www.cnblogs.com/Osword/p/9693034.html