服务器 import socket import os import json sk = socket.socket() sk.bind(('127.0.0.1',8080)) sk.listen() conn,addr = sk.accept() def show_dir(conn,path): return os.listdir(path=path) def return_res(conn,current_path): res = show_dir(conn,current_path) show_res = '--'.join(res) conn.send(show_res.encode('utf-8')) abs_path = conn.recv(1024).decode('utf-8') current_path = abs_path return_res(conn,current_path) while 1: dic_str = conn.recv(1024).decode('utf-8') dic = json.loads(dic_str) if dic['opt'] == 'cd': current_path = current_path + '/' + dic['filename'] if os.path.isdir(current_path): return_res(conn,current_path) else: conn.send('该文件不是文件夹,请重新输入!') continue if dic['opt'] == '..': l = current_path.split('/') l.pop() current_path = '/'.join(l) return_res(conn,current_path+'/') conn.close() sk.close() 客户端 import socket import json sk = socket.socket() sk.connect(('127.0.0.1',8080)) dirs = input('请输入当前目录绝对路径>>>') sk.send(dirs.encode('utf-8')) print(sk.recv(1024).decode('utf-8').split('--')) dic = {'opt':None,'filename':None} while 1: cmd = input('请输入命令操作>>>') dic['opt'] = cmd if cmd == 'cd': dic['filename'] = input('请输入要查看的文件夹名>>>') dic_str = json.dumps(dic) sk.send(dic_str.encode('utf-8')) print(sk.recv(1024).decode('utf-8').split('--')) sk.close()