午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術文章
文章詳情頁

Python WebSocket長連接心跳與短連接的示例

瀏覽:233日期:2022-07-04 13:54:49

安裝

pip install websocket-client

先來看一下,長連接調用方式:

ws = websocket.WebSocketApp('ws://echo.websocket.org/', on_message = on_message, on_error = on_error, on_close = on_close) ws.on_open = on_open ws.run_forever()

長連接,參數介紹:

(1)url: websocket的地址。

(2)header: 客戶發送websocket握手請求的請求頭,{’head1:value1’,’head2:value2’}。

(3)on_open:在建立Websocket握手時調用的可調用對象,這個方法只有一個參數,就是該類本身。

(4)on_message:這個對象在接收到服務器返回的消息時調用。有兩個參數,一個是該類本身,一個是我們從服務器獲取的字符串(utf-8格式)。

(5)on_error:這個對象在遇到錯誤時調用,有兩個參數,第一個是該類本身,第二個是異常對象。

(6)on_close:在遇到連接關閉的情況時調用,參數只有一個,就是該類本身。

(7)on_cont_message:這個對象在接收到連續幀數據時被調用,有三個參數,分別是:類本身,從服務器接受的字符串(utf-8),連續標志。

(8)on_data:當從服務器接收到消息時被調用,有四個參數,分別是:該類本身,接收到的字符串(utf-8),數據類型,連續標志。

(9)keep_running:一個二進制的標志位,如果為True,這個app的主循環將持續運行,默認值為True。

(10)get_mask_key:用于產生一個掩碼。

(11)subprotocols:一組可用的子協議,默認為空。

長連接關鍵方法:ws.run_forever(ping_interval=60,ping_timeout=5)

如果不斷開關閉websocket連接,會一直阻塞下去。另外這個函數帶兩個參數,如果傳的話,啟動心跳包發送。

ping_interval:自動發送“ping”命令,每個指定的時間(秒),如果設置為0,則不會自動發送。

ping_timeout:如果沒有收到pong消息,則為超時(秒)。

ws.run_forever(ping_interval=60,ping_timeout=5)#ping_interval心跳發送間隔時間#ping_timeout 設置,發送ping到收到pong的超時時間

我們看源代碼,會發現這樣一斷代碼:

ping的超時時間,要大于ping間隔時間

if not ping_timeout or ping_timeout <= 0: ping_timeout = None if ping_timeout and ping_interval and ping_interval <= ping_timeout: raise WebSocketException('Ensure ping_interval > ping_timeout')

長連接:

示例1:

import websockettry: import threadexcept ImportError: import _thread as threadimport timedef on_message(ws, message): print(message)def on_error(ws, error): print(error)def on_close(ws): print('### closed ###')def on_open(ws): def run(*args): ws.send('hello1') time.sleep(1) ws.close() thread.start_new_thread(run,())if __name__ == '__main__': websocket.enableTrace(True) ws = websocket.WebSocketApp('ws://echo.websocket.org/', on_message = on_message, on_error = on_error, on_close = on_close) ws.on_open = on_open ws.run_forever(ping_interval=60,ping_timeout=5)

示例2:

import websocketfrom threading import Threadimport timeimport sysclass MyApp(websocket.WebSocketApp): def on_message(self, message): print(message) def on_error(self, error): print(error) def on_close(self): print('### closed ###') def on_open(self): def run(*args): for i in range(3):# send the message, then wait# so thread doesn’t exit and socket# isn’t closedself.send('Hello %d' % i)time.sleep(1) time.sleep(1) self.close() print('Thread terminating...') Thread(target=run).start()if __name__ == '__main__': websocket.enableTrace(True) if len(sys.argv) < 2: host = 'ws://echo.websocket.org/' else: host = sys.argv[1] ws = MyApp(host) ws.run_forever()

短連接:

from websocket import create_connectionws = create_connection('ws://echo.websocket.org/')print('Sending ’Hello, World’...')ws.send('Hello, World')print('Sent')print('Receiving...')result = ws.recv()print('Received ’%s’' % result)ws.close()

以上就是Python WebSocket長連接心跳與短連接的示例的詳細內容,更多關于Python WebSocket連接的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 日韩欧美一卡二卡 | 国产黄色免费在线观看 | 日本专区 | xxx国产| 91精品国产综合久久久久久 | 四虎毛片 | 国产一区二区观看 | 亚洲精品综合 | 国产做受视频 | 伊人久久久久久久久 | 久久久久久久久亚洲 | 黄色一级大片在线免费看产 | 日韩精品欧美精品 | 国产精久久 | 日韩福利视频在线观看 | 性感毛片| 男生操女生免费网站 | 爽妇综合网 | 国产成人麻豆 | 黄色一级网 | 国产精品免费视频观看 | 超碰10000| 看av在线 | 亚洲婷婷av | 中文字幕日韩欧美 | 欧美黄色大全 | 四虎影视精品 | 日韩欧美区 | 狠狠搞狠狠搞 | 男女爽爽爽 | 日本欧美色 | 免费看成年人视频 | 中文字幕第31页 | 亚洲男人天堂网 | 欧美爱爱视频 | 国产系列在线 | 国产亚洲一区二区三区 | 毛片aaa| 国偷自拍 | 国产成人在线免费观看视频 | 337p粉嫩大胆色噜噜噜 |