Python source code for folder Network Transmission Based on the Twisted framework,

Source: Internet
Author: User

Python source code for folder Network Transmission Based on the Twisted framework,

As folders may have multiple levels of directories, recursive traversal is required.

This article adopts simple protocol customization and defines five commands. The command Head is as follows:
Sync: mark the start synchronization folder
End: End of Synchronization
File: indicates the File name to be transferred (relative path)
Folder: Flag Folder (relative path)
None: File Content

Each command starts with CMB_BEGIN and ends with CMB_END.

The client needs to parse the receiving buffer, extract a command, and then process it according to the Command Head, such as creating folders and writing files.

The following is the server code:

from twisted.internet import reactorfrom twisted.internet.protocol import Protocol,Factoryfrom twisted.protocols.basic import LineReceiverimport osimport struct BUFSIZE = 4096 class SimpleLogger(Protocol):  def connectionMade(self):    print 'Got connection from', self.transport.client   def connectionLost(self, reason):    print self.transport.client, 'disconnected'   def dataReceived(self, line):    print line    self.transport.write("Hello Client, I am the Server!\r\n")     self.transport.write("CMB_BEGIN")    self.transport.write("Sync")    self.transport.write("CMB_END")    self.send_file_folder('server')   def send_file_folder(self,folder):    '''send folder to the client'''    for f in os.listdir(folder):      sourceF = os.path.join(folder, f)      if os.path.isfile(sourceF):              print 'File:',sourceF[7:]        self.transport.write("CMB_BEGIN")        self.transport.write("File:" + sourceF[7:])        self.transport.write("CMB_END")        fp = open(sourceF,'rb')        while 1:          filedata = fp.read(BUFSIZE)          if not filedata: break          else:            self.transport.write("CMB_BEGIN")            self.transport.write(filedata)            print 'send size:::::::::',len(filedata)            self.transport.write("CMB_END")        fp.close()        self.transport.write("CMB_BEGIN")        self.transport.write("End")        self.transport.write("CMB_END")      if os.path.isdir(sourceF):        print 'Folder:',sourceF[7:]        self.transport.write("CMB_BEGIN")        self.transport.write("Folder:" + sourceF[7:])        self.transport.write("CMB_END")        self.send_file_folder(sourceF) factory = Factory()factory.protocol = SimpleLoggerreactor.listenTCP(1234, factory)reactor.run()

After the Server receives a signal from the Client (in this Code, when the Client sends any content to the Server), the Server calls send_file_folder to send all the content in the sever folder to the Client.

The server running result is as follows:

The following is the client code:

from twisted.internet.selectreactor import SelectReactorfrom twisted.internet.protocol import Protocol,ClientFactoryfrom twisted.protocols.basic import LineReceiverimport osfrom struct import * reactor = SelectReactor()protocol = Protocol()prepare = 0filename = ""sourceDir = 'client'recvBuffer = '' def delete_file_folder(src):  '''delete files and folders'''  if os.path.isfile(src):    try:      os.remove(src)    except:      pass  elif os.path.isdir(src):    for item in os.listdir(src):      itemsrc = os.path.join(src,item)      delete_file_folder(itemsrc)     try:      os.rmdir(src)    except:      pass def clean_file_folder(src):  '''delete files and child folders'''  delete_file_folder(src)  os.mkdir(src) def writefile(filename,data):  print 'write file size:::::::::',len(data)  fp = open(filename,'a+b')  fp.write(data)  fp.close() class QuickDisconnectedProtocol(Protocol):  def connectionMade(self):    print "Connected to %s."%self.transport.getPeer().host    self.transport.write("Hello server, I am the client!\r\n")  def dataReceived(self, line):    global prepare    global filename    global sourceDir    global recvBuffer     recvBuffer = recvBuffer + line    self.processRecvBuffer()   def processRecvBuffer(self):    global prepare    global filename    global sourceDir    global recvBuffer     while len(recvBuffer) > 0 :      index1 = recvBuffer.find('CMB_BEGIN')      index2 = recvBuffer.find('CMB_END')       if index1 >= 0 and index2 >= 0:        line = recvBuffer[index1+9:index2]        recvBuffer = recvBuffer[index2+7:]         if line == 'Sync':          clean_file_folder(sourceDir)         if line[0:3] == "End":          prepare = 0        elif line[0:5] == "File:":          name = line[5:]          filename = os.path.join(sourceDir, name)          print 'mk file:',filename          prepare = 1        elif line[0:7] == "Folder:":          name = line[7:]          filename = os.path.join(sourceDir, name)          print 'mkdir:',filename          os.mkdir(filename)        elif prepare == 1:          writefile(filename,line)      else:        break  class BasicClientFactory(ClientFactory):  protocol=QuickDisconnectedProtocol  def clientConnectionLost(self,connector,reason):    print 'Lost connection: %s'%reason.getErrorMessage()    reactor.stop()  def clientConnectionFailed(self,connector,reason):    print 'Connection failed: %s'%reason.getErrorMessage()    reactor.stop() reactor.connectTCP('localhost',1234,BasicClientFactory())reactor.run()

The client extracts commands from the Server. When the Sync command is extracted, the sourceDir directory is cleared and synchronized with the Server folder according to the subsequent commands.

The client running result is as follows:

Note:
When the Client writes a file, it needs to open the file in binary mode. Otherwise, errors may occur during binary file transmission or file corruption occurs.

After testing, the code can run normally, the folder is synchronized successfully, and text files, images, and other types of binary files can be transmitted normally.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.