Skip to content

Instantly share code, notes, and snippets.

@zhangwc
Created September 27, 2018 01:58
Show Gist options
  • Save zhangwc/7e559cf06542b4a9203dcc200ac5f717 to your computer and use it in GitHub Desktop.
Save zhangwc/7e559cf06542b4a9203dcc200ac5f717 to your computer and use it in GitHub Desktop.
AnchoredLowestEngineer created by zhangwc - https://repl.it/@zhangwc/AnchoredLowestEngineer
import re
import socket
# 创建一个socket:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 建立连接:
s.connect(('www.sina.com.cn', 80))
# 发送数据:
s.send(b'GET / HTTP/1.1\r\nHost: www.sina.com.cn\r\nConnection: close\r\n\r\n')
# 接收数据:
buffer = []
while True:
# 每次最多接收1k字节:
d = s.recv(1024)
if d:
buffer.append(d)
else:
break
data = b''.join(buffer)
# 关闭连接:
s.close()
header, html = data.split(b'\r\n\r\n', 1)
print(header.decode('utf-8'))
print(html)
# 把接收的数据写入文件:
# with open('sina.html', 'wb') as f:
# f.write(html)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment