使用 Simple Protocol
asyncio.BaseProtocol 類是asyncio模組中協議介面(protocol interface)的一個常見的基類。asyncio.Protocolclass 繼承自asyncio.BaseProtocol 並為stream protocols提供了一個介面。下面的代碼示範了asyncio.Protocol 介面的一個簡單實現,它的行為1就像一個echo server,同時,它還會在Python的控制台中輸出一些資訊。SimpleEchoProtocol 繼承自asyncio.Protocol,並且實現了3個方法:connection_made, data_received 以及 andconnection_lost:
import asyncio class SimpleEchoProtocol(asyncio.Protocol): def connection_made(self, transport): """ Called when a connection is made. The argument is the transport representing the pipe connection. To receive data, wait for data_received() calls. When the connection is closed, connection_lost() is called. """ print("Connection received!") self.transport = transport def data_received(self, data): """ Called when some data is received. The argument is a bytes object. """ print(data) self.transport.write(b'echo:') self.transport.write(data) def connection_lost(self, exc): """ Called when the connection is lost or closed. The argument is an exception object or None (the latter meaning a regular EOF is received or the connection was aborted or closed). """ print("Connection lost! Closing server...") server.close() loop = asyncio.get_event_loop()server = loop.run_until_complete(loop.create_server(SimpleEchoProtocol, 'localhost', 2222))loop.run_until_complete(server.wait_closed())
你可以通過運行一個telnet用戶端程式,並且串連到localhost的2222連接埠來測試這個echo server。如果你正在使用這個連接埠,你可以將這個連接埠號碼修改為任何其他可以使用的連接埠。如果你使用預設的值,你可以在Python的控制台中運行上面的代碼,之後在命令提示字元或終端中運行 telnet localhost 2222。你將會看到 Connection received! 的資訊顯示在Python的控制台中。接下來,你在telnet的控制台中輸入的任何字元都會以echo:跟上輸入的字元的形式展示出來,同時,在Python的控制台中會顯示出剛才新輸入的字元。當你退出telnet控制台時,你會看到Connection lost! Closing server... 的資訊展示在Python的控制台中。
舉個例子,如果你在開啟telnet之後輸入 abc,你將會在telnet的視窗中看到下面的訊息:
此外,在Python的控制台中會顯示下面的訊息:
Connection received! b'a' b'b' b'c' Connection lost! Closing server...
在建立了一個名為loop的事件迴圈之後,代碼將會調用loop.run_until_complete來運行loop.create_server這個協程(coroutine)。這個協程建立了一個TCP伺服器並使用protocol的工廠類綁定到指定主機的指定連接埠(在這個例子中是localhost上的2222連接埠,使用的工廠類是SimpleEchoProtocol)並返回一個Server的對象,以便用來停止服務。代碼將這個執行個體賦值給server變數。用這種方式,當建立一個用戶端串連時,會建立一個新的SimpleEchoProtocol的執行個體並且該類中的方法會被執行。
當成功的建立了一個串連之後,connection_made 方法裡面的代碼輸出了一條訊息,並將收到的內容作為一個參數賦值給transport成員變數,以便稍後在另一個方法中使用。
當收到了傳來的資料時,data_received方面裡面的代碼會將收到的資料位元組輸出,並且通過調用兩次self.transport.write 方法將echo: 和收到資料發送給用戶端。當然了,也可以只調用一次self.transport.write將所有的資料返回,但是我想更清楚的將發送echo:的代碼和發送收到的資料的代碼區分開來。
當串連關掉或者斷開時,connection_lost方法中的代碼將會輸出一條訊息,並且調用server.close();此時,那個在伺服器關閉前一直啟動並執行迴圈停止了運行。
使用 Clients and Servers
在上面的例子中,telnet是一個用戶端。asyncio模組提供了一個協程方便你很容易的使用stream reader 和 writer來編寫服務端和用戶端。下面的代碼示範了一個簡單的echo server,該server監聽localhost上的2222連接埠。你可以在Python的控制台中運行下面的代碼,之後在另一個Python的控制台中運行用戶端的代碼作為用戶端。
import asyncio @asyncio.coroutinedef simple_echo_server(): # Start a socket server, call back for each client connected. # The client_connected_handler coroutine will be automatically converted to a Task yield from asyncio.start_server(client_connected_handler, 'localhost', 2222) @asyncio.coroutinedef client_connected_handler(client_reader, client_writer): # Runs for each client connected # client_reader is a StreamReader object # client_writer is a StreamWriter object print("Connection received!") while True: data = yield from client_reader.read(8192) if not data: break print(data) client_writer.write(data) loop = asyncio.get_event_loop()loop.run_until_complete(simple_echo_server())try: loop.run_forever()finally: loop.close()
下面的代碼示範了一個用戶端程式串連了localhost上的2222連接埠,並且使用asyncio.StreamWriter對象寫了幾行資料,之後使用asyncio.StreamWriter對象讀取服務端返回的資料。
import asyncio LASTLINE = b'Last line.\n' @asyncio.coroutine def simple_echo_client(): # Open a connection and write a few lines by using the StreamWriter object reader, writer = yield from asyncio.open_connection('localhost', 2222) # reader is a StreamReader object # writer is a StreamWriter object writer.write(b'First line.\n') writer.write(b'Second line.\n') writer.write(b'Third line.\n') writer.write(LASTLINE) # Now, read a few lines by using the StreamReader object print("Lines received") while True: line = yield from reader.readline() print(line) if line == LASTLINE or not line: break writer.close() loop = asyncio.get_event_loop()loop.run_until_complete(simple_echo_client())
你可以在不同的Python控制台中執行用戶端的代碼。如果服務端正在運行,控制台中會輸出下面的內容:
Lines receivedb'First line.\n'b'Second line.\n'b'Third line.\n'b'Last line.\n'
執行服務端代碼的Python控制台會顯示下面的內容:
Connection received! b'First line.\nSecond line.\nThird line.\nLast line.\n'
首先,讓我們關注一下服務端的代碼。在建立完一個叫loop的事件迴圈之後,代碼會調用loop.run_until_complete來運行這個simple_echo_server協程。該協程調用asyncio.start_server協程來開啟一個socket伺服器,綁定到指定的主機和連接埠號碼,之後,對每一個用戶端串連執行作為參數傳入的回呼函數——client_connected_handler。在這個例子中,client_connected_handler是另一個協程,並且不會被自動的轉換為一個Task。除了協程(coroutine)之外,你可以指定一個普通的回呼函數。