最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python中asyncore異步模塊的實現(xiàn)

 更新時間:2023年01月18日 10:14:11   作者:TypicalSpider  
本文主要介紹了python中asyncore異步模塊的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

asyncore即是一個異步的socket封裝,特別是dispatcher類中包含了很多異步調(diào)用的socket操作方法。

模塊常見方法

這個模塊是socket的異步實現(xiàn),此模塊中的一些類和方法:

asyncore.core

輸入一個輪詢循環(huán)直到通過計數(shù)或打開的通道已關(guān)閉

asyncore.dispatcher

dispatcher類是一個底層socket類的包裝對象。要使它更有用, 它有一部分事件處理方法被異步循環(huán)調(diào)用。否則它就是一個標(biāo)準(zhǔn)的非阻塞socket對象。

底層的事件在特定事件或特定的連接狀態(tài)告訴異步循環(huán),某些高級事件發(fā)生了。例如, 我們要求一個socket連接到另一個主機(jī)。

  • handle_connected():第一次讀或?qū)懯录?/li>
  • handle_close():讀事件沒有數(shù)據(jù)可用。
  • handle_accept():讀事件監(jiān)聽一個socket。
  • handle_read():在異步循環(huán)察覺到通道呼叫read()時調(diào)用。
  • handle_write():在異步循環(huán)檢測到一個socket可寫時調(diào)用,例如:
def handle_write(self):
    sent = self.send(self.buff)
    self.buffer = self.buffer[sent:]
  • handle_expt():當(dāng)有OOB數(shù)據(jù)套接字(socket)連接,這幾乎永遠(yuǎn)不會發(fā)生,因為OOB精細(xì)地支持和很少使用。
  • handle_connect():當(dāng)socket創(chuàng)建一個連接時調(diào)用。
  • handle_close():當(dāng)socket連接關(guān)閉時調(diào)用。
  • handle_error():當(dāng)引發(fā)一個異常并沒有其他處理時調(diào)用。
  • handle_accept:當(dāng)本地監(jiān)聽通道與遠(yuǎn)程端建立連接(被動連接)時調(diào)用。
  • readable():每次在異步循環(huán)確定是否添加一個通道socket到讀事件列表時調(diào)用,默認(rèn)都為True。
  • writable():每次在異步循環(huán)確定是否添加一個通道socket到寫事件列表時調(diào)用, 默認(rèn)為True。
  • create_socket():與創(chuàng)建標(biāo)準(zhǔn)socket的時候相同。
  • connect():與標(biāo)準(zhǔn)socket的端口設(shè)置是相同, 接受一個元組第一個參數(shù)為主機(jī)地址,第二個參數(shù)是端口號。
  • send():向遠(yuǎn)程端socket發(fā)送數(shù)據(jù)。
  • recv():從遠(yuǎn)程端socket讀取最多buffer_size的數(shù)據(jù)。一個空的字符串意味著從另一端通道已關(guān)閉。
  • listen():監(jiān)聽socket連接。
  • bind():將socket綁定到地址。
  • accept():接受一個連接, 必須綁定到一個socket和監(jiān)聽地址。
  • close():關(guān)閉socket。

asyncore.dispatcher_with_send

dispatcher子類添加了簡單的緩沖輸出功能用于簡單的客戶,更復(fù)雜的使用asynchat.async_chat。

asyncore.file_dispatcher

file_dispatcher需要一個文件描述符或文件對象地圖以及一個可選的參數(shù),包裝,使用調(diào)查()或循環(huán)()函數(shù)。如果提供一個文件對象或任何fileno()方法,該方法將調(diào)用和傳遞到file_wrapper構(gòu)造函數(shù)??捎眯?UNIX。

asyncore.file_wrapper

file_wrapper需要一個整數(shù)文件描述符并調(diào)用os.dup()復(fù)制處理,這樣原來的處理可能是獨立于file_wrapper關(guān)閉。這個類實現(xiàn)足夠的方法來模擬一個套接字使用file_dispatcher類??捎眯?UNIX。

asyncore 實例

一個http client的實現(xiàn)

import socket
import asyncore

class Client(asyncore.dispatcher):

?? ?def __init__(self, host, path):
?? ??? ?asyncore.dispatcher.__init__(self)
?? ??? ?self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
?? ??? ?self.connect((host,80))
?? ??? ?self.buffer = 'GET %s HTTP/1.0\r\n\r\n' % path
?? ?
? ? def handle_connect(self):
?? ??? ?pass
?? ?
? ? def handle_close(self):
?? ??? ?self.close()
?? ?
? ? def handle_read(self):
?? ??? ?print self.recv(8192)
?? ?
? ? def writable(self):
?? ??? ?return (len(self.buffer) >0)
?? ?
? ? def handle_write(self):
?? ??? ?sent= self.send(self.buffer)
?? ??? ?self.buffer = self.buffer[sent:]
?? ??? ?client= Client('www.python.org','/')
?? ??? ?asyncore.loop()

