Документация по Python

Вебсокеты (WebSockets) в Python

В: Документация по Python

Простое Echo с aiohttp

aiohttp обеспечивает асинхронные WebSockets.

import asyncio
from aiohttp import ClientSession

with ClientSession() as session:
    async def hello_world():

        websocket = await session.ws_connect("wss://echo.websocket.org")

        websocket.send_str("Hello, world!")

        print("Received:", (await websocket.receive()).data)

        await websocket.close()

    loop = asyncio.get_event_loop()
    loop.run_until_complete(hello_world())

Класс Wrapper с aiohttp

aiohttp.ClientSession может быть использован в качестве родителя для пользовательского класса WebSocket.

import asyncio
from aiohttp import ClientSession

class EchoWebSocket(ClientSession):

    URL = "wss://echo.websocket.org"

    def __init__(self):
        super().__init__()
        self.websocket = None

    async def connect(self):
        """Connect to the WebSocket."""
        self.websocket = await self.ws_connect(self.URL)

    async def send(self, message):
        """Send a message to the WebSocket."""
        assert self.websocket is not None, "You must connect first!"
        self.websocket.send_str(message)
        print("Sent:", message)

    async def receive(self):
        """Receive one message from the WebSocket."""
        assert self.websocket is not None, "You must connect first!"
        return (await self.websocket.receive()).data

    async def read(self):
        """Read messages from the WebSocket."""
        assert self.websocket is not None, "You must connect first!"

        while self.websocket.receive():
            message = await self.receive()
            print("Received:", message)
            if message == "Echo 9!":
                break

async def send(websocket):
    for n in range(10):
        await websocket.send("Echo {}!".format(n))
        await asyncio.sleep(1)

loop = asyncio.get_event_loop()

with EchoWebSocket() as websocket:

    loop.run_until_complete(websocket.connect())

    tasks = (
        send(websocket),
        websocket.read()
    )

    loop.run_until_complete(asyncio.wait(tasks))

    loop.close()

Использование Autobahn в качестве фабрики веб-сокетов

Пакет Autobahn можно использовать для фабрик серверов Python.

Документация по пакету Python Autobahn

Для установки обычно нужно просто использовать команду терминала

(Для Linux):

 sudo pip install autobahn

 

(Для Windows):

 python -m pip install autobahn

 

Затем в скрипте Python можно создать простой эхо-сервер:

from autobahn.asyncio.websocket import WebSocketServerProtocol
class MyServerProtocol(WebSocketServerProtocol):
    '''When creating server protocol, the
    user defined class inheriting the 
    WebSocketServerProtocol needs to override
    the onMessage, onConnect, et-c events for 
    user specified functionality, these events 
    define your server's protocol, in essence'''
    def onMessage(self,payload,isBinary):
        '''The onMessage routine is called 
        when the server receives a message.
        It has the required arguments payload 
        and the bool isBinary. The payload is the 
        actual contents of the "message" and isBinary
        is simply a flag to let the user know that 
        the payload contains binary data. I typically 
        elsewise assume that the payload is a string.
        In this example, the payload is returned to sender verbatim.'''
        self.sendMessage(payload,isBinary)
if__name__=='__main__':
    try:
        importasyncio
    except ImportError:
        '''Trollius = 0.3 was renamed'''
        import trollius as asyncio
    from autobahn.asyncio.websocketimportWebSocketServerFactory
    factory=WebSocketServerFactory()
    '''Initialize the websocket factory, and set the protocol to the 
    above defined protocol(the class that inherits from 
    autobahn.asyncio.websocket.WebSocketServerProtocol)'''
    factory.protocol=MyServerProtocol
    '''This above line can be thought of as "binding" the methods
    onConnect, onMessage, et-c that were described in the MyServerProtocol class
    to the server, setting the servers functionality, ie, protocol'''
    loop=asyncio.get_event_loop()
    coro=loop.create_server(factory,'127.0.0.1',9000)
    server=loop.run_until_complete(coro)
    '''Run the server in an infinite loop'''
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass
    finally:
        server.close()
        loop.close()

В этом примере сервер создается на локальном хосте (127.0.0.1) на порту 9000. Это прослушивающий IP-адрес и порт. Это важная информация, так как с ее помощью вы можете определить адрес локальной сети и порт вашего компьютера от модема, независимо от того, какие маршрутизаторы у вас есть на компьютере. Затем, используя Google для исследования вашего WAN IP, вы можете создать свой веб-сайт для отправки сообщений WebSocket на ваш WAN IP через порт 9000 (в этом примере).

Важно, чтобы вы перенесли данные с модема обратно, то есть, если у вас есть маршрутизатор, подключенный к модему, войдите в настройки конфигурации модема, перенесите порт с модема на подключенный маршрутизатор и т. Д. До окончательного маршрутизатора вашего компьютера. подключен, передает информацию, полученную через модемный порт 9000 (в этом примере), на него.

Еще от кодкамп
Замечательно! Вы успешно подписались.
Добро пожаловать обратно! Вы успешно вошли
Вы успешно подписались на кодкамп.
Срок действия вашей ссылки истек.
Ура! Проверьте свою электронную почту на наличие волшебной ссылки для входа.
Успех! Ваша платежная информация обновлена.
Ваша платежная информация не была обновлена.