From 09762264f1dbdc0057a663e8a3d2dc12387dc5f8 Mon Sep 17 00:00:00 2001 From: px3gkymai <1565756097@qq.com> Date: Sat, 17 Dec 2022 16:11:30 +0800 Subject: [PATCH] ADD file via upload --- client_sendsig.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 client_sendsig.py diff --git a/client_sendsig.py b/client_sendsig.py new file mode 100644 index 0000000..50643d5 --- /dev/null +++ b/client_sendsig.py @@ -0,0 +1,38 @@ +import socket +import tqdm +import os +from config import HOST, PORT +import time + +def sendsig(): + # 传输数据分隔号 + SEPARATOR = "" + # 文件传输缓冲区 + BUFFER_SIZE = 4096 + # 传输的文件 + filename = "sigfile.bin" + # 文件大小 + file_size = os.path.getsize(filename) + # 创建socket链接 + s = socket.socket() + # 链接服务器 + print(f"服务器连接中{HOST}:{PORT}") + s.connect((HOST, PORT)) + print("与服务器连接成功!") + # 发送文件名与文件大小,进行编码出来encode() + s.send(f"{filename}{SEPARATOR}{file_size}".encode()) + + time.sleep(1) + + # 文件传输 + 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()