Skip to main content

simple tools

Project description

Desc

A tcp sever for running asyncio.Protocol. The inspiration comes from sanic,

only support python 3.6+

keywords:tcp-server,asyncio

Feature

  • with multiple worker

  • for asyncio.Protocol

  • can be closed by ctrl+C in windows

  • with hooks

Example

server.py

import asyncio
import time
from aio_tcpserver import tcp_server, listener

@listener("before_server_start")
async def beat(loop):
    print(time.time())
    print("ping")

class EchoServerClientProtocol(asyncio.Protocol):
    def connection_made(self, transport):
        peername = transport.get_extra_info('peername')
        print('Connection from {}'.format(peername))
        self.transport = transport

    def data_received(self, data):
        message = data.decode()
        print('Data received: {!r}'.format(message))

        print('Send: {!r}'.format(message))
        self.transport.write(data)

        print('Close the client socket')
        self.transport.close()

def main():
    tcp_server(EchoServerClientProtocol, worker=3)


if __name__ == '__main__':
    main()

client.py

import asyncio

class EchoClientProtocol(asyncio.Protocol):
    def __init__(self, message, loop):
        self.message = message
        self.loop = loop

    def connection_made(self, transport):
        transport.write(self.message.encode())
        print('Data sent: {!r}'.format(self.message))

    def data_received(self, data):
        print('Data received: {!r}'.format(data.decode()))

    def connection_lost(self, exc):
        print('The server closed the connection')
        print('Stop the event loop')
        self.loop.stop()

loop = asyncio.get_event_loop()
message = 'Hello World!'
coro = loop.create_connection(lambda: EchoClientProtocol(message, loop),
                            '127.0.0.1', 5000)
loop.run_until_complete(coro)
loop.run_forever()
loop.close()

Install

  • python -m pip install aio-tcpserver

Documentation

Documentation on Readthedocs.

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

aio-tcpserver-0.0.3.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

aio_tcpserver-0.0.3-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file aio-tcpserver-0.0.3.tar.gz.

File metadata

File hashes

Hashes for aio-tcpserver-0.0.3.tar.gz
Algorithm Hash digest
SHA256 366ae3a468123e04391c0e10d034d6b0a6dae82d2a7e0e3dade20ba2c856d8ad
MD5 64881de96f7d6ffa8c7b72dd201607a2
BLAKE2b-256 f583a669d4b402be0f654a7ede51eaa1043a3fffa43587643e44518cbdfbc6b0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aio_tcpserver-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 21f3252e787d657f792a56adc97d6a12818a395211cbe99b75dc2c9dd8e809c5
MD5 25cdaa8156340db16e45724357b1c084
BLAKE2b-256 e1e693dcd26bff3eb331395b896c7b72d988237cf8e609c8a777e447dda91d89

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