标签:imp __file__ src sage utf-8 技术 基于 encode code
目录格式:
构建此目录就可随意使用!
client端
import socket import sys import os import json import struct sk = socket.socket() adress = (‘192.168.12.11‘, 29231) def upload(): message = {‘filename‘: None, ‘filesize‘: None} sk.connect(adress) temp = ‘上传‘ sk.send(temp.encode(‘utf-8‘)) meg = input(‘输入文件路径:‘) filename = os.path.basename(meg) filesize = os.path.getsize(meg) message[‘filename‘] = filename message[‘filesize‘] = filesize str_message = json.dumps(message) len_message = len(str_message) b_len_message = struct.pack(‘i‘, len_message) sk.send(b_len_message + str_message.encode(‘utf-8‘)) with open(meg, ‘rb‘) as f: while filesize: connent = f.read() sk.send(connent) filesize -= len(connent) def download(): path = os.path.dirname(os.path.abspath(__file__)) sk.connect(adress) temp = ‘下载‘ sk.send(temp.encode(‘utf-8‘)) num = sk.recv(1024).decode(‘utf-8‘) print(‘可下载文件:%s‘ % (num,)) name = input(‘输入下载文件名称:‘) sk.send(name.encode(‘utf-8‘)) path = ‘/‘.join([path, ‘本地文件‘, name]) b_len_message = sk.recv(4) len_message = struct.unpack(‘i‘, b_len_message)[0] res_s = sk.recv(len_message).decode(‘utf-8‘) str_message = json.loads(res_s) filesize = str_message[‘filesize‘] with open(path, ‘ab‘) as f: while filesize: connent = sk.recv(1024) f.write(connent) f.flush() filesize -= len(connent) def main(): dic = [ (‘上传‘, ‘upload‘), (‘下载‘, ‘download‘) ] for i in enumerate(dic, 1): print(i[0], i[1][0]) num = int(input(‘请输入选项:‘)) getattr(sys.modules[__name__], dic[num - 1][1])() sk.close() if __name__ == ‘__main__‘: main()
server端
import socket import struct import os import json path = os.path.abspath(__file__) path = os.path.dirname(path) sk = socket.socket() adress = (‘0.0.0.0‘, 29231) sk.bind(adress) sk.listen(4) conn, addr = sk.accept() def upload(path): b_len_message = conn.recv(4) len_message = struct.unpack(‘i‘, b_len_message)[0] res_s = conn.recv(len_message).decode(‘utf-8‘) str_message = json.loads(res_s) filename = ‘1‘ + str_message[‘filename‘] filesize = str_message[‘filesize‘] path = ‘/‘.join([path, ‘客户端上传的文件‘, filename]) with open(path, ‘ab‘) as f: while filesize: connent = conn.recv(1024) f.write(connent) f.flush() filesize -= len(connent) def download(path): message = {‘filename‘: None, ‘filesize‘: None} res = os.listdir(‘/‘.join([path, ‘客户端上传的文件‘])) sum = ‘‘ for i in res: sum = sum + ‘ ‘ + i + ‘ ‘ conn.send(sum.encode(‘utf-8‘)) filename = conn.recv(1024).decode(‘utf-8‘) path = ‘/‘.join([path, ‘客户端上传的文件‘, filename]) filesize = os.path.getsize(path) message[‘filename‘] = path message[‘filesize‘] = filesize str_message = json.dumps(message) len_message = len(str_message) b_len_message = struct.pack(‘i‘, len_message) conn.send(b_len_message + str_message.encode(‘utf-8‘)) with open(path, ‘rb‘) as f: while filesize: connent = f.read() conn.send(connent) filesize -= len(connent) def main(): res_s = conn.recv(1024).decode(‘utf-8‘) if res_s == ‘上传‘: upload(path) sk.close() if res_s == ‘下载‘: download(path) sk.close() if __name__ == ‘__main__‘: while 1: main()
标签:imp __file__ src sage utf-8 技术 基于 encode code
原文地址:https://www.cnblogs.com/xiao-xue-di/p/9484096.html