Skip to main content

A tornado MSTP library, using WebSocket.

Project description

Tornado MSTP

A tornado MSTP library, using WebSocket.

Installation

pip install tornado_mstp

Easy Examples

See repo's directory named 'examples'.

Usage

Server

As a server-side application using tornado_mstp. You should first create a handler class inherited from MstpSocketHandler.

from tornado_mstp import MstpSocketHandler


class MyMstpSocketHandler(MstpSocketHandler):
    pass

There are two optional class attributes, automatic_closing_time(default to 10) and ver(default to "0.1"), that indicate MSTP server information need to be set.

from tornado_mstp import MstpSocketHandler


class MyMstpSocketHandler(MstpSocketHandler):
    automatic_closing_time = 15
    ver = "0.1"

login method MUST be overridden to make an authorization, and returns a tornado_mstp.schema.LoginResponse.

from typing import Optional

from tornado_mstp import MstpSocketHandler
from tornado_mstp.schema import LoginResponse


class MyMstpSocketHandler(MstpSocketHandler):
    automatic_closing_time = 15
    ver = "0.1"

    async def login(
        self,
        *,
        secret: Optional[str] = None
    ) -> LoginResponse:
        # your login processing
        return LoginResponse(
            status=True,
            connection_interval=300,
            session_alive_time=300,
            session_id="ajsdkjalskdjlka"
        )

So as reconnect, that returns a tornado_mstp.schema.ReconnectResponse.

from typing import Optional

from tornado_mstp import MstpSocketHandler
from tornado_mstp.schema import ReconnectResponse


class MyMstpSocketHandler(MstpSocketHandler):
    automatic_closing_time = 15
    ver = "0.1"

    async def reconnect(
        self,
        *,
        session_id: Optional[str] = None
    ) -> ReconnectResponse:
        # your reconnect logics
        return ReconnectResponse(status=True)

There is no need to worry about tornado websocket handler's on_message method. Each MSTP package type of message has a callback function who needs to be implemented. For example, for segment handling.

from typing import Optional

from tornado_mstp import MstpSocketHandler
from tornado_mstp.schema import ContentType


class MyMstpSocketHandler(MstpSocketHandler):
    automatic_closing_time = 15
    ver = "0.1"

    async def handle_segment(
        self,
        package_id: Optional[str] = None,
        message_index: Optional[int] = None,
        segment_index: Optional[int] = None,
        is_end: Optional[bool] = None,
        content_type: Optional[ContentType] = None,
        content: Optional[str] = None
    ) -> None:
        print(f"received content: {content}")

After implementing all callback functions, server can handle any MSTP packages as you want.

Client

As a client-side connection using tornado_mstp. You can just create an mstpsocket_connect using tornado_mstp.mstpsocket_connect. It has the same usage of tornado.websocket.websocket_connect with some extra arguments.

import asyncio
from typing import Optional

from tornado_mstp import mstpsocket_connect
from tornado_mstp import MstpSocketClientConnection


class MyMstpSocketClientConnection(MstpSocketClientConnection):

    async def handle_login_response(
        self,
        *,
        status: Optional[bool] = None,
        connection_interval: Optional[int] = None,
        session_alive_time: Optional[int] = None,
        session_id: Optional[str] = None,
        reason: Optional[str] = None
    ) -> None:
        print(session_id)


async def main():
    await mstpsocket_connect(
        url="mstp://localhost:8005/mstp",
        secret="secret",
        connection_class=MyMstpSocketClientConnection
    )
    f = asyncio.Future()
    await f


if __name__ == '__main__':
    asyncio.run(main())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tornado_mstp-0.1.1.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

tornado_mstp-0.1.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file tornado_mstp-0.1.1.tar.gz.

File metadata

  • Download URL: tornado_mstp-0.1.1.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for tornado_mstp-0.1.1.tar.gz
Algorithm Hash digest
SHA256 30cb8b9aa5b7e22d5b019df680c71ca0754ee12cac8df89ba192dfef36281f51
MD5 2f058705367f86c911b1a62edc0627f0
BLAKE2b-256 e9a760e81f26c2712803108b8c032250ef7548f818c353c572f07757d53db080

See more details on using hashes here.

File details

Details for the file tornado_mstp-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for tornado_mstp-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 26bec1a00979e906cc11fa74bef910680358fd4a5c374583c12c1ec563aa6d8a
MD5 eb599130da3f6816bbab2ccf04eb8258
BLAKE2b-256 a4638e295c9e7603f0720aacf3c10804b160ad44953e23f557cd9193ae949d3c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page