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.
106 lines
3.4 KiB
106 lines
3.4 KiB
import os
|
|
import socket
|
|
from threading import Thread
|
|
from tkinter import LEFT, RIGHT, Button, Label, StringVar, ttk
|
|
from tkinter.tix import Tk # 升级的组合控件包
|
|
from server_twokey import server_twokey
|
|
from ttkbootstrap import Style
|
|
import tqdm
|
|
|
|
from server_panduan import panduan
|
|
|
|
|
|
def agree():
|
|
# 设计IP和端口
|
|
SERVER_HOST = "0.0.0.0"
|
|
SERVER_PORT = 9799
|
|
|
|
# 文件读写缓冲区
|
|
BUFFER_SIZE = 4096
|
|
SEPARATOR = "<SEPARATOR>"
|
|
|
|
# 创建Server
|
|
s = socket.socket()
|
|
s.bind((SERVER_HOST, SERVER_PORT))
|
|
|
|
# 设置连接数
|
|
s.listen(5)
|
|
print(f"服务器监听{SERVER_HOST}:{SERVER_PORT}")
|
|
|
|
while True:
|
|
# 接受客户端连接
|
|
client_socket, address = s.accept()
|
|
zhaiyao = Label(
|
|
root, text="接收成功!", bg="red", bd=1, font=("Arial", 12), width=10, height=1
|
|
)
|
|
zhaiyao.pack()
|
|
# 输出客户端IP
|
|
print(f"客户端{address}连接")
|
|
# 接受客户端信息
|
|
received = client_socket.recv(BUFFER_SIZE).decode()
|
|
filename, file_size = received.split(SEPARATOR)
|
|
# 获取传输文件名和大小
|
|
filename = os.path.basename(filename)
|
|
file_size = int(file_size)
|
|
|
|
# 文件接受处理
|
|
progress = tqdm.tqdm(
|
|
range(file_size),
|
|
f"接收{filename}",
|
|
unit="B",
|
|
unit_divisor=1024,
|
|
unit_scale=True,
|
|
)
|
|
with open(filename, "wb") as f:
|
|
for _ in progress:
|
|
# 读取客户端数据
|
|
bytes_read = client_socket.recv(BUFFER_SIZE)
|
|
if not bytes_read:
|
|
break
|
|
f.write(bytes_read)
|
|
progress.update(len(bytes_read))
|
|
# 关闭
|
|
client_socket.close()
|
|
print("客户端关闭")
|
|
s.close()
|
|
|
|
|
|
def socketStart():
|
|
print("服务器启动")
|
|
t = Thread(target=agree)
|
|
t.start()
|
|
|
|
|
|
def init_window():
|
|
global root
|
|
root = Tk()
|
|
style = Style(theme='minty')
|
|
root.title("服务器端")
|
|
root.geometry("700x500")
|
|
root.configure(bg='white')
|
|
root.resizable(width=False, height=False)
|
|
root.tk.eval("package require Tix") # 引入升级包,这样才能使用升级的组合控件
|
|
ttk.Button(root, text="开始接收", command=socketStart,bootstyle="default-outline").place(x=400, y=210, width=200, height=60)
|
|
model = StringVar()
|
|
model.set("请选择解密模式")
|
|
values = ["DES", "AES_ECB", "AES_CBC"]
|
|
ttk.Combobox(
|
|
master=root, # 父容器
|
|
height=10, # 高度,下拉显示的条目数量
|
|
width=20, # 宽度
|
|
state="readonly", # 设置状态 normal(可选可输入)、readonly(只可选)、 disabled
|
|
cursor="arrow", # 鼠标移动时样式 arrow, circle, cross, plus... # 字体
|
|
textvariable=model, # 通过StringVar设置可改变的值
|
|
values=values,
|
|
bootstyle="info",# 设置下拉框的选项
|
|
).place(x=30,y=230,width=180,height=30)
|
|
ttk.Button(
|
|
root,text="生成接收端公私钥",command=lambda: server_twokey(),bootstyle="default-outline"
|
|
).place(x=400, y=50, width=200, height=60)
|
|
ttk.Button(root, text="解密", command=lambda: panduan(root=root, op=model.get()),bootstyle="default-outline").place(x=400,y=380,width=200, height=60)
|
|
root.mainloop()
|
|
|
|
|
|
if "__main__" == __name__:
|
|
init_window()
|