python USES tcp to realize file transfer in LAN

  • 2020-11-20 06:10:04
  • OfStack

This paper shares the specific code of python using tcp to realize file transfer in LAN for your reference. The specific content is as follows

Function:

You can use the TCP client created by python to download files from the TCP server we built ourselves.

Implementation requirements:

Install the socket module
Simple understanding of sokcet module usage

The server code is as follows:


import socket

def file_deal(file_name):
 #  Define functions to handle files that the user asks to download 
 try:
 # 2 Read in base mode 
 files = open(file_name, "rb")

 mes = files.read()

 except:


 print(" There is no such file ")

 else:

 files.close()

 return mes

def main():
 #  Create the socket 
 tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 #  Fixed port number 
 tcp_socket.bind(("",8888))
 #  Convert active socket to passive socket 
 tcp_socket.listen(128)

 while True:
 #  using accept Gets the address of the socket and the client 
 client_socket,client_addr = tcp_socket.accept()
 #  Receive data from the client 
 file_name = client_socket.recv(4096)
 #  Call the function to process the file downloaded by the user 
 mes = file_deal(file_name)

 if mes:
  #  If the file is not sent empty 

  client_socket.send(mes)
 # Close the socket 

 client_socket.close()

if __name__ == "__main__":
 main()

import socket


def file_deal(file_name):
 #  Define functions to handle files that the user asks to download 
 try:
 # 2 Read in base mode 
 files = open(file_name, "rb")

 mes = files.read()

 except:


 print(" There is no such file ")

 else:

 files.close()

 return mes

def main():
 #  Create the socket 
 tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 #  Fixed port number 
 tcp_socket.bind(("",8888))
 #  Convert active socket to passive socket 
 tcp_socket.listen(128)

 while True:
 #  using accept Gets the address of the socket and the client 
 client_socket,client_addr = tcp_socket.accept()
 #  Receive data from the client 
 file_name = client_socket.recv(4096)
 #  Call the function to process the file downloaded by the user 
 mes = file_deal(file_name)

 if mes:
  #  If the file is not sent empty 

  client_socket.send(mes)
 # Close the socket 

 client_socket.close()


if __name__ == "__main__":
 main()

Client code:


from socket import *
import os

def main():
 #  Set up the socket 
 tcp_socket = socket(AF_INET, SOCK_STREAM)
 #  Receive input from the server side ip And port 
 tcp_ip = input(" Please enter the ip:")

 tcp_port = int(input(" Please enter port :"))
 #  Connect to server 
 tcp_socket.connect((tcp_ip, tcp_port))
 #  Enter the file name to download 
 file_name = input(" Please enter the file name you want to download :")

 #  Send the file name to the server 
 tcp_socket.send(file_name.encode())
 #  create 1 An empty file 
 new_file = open(file_name, "wb")
 #  USES and calculates the number of bytes read 
 time = 0

 while True:
 #  Receive the content returned from the server side 
 mes = tcp_socket.recv(4096)
 #  If the content is not executed for empty 
 if mes:
  #  Decode and write to the file 
  new_file.write(mes.decode())

  #  Calculation of bytes 
  time += len(mes)

 else:
  #  If the number of bytes is empty, nothing is received 
  if time == 0:
  #  Close the file 
  new_file.close()
  #  Delete the file you just created 
  os.remove(file_name)
  print(" There are no files you want to download ")
  else:
  #  if time Have a value name File transfer completed 
  print(" File download successful ")

  break
 #  Close the socket 
 tcp_socket.close()


if __name__ == '__main__':
main()

Related articles: