parent
b40a4ad09d
commit
dba12b93d1
@ -0,0 +1,45 @@
|
||||
import os
|
||||
import socket
|
||||
|
||||
import tqdm
|
||||
|
||||
from config import HOST, PORT
|
||||
|
||||
|
||||
def sendcipkey():
|
||||
# 传输数据分隔号
|
||||
SEPARATOR = "<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()
|
Loading…
Reference in new issue