-
Notifications
You must be signed in to change notification settings - Fork 54
/
socket_1.py
67 lines (54 loc) · 1.79 KB
/
socket_1.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
"""
socket 简易服务器和客户端,能够在服务器端执行命令,并把结果回传到客户端
"""
#Server_side_start
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import socket,os,time
server = socket.socket()
server.bind(('localhost',9999))
server.listen()
while True:
conn,addr = server.accept()
print("new conn",addr)
while True:
data = conn.recv(8192)
if not data:
print("客户端已断开")
break
print("执行指令",data)
cmd_res = os.popen(data.decode()).read() #接收字符串,执行结果也是字符串
if not cmd_res:
cmd_res = "cmd has no output"
conn.send(str(len(cmd_res.encode("utf-8"))).encode("utf-8")) #先发大小给客户端
# time.sleep(0.5)
client_ack = conn.recv(1024) #wait client confirm,解决socket粘包问题
conn.send(cmd_res.encode("utf-8"))
print("send done...\n等待新指令...")
server.close()
#Server_side_end
================================分割线===============================
#client_start
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import socket
client = socket.socket()
client.connect(('localhost',9999))
while True:
cmd = input(">>: ").strip()
if not cmd: continue
client.send(cmd.encode("utf-8"))
cmd_res_size = client.recv(1024) #接收命令结果的长度
client.send("准备好接收了".encode("utf-8"))
received_size = 0
received_data = b''
while received_size < int(cmd_res_size.decode()) : #判断接收长度,直到接收完毕
data = client.recv(1024)
received_size += len(data)
#print(data.decode())
received_data += data
else:
print(received_data.decode())
print("cmd res receive done..")
client.close()
#client_end