Python implements TCP file transfer

I have already talked about the python implementation code of large file transmission under the UDP protocol a few days ago, and today I will implement the python implementation code of large file transmission under the TCP protocol.
The realization process of TCP and UDP is quite different.

**Implementation code: **

Server:

import socket
import time

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind(('127.0.0.1',9999))
s.listen(5)print('Waiting for connection...')while True:
 sock,addr = s.accept()print('Accept new connection from %s:%s...'% addr)if count ==0:
 data1 = sock.recv(1024)print(str(data1))
 file_total_size =int(data1.decode())
 received_size =0
 sock.send('received'.encode())
 data = sock.recv(1024)
 filepath =str(data.decode())
 f =open(filepath,'wb')while received_size < file_total_size:
 data = sock.recv(1024)
 f.write(data)
 received_size +=len(data)print('Received',received_size,' Byte')
 data = sock.recv(1024)if data == b'end':break
  
f.close()
s.close()

Client:

import socket
import os
import time
filename =input('please enter the filename you want to send:\n')
filesize =str(os.path.getsize(filename))
fname1, fname2 = os.path.split(filename)
client_addr =('127.0.0.1',9999)
f =open(filename,'rb')
count =0
flag =1
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# establish connection:
s.connect(('127.0.0.1',9999))while True:if count ==0:
 s.send(filesize.encode())
 start = time.time()
 s.recv(1024)
 s.send(fname2.encode())for line in f:
 s.send(line)print('sending...')
 s.send(b'end')break

s.close
end = time.time()print('cost'+str(round(end - start,2))+'s')

Run screenshot:

Service-Terminal:

Client:

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Python implements TCP file transfer
Python implements ftp file transfer function
Python file operation
python_ file processing
Python implements Super Mario
Python implements tic-tac-toe game
Python implements man-machine gobang
Python implements Tetris game
Python implements image stitching
Python implements minesweeper game
Python implements scanning tools
Python implements threshold regression
Python implements minesweeper games
Python implements electronic dictionary
Python implements guessing game
Python implements simple tank battle
Python implements udp chat window
Python TCP packet injection method
Python implements WeChat airplane game
Python implements word guessing game
Python implements a guessing game
Python implements parking management system
Python numpy implements rolling case
OpenCV Python implements puzzle games
Python implements simple tic-tac-toe game
Python implements password strength verification
Python implements car management system
Python tornado upload file function
Python implements code block folding
Python implements panoramic image stitching
Python implements SMTP mail sending
Python implements multi-dimensional array sorting
How Python implements FTP function
Python implements mean-shift clustering algorithm
Python implements verification code recognition
Python implements gradient descent method
Python implements text version minesweeper
Quick start Python file operation
Python implements image stitching function
Python implements the brick-and-mortar game
Python file read and write operations
How Python implements the mail function
How to write python configuration file
Python3 implements the singleton design pattern
Python implements exchange rate conversion operations
Python implements string and number splicing
Python file operation basic process analysis
Python implements ten classic sorting algorithms
Python implements a universal web framework
Python implements 126 mailboxes to send mail
Python implements AI face change function
Python implements the steepest descent method
Python implements the actual banking system
Python implements digital bomb game program
Python implements username and password verification
How Python operates on file directories
How Python implements the timer function
Python implements the aircraft war project
Python tcp transmission code example analysis
Python implements horizontal stitching of pictures
Python implements GIF graph upside down