Python asyncore socket客戶端實現(xiàn)方法詳解
介紹
asyncore庫是python的一個標準庫,提供了以異步的方式寫入套接字服務(wù)的客戶端和服務(wù)器的基礎(chǔ)結(jié)構(gòu)。操作網(wǎng)絡(luò)的時候可以直接使用socket等底層的庫,但是asyncore使得我們可以更加方便的操作網(wǎng)絡(luò),避免直接使用socket,select,poll等工具時需要面對的復(fù)雜情況。
1.定義類并且繼承 asyncore.dispatcher
class SocketClient(asyncore.dispatcher):
2.實現(xiàn)類中的回調(diào)代碼
調(diào)用父類方法
asyncore.dispatcher.__init__(self)
創(chuàng)建socket對象
self.create_socket()
連接服務(wù)器
address = (host, port) self.connect(address)
實現(xiàn) handle_connect 回調(diào)函數(shù)
當socket 連接服務(wù)器成功時回調(diào)該函數(shù)
def handle_connect(self): print('連接成功')
實現(xiàn) writable 回調(diào)函數(shù)
描述是否有數(shù)據(jù)需要被發(fā)送到服務(wù)器。返回值為True表示可寫,F(xiàn)alse 表示不可寫。
如果不識閑默認返回為 True,當返回True時,回調(diào)函數(shù)handle_write將被觸發(fā)
def writable(self): return False
實現(xiàn) handle_write 回調(diào)函數(shù)
當有數(shù)據(jù)需要發(fā)送時 (writable 回調(diào)函數(shù)返回 True時),
該函數(shù)被觸發(fā),通常情況下在該函數(shù)中編寫send方法發(fā)送數(shù)據(jù)
def handle_write(self): # 內(nèi)部實現(xiàn)對服務(wù)器發(fā)送數(shù)據(jù) # 調(diào)用 send 方法,參數(shù)是字節(jié)數(shù)據(jù) self.send('hello world'.encode('utf-8'))
實現(xiàn) readable 回調(diào)函數(shù)
描述是否有數(shù)據(jù)從服務(wù)端讀取。返回True標識有數(shù)據(jù)需要讀取,
False表示沒有數(shù)據(jù)需要被讀取,當不實現(xiàn)默認返回True,
當返回True時,handle_read將被觸發(fā)
def readable(self): # 表示有數(shù)據(jù)需要讀取 return True
實現(xiàn) handle_read回調(diào)函數(shù)
當有數(shù)據(jù)需要讀取時(readable 回調(diào)函數(shù)返回True時),
該函數(shù)被觸發(fā),通常情況下在該函數(shù)中編寫recv方法接收數(shù)據(jù)
def handle_read(self): # 主動讀取接收數(shù)據(jù) 參數(shù)是需要接收數(shù)據(jù)長度 result = self.recv(8000) print(result)
實現(xiàn) handle_error回調(diào)函數(shù)
當程序運行過程發(fā)生異常時回調(diào)
def handle_error(self): # 編寫處理錯誤方法 t, e, trace = sys.exc_info() print(t, e, trace)
實現(xiàn) handle_close回調(diào)函數(shù)
當連接被關(guān)閉時觸發(fā)
def handle_close(self): print('連接關(guān)閉') # 執(zhí)行關(guān)閉 self.close()
3.創(chuàng)建對象并且執(zhí)行asyncore.loop進入運行循環(huán)
timeout為一次循環(huán)所用的時間,也就是超時時間。
client = SocketClient('127.0.0.1', 9000) # 開始啟動運行循環(huán) asyncore.loop(timeout=10)
服務(wù)端示例代碼
import asyncore import socket 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_addr() self.bind((host, port)) # 監(jiān)聽連接參數(shù)指定排隊的最大連接數(shù)和應(yīng)至少為1; 最大值取決于系統(tǒng)(通常為5)。 self.listen(5) ''' 當與發(fā)起對本地端點的 connect() 調(diào)用的新遠程端點已建立連接時會在偵聽通道(被動打開方)上被調(diào)用。 sock 是可被用于在連接上發(fā)送和接收數(shù)據(jù)的 新建 套接字對象, 而 addr 是綁定到連接另一端的套接字的地址。 ''' def handle_accept(self): pair = self.accept() if pair is not None: sock, addr = pair print('連接來自于 %s' % repr(addr)) # 連接成功后 給客戶端發(fā)送消息 handler = EchoHandler(sock) handler.send('hello world'.encode('utf-8')) if __name__ == '__main__': server = EchoServer('127.0.0.1', 9000) asyncore.loop()
運行結(jié)果
服務(wù)端:
客戶端:
注意
本文章使用python3.7版本,3.10版本已經(jīng)移除此模塊,之后可使用asyncio模塊。
至此結(jié)束,本文章只做了一個基本使用講解,可以查看借鑒使用,若想要做消息還差的很多。
到此這篇關(guān)于Python asyncore socket客戶端實現(xiàn)方法詳解的文章就介紹到這了,更多相關(guān)Python asyncore socket內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
介紹Python的Django框架中的靜態(tài)資源管理器django-pipeline
這篇文章主要介紹了介紹Python的Django框架中的靜態(tài)資源管理器django-pipeline,django-pipeline是一個開源項目,被用來處理css等靜態(tài)文件,需要的朋友可以參考下2015-04-04Pandas 實現(xiàn)分組計數(shù)且不計重復(fù)
這篇文章主要介紹了Pandas 實現(xiàn)分組計數(shù)且不計重復(fù)的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03解決python 3 urllib 沒有 urlencode 屬性的問題
今天小編就為大家分享一篇解決python 3 urllib 沒有 urlencode 屬性的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08