forked from xianhu/LearnPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_socket.py
80 lines (62 loc) · 1.87 KB
/
python_socket.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# _*_ coding: utf-8 _*_
"""
Socket编程
"""
import sys
import socket
def server_func(port):
"""
服务端
"""
# 1. 创建socket对象
server = socket.socket()
# 2. 绑定ip和端口
server.bind(("127.0.0.1", port))
# 3. 监听是否有客户端连接
server.listen(10)
print("服务端已经启动%s端口......" % port)
# 4. 接收客户端连接
sock_obj, address = server.accept()
sock_obj.settimeout(3)
print("客户端:%s,超时时间:%s" % (address, sock_obj.gettimeout()))
while True:
try:
# 5. 接收客户端发送的消息
recv_data = sock_obj.recv(1024).decode("utf-8")
print("客户端端 -> 服务端: %s" % recv_data)
if recv_data == "quit":
break
# 6. 给客户端回复消息
send_data = "received[%s]" % recv_data
sock_obj.send(send_data.encode("utf-8"))
print("服务端 -> 客户端: %s" % send_data)
except Exception as excep:
print("error: ", excep)
# 7. 关闭socket对象
sock_obj.close()
server.close()
def client_func(port):
"""
客户端
"""
# 1. 创建客户端的socket对象
client = socket.socket()
# 2. 连接服务端, 需要指定端口和IP
client.connect(("127.0.0.1", port))
while True:
# 3. 给服务端发送数据
send_data = input("客户端>").strip()
client.send(send_data.encode("utf-8"))
if send_data == "quit":
break
# 4. 获取服务端返回的消息
recv_data = client.recv(1024).decode("utf-8")
print("服务端 -> 客户端: %s" % recv_data)
# 5. 关闭socket连接
client.close()
if __name__ == '__main__':
flag = sys.argv[1]
if flag == "server":
server_func(9901)
else:
client_func(9901)