Python 实现文件,视频的传输

# server 

import socket
import json

sk = socket.socket()
sk.bind(('127.0.0.1',9001))
sk.listen()
conn,addr = sk.accept()

json_dic = conn.recv(1024).decode('utf-8')
dic = json.loads(json_dic)
file_path = 'F:'+'\' + dic['file_name']

with open(file_path,'wb') as f:
while dic['file_size'] > 0:
file_conet = conn.recv(1024)
dic['file_size'] -= len(file_conet)
f.write(file_conet)

conn.close()
sk.close()
# client
import socket
import os
import json

sk = socket.socket()
sk.connect(('127.0.0.1',9001))

file = r'F:Nginx核心知识100讲视频155_基于OpenResty的WAF防火墙.mp4'
file_size = os.path.getsize(file)
file_name = os.path.basename(file)
dic = {'file_name':file_name,'file_size':file_size}

json_dic = json.dumps(dic)
sk.send(json_dic.encode('utf-8'))

with open(file,'rb') as f:
    conntent = f.read()
    sk.send(conntent)

sk.close()
原文地址:https://www.cnblogs.com/lxc123/p/12528765.html