Python Socket傳輸文件示例
發(fā)送端可以不停的發(fā)送新文件,接收端可以不停的接收新文件。
例如:發(fā)送端輸入:e:\visio.rar,接收端會默認保存為 e:\new_visio.rar,支持多并發(fā),具體實現(xiàn)如下;
接收端:
方法一:
#-*- coding: UTF-8 -*-
import socket,time,SocketServer,struct,os,thread
host='192.168.50.74'
port=12307
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) #定義socket類型
s.bind((host,port)) #綁定需要監(jiān)聽的Ip和端口號,tuple格式
s.listen(1)
def conn_thread(connection,address):
while True:
try:
connection.settimeout(600)
fileinfo_size=struct.calcsize('128sl')
buf = connection.recv(fileinfo_size)
if buf: #如果不加這個if,第一個文件傳輸完成后會自動走到下一句
filename,filesize =struct.unpack('128sl',buf)
filename_f = filename.strip('\00')
filenewname = os.path.join('e:\\',('new_'+ filename_f))
print 'file new name is %s, filesize is %s' %(filenewname,filesize)
recvd_size = 0 #定義接收了的文件大小
file = open(filenewname,'wb')
print 'stat receiving...'
while not recvd_size == filesize:
if filesize - recvd_size > 1024:
rdata = connection.recv(1024)
recvd_size += len(rdata)
else:
rdata = connection.recv(filesize - recvd_size)
recvd_size = filesize
file.write(rdata)
file.close()
print 'receive done'
#connection.close()
except socket.timeout:
connection.close()
while True:
connection,address=s.accept()
print('Connected by ',address)
#thread = threading.Thread(target=conn_thread,args=(connection,address)) #使用threading也可以
#thread.start()
thread.start_new_thread(conn_thread,(connection,address))
s.close()
方法二:
#-*- coding: UTF-8 -*-
import socket,time,SocketServer,struct,os
host='192.168.50.74'
port=12307
ADDR=(host,port)
class MyRequestHandler(SocketServer.BaseRequestHandler):
def handle(self):
print('connected from:', self.client_address)
while True:
fileinfo_size=struct.calcsize('128sl') #定義文件信息。128s表示文件名為128bytes長,l表示一個int或log文件類型,在此為文件大小
self.buf = self.request.recv(fileinfo_size)
if self.buf: #如果不加這個if,第一個文件傳輸完成后會自動走到下一句
self.filename,self.filesize =struct.unpack('128sl',self.buf) #根據(jù)128sl解包文件信息,與client端的打包規(guī)則相同
print 'filesize is: ',self.filesize,'filename size is: ',len(self.filename) #文件名長度為128,大于文件名實際長度
self.filenewname = os.path.join('e:\\',('new_'+ self.filename).strip('\00')) #使用strip()刪除打包時附加的多余空字符
print self.filenewname,type(self.filenewname)
recvd_size = 0 #定義接收了的文件大小
file = open(self.filenewname,'wb')
print 'stat receiving...'
while not recvd_size == self.filesize:
if self.filesize - recvd_size > 1024:
rdata = self.request.recv(1024)
recvd_size += len(rdata)
else:
rdata = self.request.recv(self.filesize - recvd_size)
recvd_size = self.filesize
file.write(rdata)
file.close()
print 'receive done'
#self.request.close()
tcpServ = SocketServer.ThreadingTCPServer(ADDR, MyRequestHandler)
print('waiting for connection...' )
tcpServ.serve_forever()
發(fā)送端:
#-*- coding: UTF-8 -*-
import socket,os,struct
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('192.168.50.74',12307))
while True:
filepath = raw_input('Please Enter chars:\r\n')
if os.path.isfile(filepath):
fileinfo_size=struct.calcsize('128sl') #定義打包規(guī)則
#定義文件頭信息,包含文件名和文件大小
fhead = struct.pack('128sl',os.path.basename(filepath),os.stat(filepath).st_size)
s.send(fhead)
print 'client filepath: ',filepath
# with open(filepath,'rb') as fo: 這樣發(fā)送文件有問題,發(fā)送完成后還會發(fā)一些東西過去
fo = open(filepath,'rb')
while True:
filedata = fo.read(1024)
if not filedata:
break
s.send(filedata)
fo.close()
print 'send over...'
#s.close()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python3使用TCP編寫一個簡易的文件下載器功能
- python:socket傳輸大文件示例
- python 通過 socket 發(fā)送文件的實例代碼
- 基于python3實現(xiàn)socket文件傳輸和校驗
- python 使用socket傳輸圖片視頻等文件的實現(xiàn)方式
- 樹莓派采用socket方式文件傳輸(python)
- 使用python socket分發(fā)大文件的實現(xiàn)方法
- Python socket模塊ftp傳輸文件過程解析
- python socket 聊天室實例代碼詳解
- Python實現(xiàn)socket非阻塞通訊功能示例
- Python socket實現(xiàn)的文件下載器功能示例
相關(guān)文章
解決Python運行文件出現(xiàn)out of memory框的問題
今天小編就為大家分享一篇解決Python運行文件出現(xiàn)out of memory框的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Python機器學(xué)習(xí)之K-Means聚類實現(xiàn)詳解
這篇文章主要為大家詳細介紹了Python機器學(xué)習(xí)之K-Means聚類的實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02
Windows系統(tǒng)配置python腳本開機啟動的3種方法分享
這篇文章主要介紹了Windows系統(tǒng)配置python腳本開機啟動的3種方法分享,本文講解了開始菜單啟動項實現(xiàn)、開機腳本、通過一個服務(wù)調(diào)用該腳本三種方法,需要的朋友可以參考下2015-03-03
基于循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的古詩生成器
這篇文章主要為大家詳細介紹了基于循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的古詩生成器,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03
Python數(shù)據(jù)容器dict(字典)的實現(xiàn)
本文主要介紹了Python數(shù)據(jù)容器dict(字典)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
python 實現(xiàn)多線程的三種方法總結(jié)
這篇文章主要介紹了python 實現(xiàn)多線程的三種方法總結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04
Python?Dash框架在數(shù)據(jù)可視化儀表板中的應(yīng)用與實踐記錄
Python的Plotly?Dash庫提供了一種簡便且強大的方式來構(gòu)建和展示互動式數(shù)據(jù)儀表板,本篇文章將深入探討如何使用Dash設(shè)計一個互動數(shù)據(jù)儀表板,并通過代碼示例幫助讀者理解如何實現(xiàn)這一過程,感興趣的朋友一起看看吧2025-03-03

