接上篇,代码里面文件发送和接受全部写道一起了比较乱
最近再次整理了一下
服务端 协议代码
class Status: RECV='RECV' #接受文件状态 SEND='SEND' #发送文件状态 DONE='DONE' #当前文件传输完成 WAIT='WAIT' #等待对方响应) LOOP='LOOP' #循环发送字节流 ABORT='ABORT' #终止发送 class FileTranServerProtocol(LineReceiver): """socket文件传输协议""" #---------------------------------------------------------------------- blocksize=65536 def __init__(self): self.fp=None self.totalsize=0 self.rest=0#这个用来扩展断点功能 self.mystatus=None self.yourstatus=None #根据命令行 查找对应的命令 def lineReceived(self, line): arr=line.split(':') cmd=line[0:4] param=pickle.loads(line[4:]) func=getattr(self,'do_'+cmd) func(param) #处理 下载文件 命令 def do_STOR(self,param): serverpath,rest=param[0],param[1] serverpath=os.path.join(Cfg.c_sharefilesdir,serverpath) self.fp=open(serverpath,'rb') if rest and rest<os.path.getsize(serverpath):self.fp.seek(rest) self.STAT(Status.SEND) def dataReceived(self,data): LineReceiver.dataReceived(self,data) #告诉对方 我的状态 def STAT(self,status): print 'tell other '+status self.mystatus=status self.sendLine('STAT'+pickle.dumps(status)) def do_STAT(self,param): print 'tell me '+param self.yourstatus=param #接受方告诉我们发送字节流 if self.yourstatus==Status.LOOP: while 1: buf=self.fp.read(self.blocksize) if not buf:break self.transport.write(buf) self.transport.loseConnection() def connectionLost(self, reason): if self.fp:self.fp.close()
def RunFileTran(reactor): f=Factory() f.protocol=FileTranServerProtocol reactor.listenTCP(8200,f) if __name__=='__main__': from twisted.internet import reactor RunFileTran(reactor) reactor.run()
class FileTranClientProtocol(LineReceiver): """socket文件传输协议""" #---------------------------------------------------------------------- def __init__(self,serverpath,localpath,rest,callBack=None): self.fp=None self.mystatus=None self.yourstatus=None self.callBack=callBack self.rest=rest self.localpath=localpath self.serverpath=serverpath #根据命令行 查找对应的命令 def lineReceived(self, line): arr=line.split(':') cmd=line[0:4] param=pickle.loads(line[4:]) func=getattr(self,'do_'+cmd) func(param) def dataReceived(self,data): #我处在接受文件 状态 对方处在发送文件状态 if self.mystatus==Status.LOOP: print 'get data '+str(len(data)) self.fp.write(data) if self.callBack:self.callBack(len(data)) else: #处理命令行 LineReceiver.dataReceived(self,data) def Run(self): self.fp=open(self.localpath,'ab') if self.rest else open(self.localpath,'wb') self.sendLine('STOR'+pickle.dumps((self.serverpath,self.rest))) self.STAT(Status.RECV) #告诉对方 我的状态 def STAT(self,status): print 'tell other '+status self.mystatus=status self.sendLine('STAT'+pickle.dumps(status)) def do_STAT(self,param): print 'tell me '+param self.yourstatus=param #告诉发送端开始文件流循环 if self.yourstatus==Status.SEND and self.mystatus==Status.RECV: self.STAT(Status.LOOP) def connectionMade(self): self.Run() def connectionLost(self, reason): if self.fp:self.fp.close()
if __name__=='__main__': from twisted.internet import reactor def GetFile(serverpath,localpath,rest=0): f=FileTranClientFactory(serverpath,localpath,rest=0) reactor.connectTCP("localhost", 8200,f) GetFile('3af82f21-94db-4231-915c-f2a72195c680.exe','D:/1.exe') reactor.run()
运行后成功将共享文件夹里面的内容下载到D盘了,以后要是加文件夹下载功能估计又要吐血了