服務(wù)器接受連接和分配任務(wù)

import socket
import asyncore

class EchoHandler(asyncore.dispatcher_with_send):
?? ?
? ? def handle_read(self):
?? ??? ?data= self.recv(8192)
? ? ? ? if data:
? ? ? ? ? ? self.send(data)

class EchoServer(asyncore.dispatcher):

? ? def __init__(self, host, port):
? ? ? ? asyncore.dispatcher.__init__(self)
? ? ? ? self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
? ? ? ? self.set_reuse_add()
? ? ? ? self.bind((host, port))
? ? ? ? self.listen(5)

? ? def handle_accept(self):
? ? ? ? pair= self.accept()
? ? ? ? if pair is not None:
? ? ? ? sock, addr= pair
? ? ? ? print 'Incoming connection from %s' % repr(addr)
? ? ? ? handler= EchoHandler(sock)
? ? ? ? server= EchoServer('localhost',8080)
? ? ? ? asyncore.loop()

利用asyncore的端口映射(端口轉(zhuǎn)發(fā))

import socket,asyncore

class forwarder(asyncore.dispatcher):

? ? def __init__(self, ip, port, remoteip,remoteport,backlog=5):
? ? ? ? asyncore.dispatcher.__init__(self)
? ? ? ? self.remoteip=remoteip
? ? ? ? self.remoteport=remoteport
? ? ? ? self.create_socket(socket.AF_INET,socket.SOCK_STREAM)
? ? ? ? self.set_reuse_addr()
? ? ? ? self.bind((ip,port))
? ? ? ? self.listen(backlog)

? ? def handle_accept(self):
? ? ? ? conn, addr= self.accept()
? ? ? ? # print '--- Connect --- '
? ? ? ? sender(receiver(conn),self.remoteip,self.remoteport)

class receiver(asyncore.dispatcher):

? ? def __init__(self,conn):
? ? ? ? asyncore.dispatcher.__init__(self,conn)
? ? ? ? self.from_remote_buffer=''
? ? ? ? self.to_remote_buffer=''
? ? ? ? self.sender=None
? ? ? ??
? ? def handle_connect(self):
? ? ? ? pass

? ? def handle_read(self):
? ? ? ? read= self.recv(4096)
? ? ? ? # print '%04i -->'%len(read)
? ? ? ? self.from_remote_buffer+= read

? ? def writable(self):
? ? ?? ?return (len(self.to_remote_buffer) >0)

? ? def handle_write(self):
?? ??? ?sent= self.send(self.to_remote_buffer)
? ? ? ? # print '%04i <--'%sent
? ? ? ? self.to_remote_buffer= self.to_remote_buffer[sent:]

? ? def handle_close(self):
? ? ? ? self.close()
? ? ? ? if self.sender:
?? ??? ??? ?self.sender.close()

class sender(asyncore.dispatcher):

? ? def __init__(self, receiver, remoteaddr,remoteport):
? ? ? ? asyncore.dispatcher.__init__(self)
? ? ? ? self.receiver=receiver
? ? ? ? receiver.sender=self
? ? ? ? self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
? ? ? ? self.connect((remoteaddr, remoteport))

? ? def handle_connect(self):
? ? ?? ?pass

? ? def handle_read(self):
? ? ? ? read= self.recv(4096)
? ? ? ? # print '<-- %04i'%len(read)
? ? ? ? self.receiver.to_remote_buffer+= read

? ? def writable(self):
? ? ?? ?return (len(self.receiver.from_remote_buffer) >0)

? ? def handle_write(self):
?? ??? ?sent= self.send(self.receiver.from_remote_buffer)
? ? ?? ?# print '--> %04i'%sent
? ? ?? ?self.receiver.from_remote_buffer= self.receiver.from_remote_buffer[sent:]

? ? def handle_close(self):
? ? ? ? self.close()
? ? ? ? self.receiver.close()

? ??
? ? if __name__=='__main__':

? ? ? ? import optparse
? ? ? ? parser= optparse.OptionParser()
? ? ? ??
? ? ? ? parser.add_option(
? ? ? ? '-l','--local-ip',
? ? ? ? dest='local_ip',default='127.0.0.1',
? ? ? ? help='Local IP address to bind to')
? ? ? ??
? ? ? ? parser.add_option(
? ? ? ? '-p','--local-port',
? ? ? ? type='int',dest='local_port',default=80,
? ? ? ? help='Local port to bind to')
? ? ? ??
? ? ? ? parser.add_option(
? ? ? ? '-r','--remote-ip',dest='remote_ip',
? ? ? ? help='Local IP address to bind to')

? ? ? ? parser.add_option(
? ? ? ? '-P','--remote-port',
? ? ? ? type='int',dest='remote_port',default=80,
? ? ? ? help='Remote port to bind to')

? ? ? ? options, args= parser.parse_args()

? ? ? ? forwarder(options.local_ip,options.local_port,options.remote_ip,options.remote_port)

