Пример #1
0
async def main(host, port):
    while True:
        async with SignalSet(signal.SIGHUP) as sigset:
            print('Starting the server')
            serv_task = await new_task(run_server(host, port, echo_client))
            await sigset.wait()
            print('Server shutting down')
            await serv_task.cancel_children()
            await serv_task.cancel()
Пример #2
0
# A simple echo server 

from curio import Kernel, new_task, run_server


async def echo_handler(client, addr):
    print('Connection from', addr)
    while True:
        data = await client.recv(10000)
        if not data:
            break
        await client.sendall(data)
    print('Connection closed')

if __name__ == '__main__':
    kernel = Kernel()
    kernel.run(run_server('', 25000, echo_handler))
# echoserv.py
#
# Echo server using streams

from curio import Kernel, new_task, run_server


async def echo_client(client, addr):
    print('Connection from', addr)
    reader, writer = client.make_streams()
    async with reader, writer:
        async for line in reader:
            await writer.write(line)
    print('Connection closed')


if __name__ == '__main__':
    kernel = Kernel()
    kernel.run(run_server('', 25000, echo_client))
Пример #4
0
# echoserv.py
#
# Echo server using streams

from curio import Kernel, new_task, run_server

async def echo_client(client, addr):
    print('Connection from', addr)
    reader, writer = client.make_streams()
    async with reader, writer:
        async for line in reader:
            await writer.write(line)
    print('Connection closed')

if __name__ == '__main__':
    kernel = Kernel()
    kernel.run(run_server('', 25000, echo_client))
Пример #5
0
from curio import Kernel, new_task, run_cpu_bound, run_server

def fib(n):
    if n <= 2:
        return 1
    else:
        return fib(n-1) + fib(n-2)

async def fib_handler(client, addr):
    print('Connection from', addr)
    rfile, wfile = client.make_streams()
    async with rfile, wfile:
        async for line in rfile:
            try:
                n = int(line)
                result = await run_cpu_bound(fib, n)
                resp = str(result) + '\n'
                await wfile.write(resp.encode('ascii'))
            except ValueError:
                await wfile.write(b'Bad input\n')
    print('Connection closed')

if __name__ == '__main__':
    kernel = Kernel()
    kernel.run(run_server('', 25000, fib_handler))



    
Пример #6
0
import curio
from curio import ssl
import time

KEYFILE = "ssl_test_rsa"    # Private key
CERTFILE = "ssl_test.crt"   # Certificate (self-signed)

async def handler(client, addr):
    reader, writer = client.make_streams()
    async with reader, writer:
        async for line in reader:
            line = line.strip()
            if not line:
                break
            print(line)

        await writer.write(
b'''HTTP/1.0 200 OK\r
Content-type: text/plain\r
\r
If you're seeing this, it probably worked. Yay!
''')
        await writer.write(time.asctime().encode('ascii'))

if __name__ == '__main__':
    kernel = curio.Kernel()
    ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
    ssl_context.load_cert_chain(certfile=CERTFILE, keyfile=KEYFILE)
    kernel.run(curio.run_server('', 10000, handler, ssl=ssl_context))
Пример #7
0
def run_server():
    kernel = curio.Kernel()
    kernel.run(curio.run_server("localhost", 8080, http_serve))
import curio
from curio import ssl
import time

KEYFILE = "ssl_test_rsa"  # Private key
CERTFILE = "ssl_test.crt"  # Certificate (self-signed)


async def handler(client, addr):
    reader, writer = client.make_streams()
    async with reader, writer:
        async for line in reader:
            line = line.strip()
            if not line:
                break
            print(line)

        await writer.write(b'''HTTP/1.0 200 OK\r
Content-type: text/plain\r
\r
If you're seeing this, it probably worked. Yay!
''')
        await writer.write(time.asctime().encode('ascii'))


if __name__ == '__main__':
    kernel = curio.Kernel()
    ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
    ssl_context.load_cert_chain(certfile=CERTFILE, keyfile=KEYFILE)
    kernel.run(curio.run_server('', 10000, handler, ssl=ssl_context))