最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python Socket傳輸文件示例

 更新時間:2017年01月16日 16:02:02   作者:愛做夢的魚  
這篇文章主要介紹了Python Socket傳輸文件示例,發(fā)送端可以不停的發(fā)送新文件,接收端可以不停的接收新文件。有興趣的可以了解一下。

發(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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

义马市| 腾冲县| 浦北县| 唐海县| 手机| 桂林市| 枞阳县| 翁牛特旗| 普洱| 昆明市| 海林市| 临沭县| 太白县| 连城县| 林州市| 广安市| 青河县| 普陀区| 桓台县| 桦甸市| 安徽省| 西乌珠穆沁旗| 土默特右旗| 平远县| 甘德县| 玛纳斯县| 三河市| 永济市| 扎兰屯市| SHOW| 张掖市| 南郑县| 崇左市| 大名县| 渝中区| 湟中县| 潍坊市| 商南县| 交口县| 新丰县| 兰州市|