客户端代码:
# -*- coding=utf-8 -*-
__Author__ = "Dennis"
import socket
client = socket.socket()
client.connect(("localhost",6969))
while True:
msg = input(">> ")
if not msg:continue
client.send(msg.encode())
data_size = client.recv(1024)
print("Will Size:",data_size)
true_size = int(data_size.decode())
client.send(b"recieve")
receive_size = 0
while receive_size < true_size:
data = client.recv(1024)
receive_size += len(data)
print(data.decode())
print("Size:",receive_size)
client.close()
服务端代码
# -*- coding=utf-8 -*-
__Author__ = 'Dennis'
import socket,os
server = socket.socket()
server.bind(("localhost",6969))
server.listen(5)
print("等电话...")
conn,addr = server.accept()
print("电话来啦。。")
while True:
data = conn.recv(1024)
if not data:
print("client has lost")
break
print("Command: ",data)
res_msg = os.popen(data.decode()).read()
res_msg_b = res_msg.encode()
res_size = len(res_msg_b)
conn.send(str(res_size).encode())
is_true = conn.recv(1024)
conn.send(res_msg_b)
server.close()