You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.1 KiB
39 lines
1.1 KiB
2 years ago
|
import socket
|
||
|
import tqdm
|
||
|
import os
|
||
|
from config import HOST, PORT
|
||
|
import time
|
||
|
|
||
|
def sendsig():
|
||
|
# 传输数据分隔号
|
||
|
SEPARATOR = "<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()
|