autobahn Python 簡易通訊互動

2021-09-27 12:59:20 字數 2423 閱讀 9280

***************=客戶端***************==

from autobahn.asyncio.websocket import websocketclientprotocol, \

websocketclientfactory

try:

import asyncio

except importerror:

import trollius as asyncio

class myclientprotocol(websocketclientprotocol):

def onconnect(self, response):

print("請求連線服務端: ".format(response.peer))

async def onopen(self):

print("成功服務端連線.")

while true:

self.sendmessage(u"hello, world!".encode('utf8'))

# self.sendmessage(b"\x00\x01\x03\x04", isbinary=true)

await asyncio.sleep(1)

def onmessage(self, payload, isbinary):

if isbinary:

print("接受到服務端的位元組資訊: bytes".format(len(payload)))

else:

print("接受到服務端的文字資訊: ".format(payload.decode('utf8')))

def onclose(self, wasclean, code, reason):

print("斷開與服務端連線的原因: ".format(reason))

if __name__ == '__main__':

factory = websocketclientfactory(u"ws:")

factory.protocol = myclientprotocol

loop = asyncio.get_event_loop()

coro = loop.create_connection(factory, '127.0.0.1', 9000)

loop.run_until_complete(coro)

loop.run_forever()

loop.close()

********************===服務端********************====

from autobahn.asyncio.websocket import websocketserverprotocol, \

websocketserve***ctory

class myserverprotocol(websocketserverprotocol):

def onconnect(self, request):

print("連線到客戶端: ".format(request.peer))

def onopen(self):

print("已有客戶端成功連線.")

def onmessage(self, payload, isbinary):

if isbinary:

print("接收到客服端位元組資訊: bytes".format(len(payload)))

else:

print("接收到客服端文字資訊: ".format(payload.decode('utf8')))

# echo back message verbatim

self.sendmessage(payload, isbinary)

def onclose(self, wasclean, code, reason):

print("斷開與客戶端的連線原因: ".format(reason))

if __name__ == '__main__':

import asyncio

factory = websocketserve***ctory(u"ws:")

factory.protocol = myserverprotocol

loop = asyncio.get_event_loop()

coro = loop.create_server(factory, '0.0.0.0', 9000)

server = loop.run_until_complete(coro)

try:

loop.run_forever()

except keyboardinterrupt:

pass

finally:

server.close()

loop.close()

JS簡簡簡易時鐘

簡易時鐘 1 搭建html css部分 插入時鐘背景,使用子絕父相。把時針和分針 秒針的樣式放在時鐘的中心位置 2 js邏輯部分 1 獲取時針 分針 秒針 var hour document.getelementbyid hour var min document.getelementbyid mi...

Metasploit Framework 簡單介紹

1.簡介 metasploit framework 是乙個緩衝區溢位測試使用的輔助工具,也可以說是乙個漏洞利用和測試平台。它整合了各平台上常見的溢位漏洞和流行的shellcode,並且不斷更新,使得緩衝區溢位測試變得方便和簡單。2.主頁 1 選定使用的漏洞 use 2 設定漏洞利用的環境 targe...

android PagerAdapter簡單應用

pageradapter是android.support.v4包中的類,它的子類有fragmentpageradapter fragmentstatepageradapter,這兩個adapter都是fragment的介面卡,用於實現fragment的滑動效果 pageradapter主要是view...