我通過爲蟒SocketServer的文檔的例子讀數https://docs.python.org/2/library/socketserver.html爲什麼只有1024個字節中的SocketServer例如
讀爲什麼在線路self.request.recv(1024)
內側手柄方法指定爲1024的大小。如果客戶端發送的數據超過1024字節會發生什麼情況? 有一個循環讀取1024個字節,直到套接字爲空爲止更好嗎?我在這裏複製了示例:
import SocketServer
class MyTCPHandler(SocketServer.BaseRequestHandler):
"""
The RequestHandler class for our server.
It is instantiated once per connection to the server, and must
override the handle() method to implement communication to the
client.
"""
def handle(self):
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024).strip() # why only 1024 bytes ?
print "{} wrote:".format(self.client_address[0])
print self.data
# just send back the same data, but upper-cased
self.request.sendall(self.data.upper())
if __name__ == "__main__":
HOST, PORT = "localhost", 9999
# Create the server, binding to localhost on port 9999
server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
server.serve_forever()
如果發送的數量較大,它將被分解,在這種情況下,只有前1024個字節將被讀取,因爲沒有循環來讀取更多數據? –