We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent f63c854 commit efc52c7Copy full SHA for efc52c7
1 file changed
py3/network/do_tcp.py
@@ -0,0 +1,34 @@
1
+#!/usr/bin/env python3
2
+# -*- coding: utf-8 -*-
3
+
4
+import socket
5
6
+# 创建一个socket:
7
+s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
8
9
+# 建立连接:
10
+s.connect(('www.sina.com.cn', 80))
11
12
+# 发送数据:
13
+s.send(b'GET / HTTP/1.1\r\nHost: www.sina.com.cn\r\nConnection: close\r\n\r\n')
14
15
+# 接收数据:
16
+buffer = []
17
+while True:
18
+ # 每次最多接收1k字节:
19
+ d = s.recv(1024)
20
+ if d:
21
+ buffer.append(d)
22
+ else:
23
+ break
24
25
+data = b''.join(buffer)
26
27
+# 关闭连接:
28
+s.close()
29
30
+header, html = data.split(b'\r\n\r\n', 1)
31
+print(header.decode('utf-8'))
32
+# 把接收的数据写入文件:
33
+with open('sina.html', 'wb') as f:
34
+ f.write(html)
0 commit comments