Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to receive and send TCP Files with python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report