# 执行命令的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()
# Enter commands and display results of the client tcp Import Socket Client = socket.socket (socket.AF_INET, socket.SOCK_STREAM) the client.connect (( ' 127.0.0.1 ' , 3000 )) the while . 1 : dirname = R & lt ' E: \ The Way to Python \ 01 Course \ week08-day01 ' .encode ( ' utf-8 ' ) client.send (dirname) res = client.recv (1024) .decode ( ' utf-8 ' ) print (res) break