這是本文件的舊版!
socket.send with Resource temporarily unavailable error
Problem
我的python程式如下,目標是建立一個socket server等待連線,並將檔案給送到socket client:
import socket import os import sys import stat from os.path import dirname, abspath, join def transmitDebugInfo(): filename = "/bootpart.gz" debugfile = None s = None conn = None try: debugfile = open(filename,'rb') blocksize = os.path.getsize(filename) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.settimeout(60) s.bind(('0.0.0.0', 5555)) s.listen(1) conn, addr = s.accept() data = debugfile.read() conn.send(data) except: print sys.exc_info()[0] print sys.exc_info()[1] finally: if s is not None: s.close() if conn is not None: conn.close() if debugfile is not None: debugfile.close() transmitDebugInfo()我的client為了方便測試,是直接使用linux的nc command:
nc 192.168.0.1 5555 > test.txt然而,在VMWare ESXI6.0 u2下會出現以下錯誤:
<class `socket.error`> [Errno 11] Resource temporarilty unavaliable
How to?
首先我發現接收到的檔案大小一定是33304 bytes,於是我加了以下程式做試驗:
print "bufsize = %s" % s.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF) s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 5000) print "bufsize = %s" % s.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)server程式輸出為:
bufsize = 32768 bufsize = 5000而client輸出檔案大小則變為5792 bytes。我猜想可能和發送buffer有關,於是找了一台ubuntu 14.04做測試;沒想到雖然buffer size為16384卻能正常的發送完檔案。因此我猜測可能和buffer釋放時間有關,於是改寫了以下發送方式:
while 1: data = debugfile.read(1024) if not data: break conn.send(data) time.sleep(0.001)以每次1024 bytes發送,每次中間等待0.001秒,如此就能順利完成發送。
Reference