欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python?asyncore?socket客戶端開發(fā)基本使用教程

 更新時間:2022年12月26日 14:50:15   作者:JSON_L  
asyncore庫是python的一個標(biāo)準(zhǔn)庫,提供了以異步的方式寫入套接字服務(wù)的客戶端和服務(wù)器的基礎(chǔ)結(jié)構(gòu),這篇文章主要介紹了Python?asyncore?socket客戶端開發(fā)基本使用,需要的朋友可以參考下

介紹

asyncore庫是python的一個標(biāo)準(zhǔn)庫,提供了以異步的方式寫入套接字服務(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ù)

    當(dāng)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 表示不可寫。

        如果不識閑默認(rèn)返回為 True,當(dāng)返回True時,回調(diào)函數(shù)handle_write將被觸發(fā)

def writable(self):
    return False

實現(xiàn) handle_write 回調(diào)函數(shù)

       當(dāng)有數(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標(biāo)識有數(shù)據(jù)需要讀取,

       False表示沒有數(shù)據(jù)需要被讀取,當(dāng)不實現(xiàn)默認(rèn)返回True,

       當(dāng)返回True時,handle_read將被觸發(fā)

def readable(self):
    # 表示有數(shù)據(jù)需要讀取
    return True

實現(xiàn) handle_read 回調(diào)函數(shù)

        當(dāng)有數(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ù)

       當(dāng)程序運行過程發(fā)生異常時回調(diào)

def handle_error(self):
    # 編寫處理錯誤方法
    t, e, trace = sys.exc_info()
    print(t, e, trace)

實現(xiàn) handle_close 回調(diào)函數(shù)

        當(dāng)連接被關(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)
 
    '''
    當(dāng)與發(fā)起對本地端點的 connect() 調(diào)用的新遠(yuǎn)程端點已建立連接時會在偵聽通道(被動打開方)上被調(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客戶端開發(fā)基本使用的文章就介紹到這了,更多相關(guān)Python asyncore socket使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論