diff --git a/tcp_server.py b/tcp_server.py new file mode 100644 index 0000000..e16a061 --- /dev/null +++ b/tcp_server.py @@ -0,0 +1,60 @@ +import os +import socket +import zipfile + +def receive_zip(HOST,PORT): + # 获取当前文件的绝对路径和目录路径 + current_file_path = os.path.abspath(__file__) + current_directory = os.path.dirname(current_file_path) + + # 创建一个socket对象 + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket: + # 绑定IP和端口 + server_socket.bind((HOST, PORT)) + # 开始监听传入的连接 + server_socket.listen() + + print(f"等待客户端连接...") + conn, addr = server_socket.accept() # 接受客户端的连接 + print(f"已连接:{addr}") + + with conn: + # 从客户端接收数据 + encrypted_data_path = os.path.join(current_directory, 'encrypted_data.zip') + with open(encrypted_data_path, 'wb') as file: + while True: + file_data = conn.recv(1024) + if not file_data: + break + file.write(file_data) + + print("文件接收完成") + + # 解压缩 ZIP 文件 + output_folder = os.path.join(current_directory) + os.makedirs(output_folder, exist_ok=True) + + with zipfile.ZipFile(encrypted_data_path, 'r') as zip_file: + zip_file.extractall(output_folder) + + print("解压缩完成到目标文件夹:", output_folder) + +def send_Bpubkey(HOST,PORT): + # 获取当前文件的绝对路径和目录路径 + current_file_path = os.path.abspath(__file__) + current_directory = os.path.dirname(current_file_path) + + # 文件路径 + Bpubkey_path = os.path.join(current_directory, 'B_public.txt') + + # 客户端连接服务器 + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket: + client_socket.connect((HOST, PORT)) + + # 读取要传输的文件 + with open(Bpubkey_path, 'rb') as file: + file_data = file.read() + + # 发送文件数据 + client_socket.sendall(file_data) + print("文件发送完成") \ No newline at end of file