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