av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

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

用Python進行websocket接口測試

瀏覽:5日期:2022-07-08 09:09:31

我們在做接口測試時,除了常見的http接口,還有一種比較多見,就是socket接口,今天講解下怎么用Python進行websocket接口測試。

現在大多數用的都是websocket,那我們就先來安裝一下websocket的安裝包。

pip install websocket-client

用Python進行websocket接口測試

安裝完之后,我們就開始我們的websocket之旅了。

我們先來看個炒雞簡單的栗子:

import websocketws = websocket.WebSocket()ws.connect('ws://example.com/websocket', http_proxy_host='proxy_host_name', http_proxy_port=3128)

這個栗子就是創建一個websocket連接,這個模塊支持通過http代理訪問websocket。代理服務器允許使用connect方法連接到websocket端口。默認的squid設置是“只允許連接HTTPS端口”。

在websocket里,我們有常用的這幾個方法:

on_message方法:

def on_message(ws, message): print(message)

on_message是用來接受消息的,server發送的所有消息都可以用on_message這個方法來收取。

on_error方法:

def on_error(ws, error): print(error)

這個方法是用來處理錯誤異常的,如果一旦socket的程序出現了通信的問題,就可以被這個方法捕捉到。

on_open方法:

def on_open(ws): def run(*args): for i in range(30): # send the message, then wait # so thread doesn’t exit and socket # isn’t closed ws.send('Hello %d' % i) time.sleep(1) time.sleep(1) ws.close() print('Thread terminating...') Thread(target=run).start()

on_open方法是用來保持連接的,上面這樣的一個例子,就是保持連接的一個過程,每隔一段時間就會來做一件事,他會在30s內一直發送hello。最后停止。

on_close方法:

def on_close(ws): print('### closed ###')

onclose主要就是關閉socket連接的。

如何創建一個websocket應用:

ws = websocket.WebSocketApp('wss://echo.websocket.org')

括號里面就是你要連接的socket的地址,在WebSocketApp這個實例化的方法里面還可以有其他參數,這些參數就是我們剛剛介紹的這些方法。

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

指定了這些參數之后就可以直接進行調用了,例如:

ws.on_open = on_open

這樣就是調用了on_open方法

如果我們想讓我們的socket保持長連接,一直連接著,就可以使用run_forever方法:

ws.run_forever()

完整代碼:

import websocketfrom threading import Threadimport timeimport sysdef 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): for i in range(3): # send the message, then wait # so thread doesn’t exit and socket # isn’t closed ws.send('Hello %d' % i) time.sleep(1) time.sleep(1) ws.close() print('Thread terminating...') Thread(target=run).start()if __name__ == '__main__': websocket.enableTrace(True) host = 'ws://echo.websocket.org/' ws = websocket.WebSocketApp(host,on_message=on_message,on_error=on_error,on_close=on_close) ws.on_open = on_open 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()

關于websocket的介紹就到這兒了。

以上就是用Python進行websocket接口測試的詳細內容,更多關于python 接口測試的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 女同一区二区三区 | 黄色在线播放 | 日韩中文字幕在线观看 | 日日拍夜夜拍 | 福利视频午夜 | www色| 国产理论在线观看 | 毛片一区| 在线视频亚洲 | 成人三级在线观看 | 日本黄色免费看 | 国产一级黄色大片 | 五月天色综合 | 国产精品一区三区 | 成人久久av| 一级片免费视频 | 欧美美女视频 | 黄色免费一级片 | 黄色一级大片 | 午夜激情福利视频 | 国产一区二区三区免费 | 99精品视频在线观看 | 黄色av毛片| 久久久久一区二区 | 日韩一级免费视频 | 国产精品视频一区二区三区 | 精品国产乱码一区二区三 | 亚洲国产欧美日韩在线 | 成人一区二区三区四区 | 日韩欧美精品在线 | 一区二区三区视频在线观看 | 99久久婷婷国产综合精品草原 | 婷婷中文网 | 国产成人av网站 | 成人国产精品一区二区 | 亚洲一区中文 | 偷偷操不一样 | www国产亚洲精品久久网站 | 午夜影院在线观看视频 | 亚洲日本在线观看 | 亚洲黄色影院 |