日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

探索Python3.4中新引入的asyncio模塊

系統(tǒng) 1699 0

使用 Simple Protocol

asyncio.BaseProtocol 類是asyncio模塊中協(xié)議接口(protocol interface)的一個常見的基類。asyncio.Protocolclass 繼承自asyncio.BaseProtocol 并為stream protocols提供了一個接口。下面的代碼演示了asyncio.Protocol 接口的一個簡單實(shí)現(xiàn),它的行為1就像一個echo server,同時,它還會在Python的控制臺中輸出一些信息。SimpleEchoProtocol 繼承自asyncio.Protocol,并且實(shí)現(xiàn)了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())


          

你可以通過運(yùn)行一個telnet客戶端程序,并且連接到localhost的2222端口來測試這個echo server。如果你正在使用這個端口,你可以將這個端口號修改為任何其他可以使用的端口。如果你使用默認(rèn)的值,你可以在Python的控制臺中運(yùn)行上面的代碼,之后在命令提示符或終端中運(yùn)行 telnet localhost 2222。你將會看到 Connection received! 的信息顯示在Python的控制臺中。接下來,你在telnet的控制臺中輸入的任何字符都會以echo:跟上輸入的字符的形式展示出來,同時,在Python的控制臺中會顯示出剛才新輸入的字符。當(dāng)你退出telnet控制臺時,你會看到Connection lost! Closing server... 的信息展示在Python的控制臺中。

舉個例子,如果你在開啟telnet之后輸入 abc,你將會在telnet的窗口中看到下面的消息:

            
 echo:abecho:bcecho:c


          

此外,在Python的控制臺中會顯示下面的消息:

            
 Connection received!
 b'a'
 b'b'
 b'c'
 Connection lost! Closing server...


          

在創(chuàng)建了一個名為loop的事件循環(huán)之后,代碼將會調(diào)用loop.run_until_complete來運(yùn)行l(wèi)oop.create_server這個協(xié)程(coroutine)。這個協(xié)程創(chuàng)建了一個TCP服務(wù)器并使用protocol的工廠類綁定到指定主機(jī)的指定端口(在這個例子中是localhost上的2222端口,使用的工廠類是SimpleEchoProtocol)并返回一個Server的對象,以便用來停止服務(wù)。代碼將這個實(shí)例賦值給server變量。用這種方式,當(dāng)建立一個客戶端連接時,會創(chuàng)建一個新的SimpleEchoProtocol的實(shí)例并且該類中的方法會被執(zhí)行。

當(dāng)成功的創(chuàng)建了一個連接之后,connection_made 方法里面的代碼輸出了一條消息,并將收到的內(nèi)容作為一個參數(shù)賦值給transport成員變量,以便稍后在另一個方法中使用。

當(dāng)收到了傳來的數(shù)據(jù)時,data_received方面里面的代碼會將收到的數(shù)據(jù)字節(jié)輸出,并且通過調(diào)用兩次self.transport.write 方法將echo: 和收到數(shù)據(jù)發(fā)送給客戶端。當(dāng)然了,也可以只調(diào)用一次self.transport.write將所有的數(shù)據(jù)返回,但是我想更清楚的將發(fā)送echo:的代碼和發(fā)送收到的數(shù)據(jù)的代碼區(qū)分開來。

當(dāng)連接關(guān)掉或者斷開時,connection_lost方法中的代碼將會輸出一條消息,并且調(diào)用server.close();此時,那個在服務(wù)器關(guān)閉前一直運(yùn)行的循環(huán)停止了運(yùn)行。
使用 Clients and Servers

在上面的例子中,telnet是一個客戶端。asyncio模塊提供了一個協(xié)程方便你很容易的使用stream reader 和 writer來編寫服務(wù)端和客戶端。下面的代碼演示了一個簡單的echo server,該server監(jiān)聽localhost上的2222端口。你可以在Python的控制臺中運(yùn)行下面的代碼,之后在另一個Python的控制臺中運(yùn)行客戶端的代碼作為客戶端。

            
import asyncio
 
@asyncio.coroutine
def 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.coroutine
def 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對象寫了幾行數(shù)據(jù),之后使用asyncio.StreamWriter對象讀取服務(wù)端返回的數(shù)據(jù)。
?

            
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控制臺中執(zhí)行客戶端的代碼。如果服務(wù)端正在運(yùn)行,控制臺中會輸出下面的內(nèi)容:

            
Lines received
b'First line.\n'
b'Second line.\n'
b'Third line.\n'
b'Last line.\n'


          

執(zhí)行服務(wù)端代碼的Python控制臺會顯示下面的內(nèi)容:

            
 Connection received!
 b'First line.\nSecond line.\nThird line.\nLast line.\n'


          

首先,讓我們關(guān)注一下服務(wù)端的代碼。在創(chuàng)建完一個叫l(wèi)oop的事件循環(huán)之后,代碼會調(diào)用loop.run_until_complete來運(yùn)行這個simple_echo_server協(xié)程。該協(xié)程調(diào)用asyncio.start_server協(xié)程來開啟一個socket服務(wù)器,綁定到指定的主機(jī)和端口號,之后,對每一個客戶端連接執(zhí)行作為參數(shù)傳入的回調(diào)函數(shù)――client_connected_handler。在這個例子中,client_connected_handler是另一個協(xié)程,并且不會被自動的轉(zhuǎn)換為一個Task。除了協(xié)程(coroutine)之外,你可以指定一個普通的回調(diào)函數(shù)。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 东台市| 宜州市| 惠水县| 得荣县| 阿合奇县| 英吉沙县| 丰顺县| 龙川县| 额济纳旗| 舞钢市| 磐石市| 泾川县| 正阳县| 门源| 洛扎县| 松阳县| 维西| 南阳市| 武威市| 兴国县| 西昌市| 陇西县| 嘉峪关市| 温宿县| 通化市| 崇文区| 平潭县| 宁乡县| 鹤峰县| 龙州县| 高青县| 福建省| 安丘市| 乌鲁木齐市| 永胜县| 华蓥市| 天峨县| 巴楚县| 龙海市| 博乐市| 太和县|