? ? ? ? asyncore.loop()

到此這篇關(guān)于python中asyncore異步模塊的實現(xiàn)的文章就介紹到這了,更多相關(guān)python asyncore異步模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Pandas修改DataFrame列名的兩種方法實例

    Pandas修改DataFrame列名的兩種方法實例

    這篇文章主要給大家介紹了關(guān)于Pandas修改DataFrame列名的兩種方法,文中通過實例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Pandas具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-03-03
  • Matplotlib自定義圖例(多張圖共享一個圖例)

    Matplotlib自定義圖例(多張圖共享一個圖例)

    最近再用Matplotlib繪圖,需要做兩個子圖都不需要設(shè)置圖例,圖例單獨用一個figure來顯示,本文就詳細(xì)的來介紹一下,感興趣的可以了解一下
    2023-08-08
  • python 如何把docker-compose.yaml導(dǎo)入到數(shù)據(jù)庫相關(guān)條目里

    python 如何把docker-compose.yaml導(dǎo)入到數(shù)據(jù)庫相關(guān)條目里

    這篇文章主要介紹了python 如何把docker-compose.yaml導(dǎo)入到數(shù)據(jù)庫相關(guān)條目里?下面小編就為大家介紹一下實現(xiàn)方式,一起跟隨小編過來看看吧
    2021-01-01
  • Python批量修改文件名操作指南(文件名的替換、前綴、后綴的添加)

    Python批量修改文件名操作指南(文件名的替換、前綴、后綴的添加)

    很多時候我們手上有一堆文件,需要修改名稱時需要一個一個修改,太麻煩了,這篇文章主要給大家介紹了關(guān)于Python批量修改文件名的相關(guān)資料,包括文件名的替換、前綴、后綴的添加的相關(guān)資料,需要的朋友可以參考下
    2024-04-04
  • Python繪制莖葉圖的示例代碼

    Python繪制莖葉圖的示例代碼

    這篇文章主要為大家信息介紹了Python繪制莖葉圖的相關(guān)知識,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01
  • Python GUI編程之tkinter模塊Toplevel控件實現(xiàn)搭建父子窗口

    Python GUI編程之tkinter模塊Toplevel控件實現(xiàn)搭建父子窗口

    這篇文章主要介紹了Python使用tkinter模塊Toplevel控件搭建父子窗口的實現(xiàn)方法,Tkinter是Python的標(biāo)準(zhǔn)GUI庫,Python使用Tkinter可以快速的創(chuàng)建GUI應(yīng)用程序,用到相關(guān)控件的同學(xué)可以參考下
    2023-12-12
  • Pyspark獲取并處理RDD數(shù)據(jù)代碼實例

    Pyspark獲取并處理RDD數(shù)據(jù)代碼實例

    這篇文章主要介紹了Pyspark獲取并處理RDD數(shù)據(jù)代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • 使用Python實現(xiàn)簡單的數(shù)據(jù)備份

    使用Python實現(xiàn)簡單的數(shù)據(jù)備份

    數(shù)據(jù)備份,即數(shù)據(jù)的復(fù)制和存儲,是指將數(shù)據(jù)從一個位置復(fù)制到另一個位置,以防止原始數(shù)據(jù)丟失或損壞,下面我們就來了解一下用Python如何實現(xiàn)這一功能吧
    2025-03-03
  • Python實現(xiàn)繪制多角星實例

    Python實現(xiàn)繪制多角星實例

    這篇文章要給大家分享Python實現(xiàn)繪制多角星的實例,在具備一定的Python編程基礎(chǔ)以后,我們可以結(jié)合for循環(huán)進(jìn)行多角星的編寫,只要簡單的幾次循環(huán),即可以極大的解決重復(fù)編寫相同代碼方面的問題,下面小編將以三角星,五角星為例,進(jìn)而引入如何繪制多角星,需要的朋友可以參考一下
    2021-11-11
  • python神經(jīng)網(wǎng)絡(luò)Keras構(gòu)建CNN網(wǎng)絡(luò)訓(xùn)練

    python神經(jīng)網(wǎng)絡(luò)Keras構(gòu)建CNN網(wǎng)絡(luò)訓(xùn)練

    這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)學(xué)習(xí)使用Keras構(gòu)建CNN網(wǎng)絡(luò)訓(xùn)練,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05

最新評論

措勤县| 安陆市| 彰武县| 永善县| 沈阳市| 奈曼旗| 吴堡县| 保亭| 赤峰市| 枣强县| 依兰县| 藁城市| 扎赉特旗| 新晃| 广德县| 绵阳市| 晋州市| 汤阴县| 抚远县| 加查县| 永清县| 新泰市| 蚌埠市| 庐江县| 那曲县| 松潘县| 阳泉市| 建昌县| 宜城市| 文化| 富蕴县| 盐津县| 岫岩| 娄底市| 海淀区| 徐州市| 石门县| 嘉峪关市| 蕉岭县| 高州市| 长葛市|