這聽起來像你的客戶端發送的文件,然後等待服務器的響應,但如果你不給服務器上的指示,它已經完全讀取文件,的recv()服務器端將掛起等待更多數據。一旦客戶端完成發送,您可以在客戶端調用shutdown(SHUT_WR)。這會通知服務器,一旦它讀取了所有發送的數據,就再也沒有數據了。
一個非常基本的例子(發送一個數據blob到服務器,並且響應接收到一個數據blob):
服務器
>>> from socket import *
>>> s=socket()
>>> s.bind(('',8000))
>>> s.listen(1)
>>> c,a = s.accept() # will hang here until a client connects
>>> recvd=''
>>> while True:
... data = c.recv(1024)
... if not data: break # recv() will return '' only if the client calls shutdown(SHUT_WR) or close()
... recvd += data
...
>>> recvd
'a message'
>>> c.sendall('a response')
>>> c.close() # done with client, could loop back to accept here
>>> s.close() # done with server
客戶
>>> from socket import *
>>> s=socket()
>>> s.connect(('localhost',8000))
>>> s.sendall('a message')
>>> s.shutdown(SHUT_WR) # tells server you're done sending, so recv won't wait for more
>>> recvd=''
>>> while True:
... data = s.recv(1024)
... if not data: break
... recvd += data
...
>>> recvd
'a response'
>>> s.close()
郵一些代碼請? – 2010-05-26 16:50:24