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()

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM