关于python';s套接字备份
我已经编写了一个python脚本来备份我的文件,比如Dropbox 但是有一些错误。我有一个名为关于python';s套接字备份,python,sockets,Python,Sockets,我已经编写了一个python脚本来备份我的文件,比如Dropbox 但是有一些错误。我有一个名为SyncServer的类,有两个函数分别名为\uuu init\uuu和TF1TF1代表“线程函数1” 当我写入thread.start\u new\u thread(TF1,(conn,0))第一个参数时,我在其中发送了一个套接字对象。不幸的是,python的IDLE返回了一个错误:NameError:未定义全局名称“TF1” # -*- coding: cp950 -*- import wx, s
SyncServer
的类,有两个函数分别名为\uuu init\uuu
和TF1
TF1
代表“线程函数1”
当我写入thread.start\u new\u thread(TF1,(conn,0))
第一个参数时,我在其中发送了一个套接字对象。不幸的是,python的IDLE返回了一个错误:NameError:未定义全局名称“TF1”
# -*- coding: cp950 -*-
import wx, socket, os, md5, thread, threading
class SyncClient:HOST = "127.0.0.1"
def __init__(self):
self.config = {}
open("sync.config", "a").close()
f = open("sync.config", "r")
line = f.readline()
while line:
tmp = line.split(":")
self.config[tmp[0]] = ":".join(tmp[1:]).split("\n")[0]
line = f.readline()
f.close()
ex = wx.App()
ex.MainLoop()
if (not self.config.has_key("id")) or (not self.config.has_key("password")) or (not self.config.has_key("port")) or (not self.config.has_key("path")):
wx.MessageBox('something wrong. Q__________________________Q', 'Error',
wx.OK | wx.ICON_ERROR)
return
if (not os.access(self.config["path"], os.F_OK)):
wx.MessageBox("It seems that " + self.config["path"] + " doesn't exist.", 'Error',
wx.OK | wx.ICON_ERROR)
return
if int(self.config['port']) > 5:
wx.MessageBox('something wrong. Q__________________________Q', 'Error',
wx.OK | wx.ICON_ERROR)
return
chpswd = md5.new(self.config['password']).hexdigest()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((self.HOST, 7575))
self.s = s;
s.send("CHECK ID")
if s.recv(1024) != "200 OK":
return
s.send(config['id'] + ";;" + chpswd)
if s.recv(1024) == "False":
wx.MessageBox("id and password not match.", 'Error',
wx.OK | wx.ICON_ERROR)
return
self.path = []
for root, dirs, files in os.walk(self.config['path']):
for f in files:
self.path.append(root + f)
self.s.send("FILE NAME")
if self.s.recv(1024) != "200 OK":
continue
self.s.send(f)
if self.s.recv(1024) != "200 OK":
continue
self.s.send("FILE LEN")
if self.s.recv(1024) != "200 OK":
continue
cut = file_cut(root + f)
self.s.send(len(cut))
MakeThread(cut)
def MakeSocket(self):
self.s.send("GIVE ME A PORT")
port = int(self.s.recv(1024))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((self.HOST, port))
return s
def MakeThread(self, cut):
self.ptr = 0
s = MakeSocket()
for i in self.config['port']:
#s = MakeSocket()
thread.start_new_thread(TF, (s, cut))
def TF(self, Socket, cut):
l = len(cut)
while self.ptr < l:
Socket.send(self.ptr)
if Socket.recv(1024) != "200 OK":
continue
Socket.send(cut[self.ptr])
self.ptr += 1
Socket.close()
def file_cut(self, path):
f = open(path, "rb")
content = f.read()
cut = []
l = len(content)
i = 0
while i < l:
cut.append(content[i:i+1024])
i += 1024
return cut
'''f = open(path, "rb")
cont = f.read()
f.close()
fsize = len(cont)
fname = path.split("\\")[-1]
self.com.send(fname)
check = self.com.recv(1024)
if check != "200 OK": return
self.com.send(str(fsize))
check = self.com.recv(1024)
if check != "200 OK": return
i = 0
while i < fsize + 1025:
Socket.send(cont[i:i+1024])
i += 1024'''
def file_recv(self, Socket, path=".\\"):
fname = self.com.recv(1024)
self.com.send("200 OK")
f = open(path + fname, "wb")
fsize = self.com.recv(1024)
self.com.send("200 OK")
i = 0
while i < fsize + 1025:
line = Socket.recv(1024)
f.write(line)
f.flush()
i += 1024
f.close()
class SyncServer:
def TF1(self, Socket, null):
while True:
data = Socket.recv(1024)
if data == "CHECK ID":
Socket.send("200 OK!")
user = Socket.recv(1024)
u = open("uid.txt","r")
while True:
udata = u.readline().split(" ")
if udata == "":
Socket.send("False")
break
if user.split(";;")[0] == udata[0]:
Flag = True
if user.split(";;")[1] != md5.hexidigest(udata[1]):
Socket.send("False")
else:
self.user = user.split(";;")[0]
self.files[self.user] = []
Socket.send("True")
break
if data == "GIVE ME A PORT":
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("", self.portList[0]))
s.listen(1)
Socket.send(self.portList[0])
for i in range(0, flen):
thread.start_new_thread(TF2, (s.accept(), 0))
f = open(fname, "wb")
for line in self.files[self.user]:
f.write(line)
f.close()
#self.port
if data == "FILE NAME":
Socket.send("200 OK")
fname = Socket.recv(1024)
Socket.send("200 OK")
if data == "FILE LEN":
Socket.send("200 OK")
flen = int(Socket.recv(1024))
def TF2(self, Socket, null):
idx = Socket.recv(1024)
Socket.send("200 OK")
line = Socket.recv(1024)
self.files[self.user][idx] = line
def __init__(self):
self.portList = []
self.files = {}
for i in range(7576,7700):
self.portList.append(i)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("", 7575))
s.listen(1)
while True:
conn, addr = s.accept()
thread.start_new_thread(TF1, (conn, 0))
#-*-编码:cp950-*-
导入wx、套接字、操作系统、md5、线程、线程
类SyncClient:HOST=“127.0.0.1”
定义初始化(自):
self.config={}
打开(“sync.config”,“a”).close()
f=打开(“sync.config”、“r”)
line=f.readline()
while line:
tmp=行分割(“:”)
self.config[tmp[0]]=“:”.join(tmp[1:]).split(“\n”)[0]
line=f.readline()
f、 关闭()
ex=wx.App()
例如MainLoop()
如果(非self.config.has_key(“id”))或(非self.config.has_key(“密码”))或(非self.config.has_key(“端口”))或(非self.config.has_key(“路径”):
MessageBox('something.Q.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuq','Error',
wx.OK | wx.ICON_错误)
返回
如果(不是os.access(self.config[“path”],os.F_OK)):
MessageBox(“似乎“+self.config[“path”]+”不存在。”,“Error”,
wx.OK | wx.ICON_错误)
返回
如果int(self.config['port'])>5:
MessageBox('something.Q.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuq','Error',
wx.OK | wx.ICON_错误)
返回
chpswd=md5.new(self.config['password']).hexdigest()
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s、 连接((self.HOST,7575))
self.s=s;
s、 发送(“检查ID”)
如果s.recv(1024)!=“200好”:
返回
s、 发送(配置['id']+“;”+chpswd)
如果s.recv(1024)=“False”:
MessageBox(“id和密码不匹配。”,“错误”,
wx.OK | wx.ICON_错误)
返回
self.path=[]
对于os.walk(self.config['path'])中的根目录、目录和文件:
对于文件中的f:
self.path.append(root+f)
self.s.send(“文件名”)
如果self.s.recv(1024)!=“200好”:
持续
self.s.send(f)
如果self.s.recv(1024)!=“200好”:
持续
self.s.send(“文件长度”)
如果self.s.recv(1024)!=“200好”:
持续
剪切=文件剪切(根+f)
自s.send(len(切割))
制造螺纹(切割)
def MakeSocket(自):
self.s.send(“给我一个端口”)
端口=int(self.s.recv(1024))
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s、 连接((self.HOST,端口))
返回s
def MakeThread(自切、切割):
self.ptr=0
s=MakeSocket()
对于self.config['port']中的i:
#s=MakeSocket()
螺纹。开始新螺纹(TF,(s,切割))
def TF(自身、插座、切割):
l=透镜(切割)
当self.ptr