#!/usr/bin/env python
#-*-coding:utf-8-*-
#GUI设计与构建 客户端建立from tkinter import *
from tkinter.ttk import *
import socket
import struct#启动服务
def start(host,port):pass#我的控件
class MyFrame(Frame):def __init__(self,root):super().__init__(root)self.root=rootself.grid()#网格化布局self.remote_ip='127.0.0.1'self.remote_ports=10888self.remote_ip_var=StringVar()self.remote_ports_var=IntVar()self.bak_src_var=StringVar#图形化界面self.init_components()def init_components(self):proj_name=Label(self,text="远程备份客户机")#位置proj_name.grid(columnspan=2)serv_ip_label=Label(self,text="服务地址: ")#第一行第一列serv_ip_label.grid(row=1)self.serv_ip=Entry(self,textvariable=self.remote_ip_var)#默认ip地址self.remote_ports_var.set(self.remote_ip)self.serv_ip.grid(row=1,column=1)#端口号框serv_port_label=Label(self,text="服务端口: ")serv_port_label.grid(row=2)self.serv_port=Entry(self,textvariable=self.remote_ports_var)#默认的ports窗口self.remote_ports_var.set(self.remote_ports)self.serv_port.grid(row=2,column=1)src_label=Label(self,text="备份的目标:")src_label.grid(row=3)self.bak_src=Entry(self,textvariable=self.bak_src_var)self.bak_src.grid(row=3,column=1)#定以端口号self.start_serv_btn=Button(self,text="开始备份",command=self.start_send)self.start_serv_btn.grid(row=4)self.start_serv_btn=Button(self,text="退出程序",command=self.root.destroy)self.start_serv_btn.grid(row=4,column=1)#开始发送的方法def start_send(self):print(self.remote_ip_var.get(),self.remote_ports_var.get())print('start---')if __name__=='__main__':root=Tk()root.title('远程备份客户机')root.resizable(False,False)app=MyFrame(root)#启动程序app.mainloop()#!/usr/bin/env python
#-*-coding:utf-8-*-
#GUI设计与构建
from tkinter import *
from tkinter.ttk import *
import socket
import struct#启动服务
def start(host,port):pass#我的控件
class MyFrame(Frame):def __init__(self,root):super().__init__(root)self.root=rootself.grid()self.local_ip='127.0.0.1'self.serv_ports=[10888,20888,30888]#图形化界面self.init_components()def init_components(self):proj_name=Label(self,text="远程备份服务器")#位置proj_name.grid(columnspan=2)serv_ip_label=Label(self,text="服务地址")#第一行第一列serv_ip_label.grid(row=1)#下拉地址框self.serv_ip=Combobox(self,values=self.get_ipaddr())#默认ip地址self.serv_ip.set(self.local_ip)self.serv_ip.grid(row=1,column=1)#端口号框serv_port_label=Label(self,text="服务端口")serv_port_label.grid(row=2)self.serv_port=Combobox(self,values=self.serv_ports)#默认的ports窗口self.serv_port.set(self.serv_ports[0])self.serv_port.grid(row=2,column=1)#定以端口号self.start_serv_btn=Button(self,text="启动服务",command=self.start_serv)self.start_serv_btn.grid(row=3)self.start_serv_btn=Button(self,text="退出服务",command=self.root.destroy)self.start_serv_btn.grid(row=3,column=1)#获取本地ip地址def get_ipaddr(self):host_name=socket.gethostname()info=socket.gethostbyname_ex(host_name)info=info[2]#企业IP地址info.append(self.local_ip)return info
#启动服务器的方法def start_serv(self):print(self.serv_ip.get(),self.serv_port.get())start(self.serv_ip.get(),self.serv_port.get())if __name__=='__main__':root=Tk()root.title('备份服务器')root.resizable(False,False)app=MyFrame(root)#启动程序app.mainloop()#!/usr/bin/env python
#-*-coding:utf-8-*-
#GUI设计与构建 客户端具体建立from tkinter import *
from tkinter.ttk import *
import socket
import struct
import os
import pickle#获取文件信息
def get_files_info(path):if not path or not os.path.exists(path):return Nonefiles=os.walk(path)infos=[]file_paths=[]for p,ds,fs in files:for f in fs:file_name=os.path.join(p,f)file_size=os.stat(file_name).st_sizefile_paths.append(file_name)file_name=file_name[len(path)+1:]infos.append((file_size,file_name))return infos,file_paths
#向服务器发送信息
def send_files_infos(my_sock,infos):fmt_str='Q'#二进制编码infos_bytes=pickle.dumps(infos)infos_bytes_len=len(infos_bytes)infos_len_pack=struct.pack(fmt_str,infos_bytes_len)#二进制编码#发送长度my_sock.sendall(infos_len_pack)#发送信息my_sock.sendall(infos_bytes)#发送文件
def send_files(my_sock,file_path):f=open(file_path,'rb')try:while True:data=f.read(1024)if data:my_sock.sendall(data)else:breakfinally:f.close()
#接收从服务器发来的信息
def get_bak_info(my_sock,size=7):info=my_sock.recv(size)print(info.decode('utf-8'))#开始备份
def start(host,port,src):if not os.path.exists(src):print('备份的目标不存在!')return s=socket.socket()s.connect((host,port))path=srcfile_infos,file_paths=get_files_info(path)send_files_infos(s,file_infos)for fp in file_paths:send_files(s,fp)print(fp)get_bak_info(s)s.close()#我的控件
class MyFrame(Frame):def __init__(self,root):super().__init__(root)self.root=rootself.grid()#网格化布局self.remote_ip='127.0.0.1'self.remote_ports=10888self.remote_ip_var=StringVar()self.remote_ports_var=IntVar()self.bak_src_var=StringVar()#图形化界面self.init_components()def init_components(self):proj_name=Label(self,text="远程备份客户机")#位置proj_name.grid(columnspan=2)serv_ip_label=Label(self,text="服务地址: ")#第一行第一列serv_ip_label.grid(row=1)self.serv_ip=Entry(self,textvariable=self.remote_ip_var)#默认ip地址self.remote_ip_var.set(self.remote_ip)self.serv_ip.grid(row=1,column=1)#端口号框serv_port_label=Label(self,text="服务端口: ")serv_port_label.grid(row=2)self.serv_port=Entry(self,textvariable=self.remote_ports_var)#默认的ports窗口self.remote_ports_var.set(self.remote_ports)self.serv_port.grid(row=2,column=1)src_label=Label(self,text="备份的目标:")src_label.grid(row=3)self.bak_src=Entry(self,textvariable=self.bak_src_var)self.bak_src.grid(row=3,column=1)#定以端口号self.start_serv_btn=Button(self,text="开始备份",command=self.start_send)self.start_serv_btn.grid(row=4)self.start_serv_btn=Button(self,text="退出程序",command=self.root.destroy)self.start_serv_btn.grid(row=4,column=1)#开始发送的方法def start_send(self):print(self.remote_ip_var.get(),self.remote_ports_var.get())print('start---')start(self.remote_ip_var.get(),int(self.remote_ports_var.get()),self.bak_src_var.get())if __name__=='__main__':root=Tk()root.title('远程备份客户机')root.resizable(False,False)app=MyFrame(root)#启动程序app.mainloop()#!/usr/bin/env python
#-*-coding:utf-8-*-
#GUI设计与构建 实现一个客户端的备份问题
from tkinter import *
from tkinter.ttk import *
import socket
import struct
import os
import pickleBAK_PATH=r'/home/acm506/桌面/备份/'#接收文件信息
def recv_unit_data(clnt,infos_len):data=b''if 0<infos_len<=1024:data+=clnt.recv(infos_len)else:while True:if infos_len>1024:data+=clnt.recv(1024)infos_len-=1024else:data+=clnt.recv(infos_len)breakreturn data#得到文件的大小和名称
def get_files_info(clnt):fmt_str='Q'#文件的大小headsize=struct.calcsize(fmt_str)data =clnt.recv(headsize)infos_len=struct.unpack(fmt_str,data)[0]#二进制编码data=recv_unit_data(clnt,infos_len)return pickle.loads(data)#创建文件的路径
def mk_path(filepath):paths=filepath.split(os.path.sep)[:-1]p=BAK_PATHfor path in paths:p=os.path.join(p,path)if not os.path.exists(p):os.mkdir(p)
#接收固定长度的长度的文件
def recv_file(clnt,infos_len,filepath):mk_path(filepath)filepath=os.path.join(BAK_PATH,filepath)f=open(filepath,'wb+')try:if 0<infos_len<=1024:data=clnt.recv(infos_len)f.write(data)else:while True:if infos_len>1024:data=clnt.recv(1024)f.write(data)infos_len-=1024else:data=clnt.recv(infos_len)f.write(data)#写入文件breakexcept:print('error')else:return Truefinally:f.close()#向客户端发送成功的消息
def send_echo(clnt,res):if res:clnt.sendall(b'success')else:clnt.sendall(b'failure')#启动服务
def start(host,port):if not os.path.exists(BAK_PATH):os.mkdir(BAK_PATH)st=socket.socket()st.bind((host,port))st.listen(1)client,addr=st.accept()#得到文件列表files_lst=get_files_info(client)#每个元素是个元组for size,filepath in files_lst:res=recv_file(client,size,filepath)#备份成功send_echo(client,res)client.close()st.close()#我的控件
class MyFrame(Frame):def __init__(self,root):super().__init__(root)self.root=rootself.grid()self.local_ip='127.0.0.1'self.serv_ports=[10888,20888,30888]#图形化界面self.init_components()def init_components(self):proj_name=Label(self,text="远程备份服务器")#位置proj_name.grid(columnspan=2)serv_ip_label=Label(self,text="服务地址")#第一行第一列serv_ip_label.grid(row=1)#下拉地址框self.serv_ip=Combobox(self,values=self.get_ipaddr())#默认ip地址self.serv_ip.set(self.local_ip)self.serv_ip.grid(row=1,column=1)#端口号框serv_port_label=Label(self,text="服务端口")serv_port_label.grid(row=2)self.serv_port=Combobox(self,values=self.serv_ports)#默认的ports窗口self.serv_port.set(self.serv_ports[0])self.serv_port.grid(row=2,column=1)#定以端口号self.start_serv_btn=Button(self,text="启动服务",command=self.start_serv)self.start_serv_btn.grid(row=3)self.start_serv_btn=Button(self,text="退出服务",command=self.root.destroy)self.start_serv_btn.grid(row=3,column=1)#获取本地ip地址def get_ipaddr(self):host_name=socket.gethostname()info=socket.gethostbyname_ex(host_name)info=info[2]#企业IP地址info.append(self.local_ip)return info
#启动服务器的方法def start_serv(self):print(self.serv_ip.get(),self.serv_port.get())start(self.serv_ip.get(),int(self.serv_port.get()))if __name__=='__main__':root=Tk()root.title('备份服务器')root.resizable(False,False)app=MyFrame(root)#启动程序app.mainloop()
#!/usr/bin/env python
#-*-coding:utf-8-*-
#GUI设计与构建 实现多个客户端的备份问题,多线程问题,压缩备份,socketserver模块
from tkinter import *
from tkinter.ttk import *
import socket
import struct
import os
import pickle
import threading,time
import socketserverBAK_PATH=r'/home/acm506/桌面/备份/'#接收文件信息
def recv_unit_data(clnt,infos_len):data=b''if 0<infos_len<=1024:data+=clnt.recv(infos_len)else:while True:if infos_len>1024:data+=clnt.recv(1024)infos_len-=1024else:data+=clnt.recv(infos_len)breakreturn data#得到文件的大小和名称
def get_files_info(clnt):fmt_str='Q?'#文件的大小headsize=struct.calcsize(fmt_str)data =clnt.recv(headsize)#文件长度及压缩编码infos_len,compress=struct.unpack(fmt_str,data)#二进制编码data=recv_unit_data(clnt,infos_len)return pickle.loads(data),compress#创建文件的路径
def mk_path(filepath):paths=filepath.split(os.path.sep)[:-1]p=BAK_PATHfor path in paths:p=os.path.join(p,path)if not os.path.exists(p):os.mkdir(p)#得到压缩文件的长度
def get_compress_size(clnt):fmt_str="Q"size=struct.calcsize(fmt_str)data=clnt.recv(size)size,=struct.unpack(fmt_str,data)return size#接收固定长度的长度的文件
def recv_file(clnt,infos_len,filepath,compress):mk_path(filepath)filepath=os.path.join(BAK_PATH,filepath)if compress:infos_len=get_compress_size(clnt)filepath=''.join([os.path.splitext(filepath)[0],'.tar.gz'])f=open(filepath,'wb+')try:if 0<infos_len<=1024:data=clnt.recv(infos_len)f.write(data)else:while True:if infos_len>1024:data=clnt.recv(1024)f.write(data)infos_len-=1024else:data=clnt.recv(infos_len)f.write(data)#写入文件breakexcept:print('error')else:return Truefinally:f.close()#向客户端发送成功的消息
def send_echo(clnt,res):if res:clnt.sendall(b'success')else:clnt.sendall(b'failure')def client_operate(client):#得到文件列表files_lst,compress=get_files_info(client)#每个元素是个元组for size,filepath in files_lst:res=recv_file(client,size,filepath,compress)#备份成功send_echo(client,res)client.close()class BakHdl(socketserver.StreamRequestHandler):def handle(self):client_operate(self.request)
#开启服务
def start(host,port):server=socketserver.ThreadingTCPServer((host,port),BakHdl)s=threading.Thread(target=server.serve_forever)s.start()return server#我的控件
class MyFrame(Frame):def __init__(self,root):super().__init__(root)self.root=rootself.grid()self.server=Noneself.local_ip='127.0.0.1'self.serv_ports=[10888,20888,30888]#图形化界面self.init_components()def init_components(self):proj_name=Label(self,text="远程备份服务器")#位置proj_name.grid(columnspan=2)serv_ip_label=Label(self,text="服务地址")#第一行第一列serv_ip_label.grid(row=1)#下拉地址框self.serv_ip=Combobox(self,values=self.get_ipaddr())#默认ip地址self.serv_ip.set(self.local_ip)self.serv_ip.grid(row=1,column=1)#端口号框serv_port_label=Label(self,text="服务端口")serv_port_label.grid(row=2)self.serv_port=Combobox(self,values=self.serv_ports)#默认的ports窗口self.serv_port.set(self.serv_ports[0])self.serv_port.grid(row=2,column=1)#定以端口号self.start_serv_btn=Button(self,text="启动服务",command=self.start_serv)self.start_serv_btn.grid(row=3)self.start_exit_btn=Button(self,text="退出服务",command=self.exit)self.start_exit_btn.grid(row=3,column=1)def exit(self):if self.server:threading.Thread(target=self.server.shutdown).start()self.root.destroy()#获取本地ip地址def get_ipaddr(self):host_name=socket.gethostname()info=socket.gethostbyname_ex(host_name)info=info[2]#企业IP地址info.append(self.local_ip)return info
#启动服务器的方法def start_serv(self):if not os.path.exists(BAK_PATH):os.mkdir(BAK_PATH)#print(self.serv_ip.get(),self.serv_port.get())#start(self.serv_ip.get(),int(self.serv_port.get()))host=self.serv_ip.get()port=int(self.serv_port.get())#serv_th=threading.Thread(target=start,args=(host,port))#serv_th.start()self.server=start(host,port)self.start_serv_btn.state(['disabled',])if __name__=='__main__':root=Tk()root.title('备份服务器')root.resizable(False,False)app=MyFrame(root)#启动程序app.mainloop()#!/usr/bin/env python
#-*-coding:utf-8-*-
#GUI设计与构建 客户端具体建立from tkinter import *
from tkinter.ttk import *
import socket
import struct
import os
import pickle
import threading,time
import tarfile,tempfile#获取文件信息
def get_files_info(path):if not path or not os.path.exists(path):return Nonefiles=os.walk(path)infos=[]file_paths=[]for p,ds,fs in files:for f in fs:file_name=os.path.join(p,f)file_size=os.stat(file_name).st_sizefile_paths.append(file_name)file_name=file_name[len(path)+1:]infos.append((file_size,file_name))return infos,file_paths
#向服务器发送信息
def send_files_infos(my_sock,infos,compress):fmt_str='Q?'infos_bytes=pickle.dumps(infos)infos_bytes_len=len(infos_bytes)#二进制编码infos_len_pack=struct.pack(fmt_str,infos_bytes_len,compress)#二进制编码#发送长度my_sock.sendall(infos_len_pack)#发送信息my_sock.sendall(infos_bytes)#发送文件
def send_files(my_sock,file_path,compress):if not compress:f=open(file_path,'rb')else:f=tempfile.NamedTemporaryFile()#临时文件tar=tarfile.open(mode='w|gz',fileobj=f)tar.add(file_path)tar.close()f.seek(0)filesize=os.stat(f.name).st_sizefilesize_bytes=struct.pack('Q',filesize)my_sock.sendall(filesize_bytes)try:while True:data=f.read(1024)if data:my_sock.sendall(data)else:breakfinally:f.close()
#接收从服务器发来的信息
def get_bak_info(my_sock,size=7):info=my_sock.recv(size)print(info.decode('utf-8'))#开始备份
def start(host,port,src,compress):if not os.path.exists(src):print('备份的目标不存在!')return s=socket.socket()s.connect((host,port))path=srcfile_infos,file_paths=get_files_info(path)send_files_infos(s,file_infos,compress)for fp in file_paths:send_files(s,fp,compress)print(fp)get_bak_info(s)# time.sleep(2)s.close()#我的控件
class MyFrame(Frame):def __init__(self,root):super().__init__(root)self.root=rootself.grid()#网格化布局self.remote_ip='127.0.0.1'self.remote_ports=10888self.remote_ip_var=StringVar()self.remote_ports_var=IntVar()self.bak_src_var=StringVar()self.compress_var=BooleanVar()#图形化界面self.init_components()def init_components(self):proj_name=Label(self,text="远程备份客户机")#位置proj_name.grid(columnspan=2)serv_ip_label=Label(self,text="服务地址: ")#第一行第一列serv_ip_label.grid(row=1)self.serv_ip=Entry(self,textvariable=self.remote_ip_var)#默认ip地址self.remote_ip_var.set(self.remote_ip)self.serv_ip.grid(row=1,column=1)#端口号框serv_port_label=Label(self,text="服务端口: ")serv_port_label.grid(row=2)self.serv_port=Entry(self,textvariable=self.remote_ports_var)#默认的ports窗口self.remote_ports_var.set(self.remote_ports)self.serv_port.grid(row=2,column=1)src_label=Label(self,text="备份的目标:")src_label.grid(row=3)self.bak_src=Entry(self,textvariable=self.bak_src_var)self.bak_src.grid(row=3,column=1)tar_label=Label(self,text="压缩备份: ")tar_label.grid(row=4)self.compress_on=Checkbutton(self,text="开启压缩",variable=self.compress_var,onvalue=1,offvalue=0)self.compress_on.grid(row=4,column=1)#定以端口号self.start_serv_btn=Button(self,text="开始备份",command=self.start_send)self.start_serv_btn.grid(row=5)self.start_exit_btn=Button(self,text="退出程序",command=self.root.destroy)self.start_exit_btn.grid(row=5,column=1)#开始发送的方法def start_send(self):#print(self.remote_ip_var.get(),self.remote_ports_var.get())#print('start---')#start(self.remote_ip_var.get(),int(self.remote_ports_var.get()),self.bak_src_var.get())host=self.remote_ip_var.get()port=self.remote_ports_var.get()compress=self.compress_var.get()src=self.bak_src_var.get()self.bak_src_var.set('')t=threading.Thread(target=start,args=(host,int(port),src,compress))t.start()if __name__=='__main__':root=Tk()root.title('远程备份客户机')root.resizable(False,False)app=MyFrame(root)#启动程序app.mainloop()#!/usr/bin/env python
#-*-coding:utf-8-*-
#GUI设计与构建 实现多个客户端的备份问题,多线程问题,压缩备份
from tkinter import *
from tkinter.ttk import *
import socket
import struct
import os
import pickle
import threading,timeSERV_RUN_FLAG=True
flag_lock=threading.Lock()BAK_PATH=r'/home/acm506/桌面/备份/'#接收文件信息
def recv_unit_data(clnt,infos_len):data=b''if 0<infos_len<=1024:data+=clnt.recv(infos_len)else:while True:if infos_len>1024:data+=clnt.recv(1024)infos_len-=1024else:data+=clnt.recv(infos_len)breakreturn data#得到文件的大小和名称
def get_files_info(clnt):fmt_str='Q?'#文件的大小headsize=struct.calcsize(fmt_str)data =clnt.recv(headsize)#文件长度及压缩编码infos_len,compress=struct.unpack(fmt_str,data)#二进制编码data=recv_unit_data(clnt,infos_len)return pickle.loads(data),compress#创建文件的路径
def mk_path(filepath):paths=filepath.split(os.path.sep)[:-1]p=BAK_PATHfor path in paths:p=os.path.join(p,path)if not os.path.exists(p):os.mkdir(p)#得到压缩文件的长度
def get_compress_size(clnt):fmt_str="Q"size=struct.calcsize(fmt_str)data=clnt.recv(size)size,=struct.unpack(fmt_str,data)return size#接收固定长度的长度的文件
def recv_file(clnt,infos_len,filepath,compress):mk_path(filepath)filepath=os.path.join(BAK_PATH,filepath)if compress:infos_len=get_compress_size(clnt)filepath=''.join([os.path.splitext(filepath)[0],'.tar.gz'])f=open(filepath,'wb+')try:if 0<infos_len<=1024:data=clnt.recv(infos_len)f.write(data)else:while True:if infos_len>1024:data=clnt.recv(1024)f.write(data)infos_len-=1024else:data=clnt.recv(infos_len)f.write(data)#写入文件breakexcept:print('error')else:return Truefinally:f.close()#向客户端发送成功的消息
def send_echo(clnt,res):if res:clnt.sendall(b'success')else:clnt.sendall(b'failure')def client_operate(client):#得到文件列表files_lst,compress=get_files_info(client)#每个元素是个元组for size,filepath in files_lst:res=recv_file(client,size,filepath,compress)#备份成功send_echo(client,res)client.close()#启动服务
def start(host,port):if not os.path.exists(BAK_PATH):os.mkdir(BAK_PATH)st=socket.socket()st.settimeout(1)#超时1sst.bind((host,port))st.listen(1)flag_lock.acquire()#获得资源的使用while SERV_RUN_FLAG:flag_lock.release()client=Nonetry:client,addr=st.accept()except socket.timeout:passif client:t=threading.Thread(target=client_operate,args=(client,))t.start()flag_lock.acquire()st.close()#我的控件
class MyFrame(Frame):def __init__(self,root):super().__init__(root)self.root=rootself.grid()self.local_ip='127.0.0.1'self.serv_ports=[10888,20888,30888]#图形化界面self.init_components()def init_components(self):proj_name=Label(self,text="远程备份服务器")#位置proj_name.grid(columnspan=2)serv_ip_label=Label(self,text="服务地址")#第一行第一列serv_ip_label.grid(row=1)#下拉地址框self.serv_ip=Combobox(self,values=self.get_ipaddr())#默认ip地址self.serv_ip.set(self.local_ip)self.serv_ip.grid(row=1,column=1)#端口号框serv_port_label=Label(self,text="服务端口")serv_port_label.grid(row=2)self.serv_port=Combobox(self,values=self.serv_ports)#默认的ports窗口self.serv_port.set(self.serv_ports[0])self.serv_port.grid(row=2,column=1)#定以端口号self.start_serv_btn=Button(self,text="启动服务",command=self.start_serv)self.start_serv_btn.grid(row=3)self.start_exit_btn=Button(self,text="退出服务",command=self.root.destroy)self.start_exit_btn.grid(row=3,column=1)#获取本地ip地址def get_ipaddr(self):host_name=socket.gethostname()info=socket.gethostbyname_ex(host_name)info=info[2]#企业IP地址info.append(self.local_ip)return info
#启动服务器的方法def start_serv(self):#print(self.serv_ip.get(),self.serv_port.get())#start(self.serv_ip.get(),int(self.serv_port.get()))host=self.serv_ip.get()port=int(self.serv_port.get())serv_th=threading.Thread(target=start,args=(host,port))serv_th.start()self.start_serv_btn.state(['disabled',])#定义自己图形化界面窗口
class MyTk(Tk):def destroy(self):global SERV_RUN_FLAGwhile True:if flag_lock.acquire():SERV_RUN_FLAG=Falseflag_lock.release()breaksuper().destroy()if __name__=='__main__':root=MyTk()root.title('备份服务器')root.resizable(False,False)app=MyFrame(root)#启动程序app.mainloop()