欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python:socket傳輸大文件示例

 更新時間:2017年01月18日 10:47:14   作者:夏夜晚風(fēng)  
本篇文章主要介紹了python:socket傳輸大文件示例,具有一定的參考價值,有興趣的可以了解一下,

文件可以傳輸,但是對比傳輸前后的文件:socket_test.txt,末尾有一些不一致服務(wù)端代碼:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
import time
'''
等待連接
等待發(fā)送文件
讀取數(shù)據(jù)
寫入文件并且保存
等待連接
'''
import socket
import threading
import time
import struct


def function(newsock, address):
  FILEINFO_SIZE = struct.calcsize('128sI')
  '''定義文件信息(包含文件名和文件大?。┐笮 ?28s代表128個char[](文件名),I代表一個integer or long(文件大?。?''
  while 1:
    try:
      fhead = newsock.recv(FILEINFO_SIZE)
      filename, filesize = struct.unpack('128sI', fhead)
      '''把接收到的數(shù)據(jù)庫進行解包,按照打包規(guī)則128sI'''
      print "address is: ", address
      print filename, len(filename), type(filename)
      print filesize
      #filename = 'new_'+filename.strip('\00') # 命名新文件new_傳送的文件
      filename = filename.strip('\00')
      fp = open(filename, 'wb') # 新建文件,并且準(zhǔn)備寫入
      restsize = filesize
      print "recving..."
      while 1:
        if restsize > 102400: # 如果剩余數(shù)據(jù)包大于1024,就去1024的數(shù)據(jù)包
          filedata = newsock.recv(10240)
        else:
          filedata = newsock.recv(restsize)
          fp.write(filedata)
          #break
        if not filedata:
          break
        fp.write(filedata)
        restsize = restsize - len(filedata) # 計算剩余數(shù)據(jù)包大小
        if restsize <= 0:
          break
      fp.close()
      print "recv succeeded !!File named:", filename
    except Exception, e:
      print unicode(e).encode('gbk')
      print "the socket partner maybe closed"
      newsock.close()
      break
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 創(chuàng)建tcp連接
sock.bind(('10.240.146.82', 8887)) # 定于端口和ip
sock.listen(5) # 監(jiān)聽
while True:
  newsock, address = sock.accept()
  print "accept another connection"
  tmpThread = threading.Thread(target=function, args=(newsock, address)) # 如果接收到文件,創(chuàng)建線程
  tmpThread.start() # 執(zhí)行線程
print 'end'

客戶端代碼:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
'''
輸入文件名,并且上傳
'''
import socket
import time
import struct
import os
f = open('socket_test.txt', 'wb')

for i in range(1000000):
  f.write('for socket test, the line number is : ' + str(i) + '\n')

f.close()

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(50)
e = 0
try:
  sock.connect(('10.240.146.82', 8887))
  print 'connect...'
except socket.timeout, e:
  print 'timeout', e
except socket.error, e:
  print 'error', e
except e:
  print 'any', e
if not e:
  #while (1):
    #filename = raw_input('input your filename------->') # 輸入文件名
  filename = 'socket_test.txt'
  FILEINFO_SIZE = struct.calcsize('128sI') # 編碼格式大小
  fhead = struct.pack('128sI', filename, os.stat(filename).st_size) # 按照規(guī)則進行打包
  sock.send(fhead) # 發(fā)送文件基本信息數(shù)據(jù)
  fp = open(filename, 'rb')
  fp2 = open('local_test.txt','wb')
  i = 0
  while 1: # 發(fā)送文件
    filedata = fp.read(10240)
    if not filedata:
      break
    sock.sendall(filedata)
    fp2.write(filedata)
    print i
    i = i + 1
  print "sending over..."
  fp.close()
  fp2.close()

 以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python網(wǎng)絡(luò)通信圖文詳解

    python網(wǎng)絡(luò)通信圖文詳解

    這篇文章主要介紹了Python網(wǎng)絡(luò)編程詳解,涉及具體代碼示例,還是挺不錯的,這里分享給大家,供需要的朋友參考,希望能給你帶來幫助
    2021-08-08
  • 使用python計算方差方式——pandas.series.std()

    使用python計算方差方式——pandas.series.std()

    這篇文章主要介紹了使用python計算方差方式——pandas.series.std(),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • pandas庫中to_datetime()方法的使用解析

    pandas庫中to_datetime()方法的使用解析

    這篇文章主要介紹了pandas庫中to_datetime()方法的使用解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • 詳細介紹pandas的DataFrame的append方法使用

    詳細介紹pandas的DataFrame的append方法使用

    這篇文章主要介紹了詳細介紹pandas的DataFrame的append方法使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Python字典的基本用法實例分析【創(chuàng)建、增加、獲取、修改、刪除】

    Python字典的基本用法實例分析【創(chuàng)建、增加、獲取、修改、刪除】

    這篇文章主要介紹了Python字典的基本用法,結(jié)合具體實例形式分析了Python字典的創(chuàng)建、增加、獲取、修改、刪除等基本操作技巧與注意事項,需要的朋友可以參考下
    2019-03-03
  • 在python中利用GDAL對tif文件進行讀寫的方法

    在python中利用GDAL對tif文件進行讀寫的方法

    今天小編就為大家分享一篇在python中利用GDAL對tif文件進行讀寫的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • python安裝cx_Oracle模塊常見問題與解決方法

    python安裝cx_Oracle模塊常見問題與解決方法

    這篇文章主要介紹了python安裝cx_Oracle模塊常見問題與解決方法,舉例分析了Python在Windows平臺與Linux平臺安裝cx_Oracle模塊常見問題、解決方法及相關(guān)注意事項,需要的朋友可以參考下
    2017-02-02
  • python 如何在 Matplotlib 中繪制垂直線

    python 如何在 Matplotlib 中繪制垂直線

    這篇文章主要介紹了python 如何在 Matplotlib 中繪制垂直線,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-04-04
  • python speech模塊的使用方法

    python speech模塊的使用方法

    這篇文章主要介紹了python speech模塊的使用方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • 學(xué)python安裝的軟件總結(jié)

    學(xué)python安裝的軟件總結(jié)

    在本篇文章里小編給大家整理了是關(guān)于學(xué)python安裝什么軟件的相關(guān)知識點內(nèi)容,有需要的朋友們學(xué)習(xí)下。
    2019-10-10

最新評論