Get the App
SLTechnology News&Howtos  ›  Development  › 

How to receive and send TCP Files with python

Shulou Source: shulou.com Published: 2022-06-03 19:38:51 11月12日 Update

This article mainly explains "how to receive and send TCP files by python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to receive and send TCP files by python".

1. Send

Modify according to your own needs and complete the sending and receiving of files under a simple local area network

Client

# transfer data from the client to the server File import socketimport tqdmimport osdef send (filename): # transfer data spacer SEPARATOR =''# server information host = '127.0.0.1' port = 1234 # file buffer Buffersize = 4096 buffer 10 # transfer file name filename = filename # file size file_size = os.path.getsize (filename) # create socket link s = Socket.socket () print ({host}: {port}') s.connect ((host) in server connection Port)) print ('connected successfully with the server') # send file name and file size Must encode s.send (f'{filename} {file_size} '.encode ()) # File transfer progress = tqdm.tqdm (range (file_size), f' send {filename}', unit='B', unit_divisor=1024) with open (filename) 'rb') as f: # read the file for _ in progress: bytes_read = f.read (Buffersize) if not bytes_read: break # sendall to ensure that the network is busy Data can still be transferred s.sendall (bytes_read) progress.update (len (bytes_read)) # close resource s.close () if _ _ name__ = ='_ _ main__': filename = input ('Please enter file name:') send (filename) 2, receive

Server side

Import socketimport tqdmimport osimport threadingdef received (): # set the server's ip and port # server information sever_host = '127.0.0.1' sever_port = 1234 # transfer data spacer SEPARATOR ='# file buffer Buffersize = 4096room10s = socket.socket () s.bind ((sever_host) Sever_port) # set the number of listeners s.listen (128) print (f 'server listens for {sever_host}: {sever_port}') # receive client connection client_socket, address = s.accept () # print client ip print (f 'client {address} connection') # receive client information received = client_socket.recv (Buffersize) .decode () filename File_size = received.split (SEPARATOR) # get the name of the file, size filename = os.path.basename (filename) file_size = int (file_size) # File receive processing progress = tqdm.tqdm (range (file_size), f' receive {filename}', unit='B', unit_divisor=1024, unit_scale=True) with open ('8_18_'+filename 'wb') as f: for _ in progress: # read data from the client bytes_read = client_socket.recv (Buffersize) # if there is no data transfer content if not bytes_read: break # read and write f.write (bytes_read) # Update the progress bar progress.update (len (bytes_read)) # close the resource client_socket.close () s.close () if _ _ name__ = ='_ _ main__': received () so far I believe that the "python how to achieve TCP file receiving and sending" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: File server service client client data transfer message content name size buffer resource processing learning monitoring buffering utility deeper success Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info macOS MariaDB Microsoft NVidia