[python]代码库
# -*- coding: UTF-8 -*-
import socket
import os
# 声明协议类型
server = socket.socket()
# 绑定本地网卡(多网卡选择),端口
server.bind(('0.0.0.0', 9999))
# 监听端口
server.listen() # 监听
while True:
conn, address = server.accept()
print("进入等待时间....")
while True:
print("收到连接....")
# 接收数据
data_server = conn.recv(1024)
if not data_server:
print('client is lost...')
break
res = os.popen("{}".format(data_server.decode())).read() # 将执行命令的结果存储返回
# 返回结果
conn.send(res.encode('utf-8'))
# -*- coding: UTF-8 -*-
import socket
# 客户端
# 声明协议类型,同时生成socket对象
client = socket.socket()
#
client.connect(('127.0.0.1', 9999)) # 服务端ip和端口
# python3 接收字节流数据
while True:
msg = input('>>:').strip()
if len(msg) == 0:
continue
client.send(msg.encode('utf-8'))
data = client.recv(1024) # 1024字节的数据
print(data.decode())