标签:执行命令 命令 utf-8 bin import 套接字 client sock net
# 执行命令的tcp服务端 import socket import os server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((‘127.0.0.1‘, 3000)) server.listen(5) while 1: conn, client_addr = server.accept() while 1: try: dirname = conn.recv(1024).decode(‘utf-8‘) res = str(os.listdir(dirname)).encode(‘utf-8‘) conn.send(res) except Exception as e: print(e) break conn.close()
# 输入命令并显示结果的tcp客户端 import socket client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((‘127.0.0.1‘, 3000)) while 1: dirname = r‘E:\python之路\01 课程\week08-day01‘.encode(‘utf-8‘) client.send(dirname) res = client.recv(1024).decode(‘utf-8‘) print(res) break
标签:执行命令 命令 utf-8 bin import 套接字 client sock net
原文地址:https://www.cnblogs.com/caoyu080202201/p/12742060.html