Skip to main content

Library to build backend with MTProto protocol

Project description

mtpylon

Running Tests codecov

Library to build backend with MTProto's protocol

Installation

pip install mtpylon

Getting started

  1. Generate rsa keys:

rsa_keys.py:

from typing import List
import rsa  # type: ignore
from mtpylon.crypto import KeyPair  # type: ignore


def get_rsa_keys(count: int = 2) -> List[KeyPair]:
    rsa_list = [
        rsa.newkeys(nbits=2048)
        for _ in range(count)
    ]

    return [
        KeyPair(
            public=public,
            private=private
        ) for (public, private) in rsa_list
    ]
  1. Declare schema for mtpylon

schema.py:

import random
from dataclasses import dataclass

from aiohttp import web

from mtpylon import Schema


@dataclass
class Reply:
    rand_id: int
    content: str

    class Meta:
        name = 'reply'
        order = ('rand_id', 'content')


async def echo(request: web.Request, content: str) -> Reply:
    return Reply(
        rand_id=random.randint(1, 100),
        content=content
    )


schema = Schema(constructors=[Reply], functions=[echo])
  1. Configure aiohttp with mtpylon

web.py:

import sys
import logging

from aiohttp import web
import aiohttp_cors

from mtpylon.configuration import configure_app

from schema import schema as app_schema
from rsa_keys import get_rsa_keys

# create console handler and set level to debug
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(level=logging.DEBUG)

# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# add formatter to ch
ch.setFormatter(formatter)

logging.basicConfig(level=logging.DEBUG)


if __name__ == '__main__':
    app = web.Application()
    configure_app(
        app,
        app_schema,
        {
            'rsa_manager': {
                'params': {
                    'rsa_keys': get_rsa_keys()
                }
            },
            'pub_keys_path': '/pub-keys',
            'schema_path': '/schema',
        }
    )

    cors = aiohttp_cors.setup(
        app,
        defaults={
            '*': aiohttp_cors.ResourceOptions(
                allow_credentials=True,
                expose_headers="*",
                allow_headers="*",
            )
        }
    )

    for route in list(app.router.routes()):
        cors.add(route)

    web.run_app(app, port=8081)
  1. Start it!
python web.py
  1. to work with backend please try https://github.com/Zapix/zagram

Documentation

For more information visit:

https://mtpylon.readthedocs.io/en/latest/

Example:

Echo server: https://github.com/Zapix/echo-server

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

mtpylon-0.0.3.tar.gz (103.6 kB view details)

Uploaded Source

Built Distribution

mtpylon-0.0.3-py3-none-any.whl (170.7 kB view details)

Uploaded Python 3

File details

Details for the file mtpylon-0.0.3.tar.gz.

File metadata

  • Download URL: mtpylon-0.0.3.tar.gz
  • Upload date:
  • Size: 103.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

File hashes

Hashes for mtpylon-0.0.3.tar.gz
Algorithm Hash digest
SHA256 3a5a7bd717de7306a3da3a70ac2d002ebc52c6c8062054119142019a031e8270
MD5 e8f49d6780a3638333537f542675ae8b
BLAKE2b-256 58fedab52af5acd49cba9a77df91f5cb17ab353a4b15dd9b310b8cdfffc56aa9

See more details on using hashes here.

File details

Details for the file mtpylon-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: mtpylon-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 170.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

File hashes

Hashes for mtpylon-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 cd2544dfc23ddc90fa52fc5d700910328973211a089cfd72b4a3b745231e8c44
MD5 982bcf9accc6b6d8e2d13941b0a50380
BLAKE2b-256 d7a0297e883f5d85fcfeeab026fe59c3b7d6a4b65eadae6c74dcff9e7d463dde

See more details on using hashes here.

Supported by

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