Python Socket File Transfer example,
The sender can continuously send new files, and the receiver can continuously receive new files.
For example, if the sender input is e: \ visio.rar, the receiver will save it as e: \ new_visio.rar by default. multiple concurrent connections are supported. The specific implementation is as follows;
Acceptor:
Method 1:
#-*-Coding: UTF-8-*-import socket, time, SocketServer, struct, OS, threadhost = '2017. 168.50.74 'port = 12307 s = socket. socket (socket. AF_INET, socket. SOCK_STREAM) # define the socket type s. bind (host, port) # bind the Ip address and port number to be monitored, in the tuple format s. listen (1) def conn_thread (connection, address): while True: try: connection. settimeout (600) fileinfo_size = struct. calcsize ('128sl ') buf = connection. recv (fileinfo_size) if buf: # if This Is Not added, the first file is automatically transferred to the next filename, filesize = struct. unpack ('128sl ', buf) filename_f = filename. strip ('\ 00') filenewname = OS. path. join ('e: \ ', ('new _' + filename_f) print 'file new name is % s, filesize is % s' % (filenewname, filesize) recvd_size = 0 # defines the size of the received file. file = open (filenewname, 'wb ') print 'stat refreshing ing... 'While not recvd_size = filesize: if filesize-recvd_size> 1024: rdata = connection. recv (1024) recvd_size + = len (rdata) else: rdata = connection. recv (filesize-recvd_size) recvd_size = filesize file. write (rdata) file. close () print 'receive done' # connection. close () handle T socket. timeout: connection. close () while True: connection, address = s. accept () print ('connectedby', address) # thread = threading. thread (target = conn_thread, args = (connection, address) # threading can also be used # thread. start () thread. start_new_thread (conn_thread, (connection, address) s. close ()
Method 2:
#-*-Coding: UTF-8-*-import socket, time, SocketServer, struct, oshost = '2017. 168.50.74 'port = 12307 ADDR = (host, port) class MyRequestHandler (SocketServer. baseRequestHandler): def handle (self): print ('connectedfrom: ', self. client_address) while True: fileinfo_size = struct. calcsize ('128sl ') # defines the file information. S indicates that the file name is bytes long, and l indicates an int or log file type. Here, the file size is self. buf = self. request. recv (fileinfo_size) if self. buf: # if This Is Not added, the first file will automatically go to the next self. filename, self. filesize = struct. unpack ('128sl ', self. buf) # according to the package information of SL, print 'filesize is: ', self. filesize, 'filename size is: ', len (self. filename) # The file name length is 128, which is greater than the actual length of the file name self. filenewname = OS. path. join ('e: \ ', ('new _' + self. filename ). strip ('\ 00') # print self. filenewname, type (self. filenewname) recvd_size = 0 # defines the size of the received file = open (self. filenewname, 'wb ') print 'stat processing ing... 'While not recvd_size = self. filesize: if self. filesize-recvd_size> 1024: rdata = self. request. recv (1024) recvd_size + = len (rdata) else: rdata = self. request. recv (self. filesize-recvd_size) recvd_size = self. filesize file. write (rdata) file. close () print 'receive done' # self. request. close () tcpServ = SocketServer. threadingTCPServer (ADDR, MyRequestHandler) print ('Waiting for connection... ') tcpServ. serve_forever ()
Sender:
#-*-Coding: UTF-8-*-import socket, OS, structs = socket. socket (socket. AF_INET, socket. SOCK_STREAM) s. connect ('2017. 168.50.74 ', 12307) while True: filepath = raw_input ('Please Enter chars: \ r \ n') if OS. path. isfile (filepath): fileinfo_size = struct. calcsize ('128sl ') # define packaging rules # define file header information, including file name and file size fhead = struct. pack ('128sl ', OS. path. basename (filepath), OS. stat (filepath ). st_size) s. send (fhead) print 'client filepath: ', filepath # with open (filepath, 'rb') as fo: there is a problem with sending the file, after sending, some items will be sent in the past fo = open (filepath, 'rb') while True: filedata = fo. read (1024) if not filedata: break s. send (filedata) fo. close () print 'send over... '# s. close ()
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.