Skip to main content

aio-tcpserver

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.

Metadata

Release files for aio-tcpserver 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aio-tcpserver 0.0.3
File Size Uploaded Provenance Actions
aio-tcpserver-0.0.3.tar.gz 9.7 kB Without provenance Details for aio-tcpserver-0.0.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for aio-tcpserver 0.0.3
File Interpreter ABI Platform Actions
aio_tcpserver-0.0.3-py3-none-any.whl Python 3 none any Details for aio_tcpserver-0.0.3-py3-none-any.whl

Total release size: 23.9 kB

Release files / aio-tcpserver-0.0.3.tar.gz

Download URL aio-tcpserver-0.0.3.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
366ae3a468123e04391c0e10d034d6b0a6dae82d2a7e0e3dade20ba2c856d8ad
BLAKE2b-256 checksum
How to use checksums
f583a669d4b402be0f654a7ede51eaa1043a3fffa43587643e44518cbdfbc6b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / aio_tcpserver-0.0.3-py3-none-any.whl

Download URL aio_tcpserver-0.0.3-py3-none-any.whl
Size 14.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
21f3252e787d657f792a56adc97d6a12818a395211cbe99b75dc2c9dd8e809c5
BLAKE2b-256 checksum
How to use checksums
e1e693dcd26bff3eb331395b896c7b72d988237cf8e609c8a777e447dda91d89
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for aio-tcpserver 0.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page