From dba12b93d1a07a96d32621bf58d35c8badb0cb50 Mon Sep 17 00:00:00 2001 From: px3gkymai <1565756097@qq.com> Date: Sat, 17 Dec 2022 16:10:57 +0800 Subject: [PATCH] ADD file via upload --- client_sendcipkey.py | 45 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 client_sendcipkey.py diff --git a/client_sendcipkey.py b/client_sendcipkey.py new file mode 100644 index 0000000..007999a --- /dev/null +++ b/client_sendcipkey.py @@ -0,0 +1,45 @@ +import os +import socket + +import tqdm + +from config import HOST, PORT + + +def sendcipkey(): + # 传输数据分隔号 + SEPARATOR = "" + + # 文件传输缓冲区 + BUFFER_SIZE = 4096 + + # 传输的文件 + filename = "encryptedkeyfile.bin" + + # 文件大小 + file_size = os.path.getsize("encryptedkeyfile.bin") + + # 创建socket链接 + s = socket.socket() + + # 链接服务器 + print(f"服务器连接中{HOST}:{PORT}") + s.connect((HOST, PORT)) + print("与服务器连接成功!") + + # 发送文件名与文件大小,进行编码出来edcode() + s.send(f"{filename}{SEPARATOR}{file_size}".encode()) + + # 文件传输 + progress = tqdm.tqdm(range(file_size), f"发送{filename}", unit="8", unit_divisor=1024) + with open(filename, "rb") as f: + for _ in progress: + + bytes_read = f.read(BUFFER_SIZE) + if not bytes_read: + break + s.sendall(bytes_read) + progress.update(len(bytes_read)) + print(f"发送文件成功!") + # 关闭 + s.close()