Implementation method of distributing large files using python socket

  • 2021-07-10 20:23:12
  • OfStack

Thoughts:

In the process of using socket to transfer files, if a single transfer can only send one part of data at a time, if it is for large files, one transfer is definitely not possible, so we need to send the size of the transmission content to the client in advance when transmitting, and receive the data cyclically at the client.

Code section:

1. Server


#!/usr/bin/env python
#-*- coding:utf-8 -*-
#environment : 2.7

import os,json,time,socket
sk = socket.socket()
ip_port=('127.0.0.1',9600)
sk.bind(ip_port)
sk.listen(5)
file_path = '/root/update/123.txt'
file_name = file_path.rsplit(os.sep,1)[1] 
file_size = os.path.getsize(file_path)  # Get the file content size 
# Header information sent to the client 
header_data = {
  'file_name': file_name,
  'file_size': file_size,
  'date': time.strftime('%Y-%m-%d %X',time.localtime()),
  'charset': 'utf-8'
}
while True:
  conn, addr = sk.accept()
  print("%s:%s is connect"% addr)
  request_data = conn.recv(1024)
  print(request_data)
  #  Send the header contents 
  conn.send(json.dumps(header_data))
  request_data1 = conn.recv(1024)
  print(request_data1)
  f = open(file_path,'r')
  content = f.read()
  # Send file contents 
  conn.sendall(content)
  conn.close()

2. Client


#!/usr/bin/env python
#-*- coding:utf-8 -*-
#environment : 2.7

import socket,json
while True:
  sk = socket.socket()
  ip_port = ('127.0.0.1',9600)
  sk.connect(ip_port)
  user_input = raw_input('>>>:').strip()
  if len(user_input) == 0:continue
  if user_input == 'q':break
  sk.send(user_input)
  server_head_msg = json.loads(sk.recv(1024))
  print(server_head_msg)
  # Filename res_name , file size res_size
  res_name = server_head_msg['file_name']
  res_size = server_head_msg['file_size']
  sk.send(' Header information has been received , You can send data now ')
  # The following is the part that loops in to receive the contents of the file 
  num = res_size/1024.0
  if num != int(num):
    num = int(num) +1
  else:
    num = int(num)
  for i in range(num):
    content = sk.recv(1024)
    print(content)

Remarks: The files sent from the receiving server are also considered according to the size of the data received each time. The general idea is as follows


res = ''
recive_size = 0
while recive_size <= res_size:
  data = sk.recv(1024)
  recive_size += len(data)
  res += data
else:
  print(res)
  print('done'.center(40,'='))


Related articles: