Python network transmission based on the source file folder Twisted framework

Since the folder may have a multi-layer directory, so it needs to be recursive traversal.

This paper took the simple protocol customization, defines five commands, instructions Head as follows:
Sync: identifies the start synchronizing folders
End: identifying end synchronization
File: file name identifies the transfer of (relative path)
Folder: flag folder (relative path)
None :document content

Each command CMB_BEGIN begin to CMB_END end.

The client needs to be done to resolve the receive buffer, an extraction of an instruction, then do instructions Head according to corresponding processing, such as create folders, files written.

Here is the server code:

from twisted.internet import reactor
from twisted.internet.protocol import Protocol,Factory
from twisted.protocols.basic import LineReceiver
import os
import 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 = SimpleLogger
reactor.listenTCP(1234, factory)
reactor.run()

Server Client after receiving a signal of (this code, when just send anything to Client Server can), Server gets called send_file_folder the contents of the folder under sever all sent to the client.

The server runs the results are as follows: Here Insert Picture Description
The following is the client code:

from twisted.internet.selectreactor import SelectReactor
from twisted.internet.protocol import Protocol,ClientFactory
from twisted.protocols.basic import LineReceiver
import os
from struct import *
  
reactor = SelectReactor()
protocol = Protocol()
prepare = 0
filename = ""
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 the command from the Server when extracted Sync command, it will sourceDir empty directory, then under the follow instructions, file with the Server folder for synchronization.

The client runs as follows: Here Insert Picture Description
where Note:
When Client written to the file, you need to open the file in binary mode, otherwise, errors may occur or lead to corrupted files when transferring binary files.

After testing, the code can be a normal operation, the folder synchronization is successful, text files, images, and other types of binary files can be normal transmission.

Finally, I recommend a good reputation python gathering [ click to enter ], there are a lot of old-timers learning skills, learning experience

, Interview skills, workplace experience and other share, the more carefully prepared the zero-based introductory information, information on actual projects, programmers every day

Python method to explain the timing of technology, to share some of the learning and the need to pay attention to small details

Published 10 original articles · won praise 0 · Views 3947

Guess you like

Origin blog.csdn.net/haoxun11/article/details/104886815