async def main(): # It might be desirable to move these out of the examples # directory, as this test are now relying on them being around file_path = join(dirname(dirname(__file__)), 'examples') cert_file = join(file_path, 'ssl_test.crt') key_file = join(file_path, 'ssl_test_rsa') server_context = curiossl.create_default_context( ssl.Purpose.CLIENT_AUTH) server_context.load_cert_chain(certfile=cert_file, keyfile=key_file) stdlib_client_context = ssl.create_default_context( ssl.Purpose.SERVER_AUTH) curio_client_context = curiossl.create_default_context( ssl.Purpose.SERVER_AUTH) server_task = await spawn( network.tcp_server('', 10000, handler, ssl=server_context)) await sleep(0.1) for test_context in (curio_client_context, stdlib_client_context): test_context.check_hostname = False test_context.verify_mode = ssl.CERT_NONE resp = await client('localhost', 10000, test_context) assert resp == b'Back atcha: Hello, world!' await server_task.cancel()
# ssl_echo # # An example of a simple SSL echo server. Use ssl_echo_client.py to test. import os import curio from curio import ssl from curio import network KEYFILE = 'ssl_test_rsa' # Private key # Certificate (self-signed) CERTFILE = 'ssl_test.crt' async def handle(client, addr): print('Connection from', addr) async with client: while True: data = await client.recv(1000) if not data: break await client.send(data) print('Connection closed') if __name__ == '__main__': ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) ssl_context.load_cert_chain(certfile=CERTFILE, keyfile=KEYFILE) curio.run(network.tcp_server('', 10000, handle, ssl=ssl_context))
# curiosslecho.py # # Use sslclient.py to test import curio from curio import ssl from curio import network from socket import * KEYFILE = "ssl_test_rsa" # Private key CERTFILE = "ssl_test.crt" # Certificate (self-signed) async def handle(client, addr): print('Connection from', addr) client.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1) async with client: while True: data = await client.recv(100000) if not data: break await client.sendall(data) print('Connection closed') if __name__ == '__main__': ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) ssl_context.load_cert_chain(certfile=CERTFILE, keyfile=KEYFILE) curio.run(network.tcp_server('', 25000, handle, ssl=ssl_context))