Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
s = client.as_stream()
async for line in s:
try:
n = int(line)
result = await run_in_process(fib, n)
resp = str(result) + '\n'
await s.write(resp.encode('ascii'))
except ValueError:
await s.write(b'Bad input\n')
print('Connection closed')
await client.close()
if __name__ == '__main__':
try:
run(tcp_server, '', 25000, fib_handler)
except KeyboardInterrupt:
pass
async def run(self):
"""
Main loop to spawn http and https servers and handle signal interrupts
"""
print("Server starting up")
async with SignalQueue(signal.SIGHUP, signal.SIGINT, signal.SIGTERM) as sig:
while True:
# Spin up tcp servers
if settings.ENABLE_HTTP:
serve_http_task = await spawn(tcp_server, "localhost", settings.HTTP_PORT, self.serve_http)
if settings.ENABLE_HTTPS:
serve_https_task = await spawn(tcp_server, "localhost", settings.HTTPS_PORT, self.serve_https)
# wait for signal intterupts
signo = await sig.get()
await serve_http_task.cancel()
await serve_https_task.cancel()
if signo == signal.SIGHUP:
print("Server restarting")
# TODO reload configuration
else:
print("Server shutting down")
break
async def run(self):
"""
Main loop to spawn http and https servers and handle signal interrupts
"""
print("Server starting up")
async with SignalQueue(signal.SIGHUP, signal.SIGINT, signal.SIGTERM) as sig:
while True:
# Spin up tcp servers
if settings.ENABLE_HTTP:
serve_http_task = await spawn(tcp_server, "localhost", settings.HTTP_PORT, self.serve_http)
if settings.ENABLE_HTTPS:
serve_https_task = await spawn(tcp_server, "localhost", settings.HTTPS_PORT, self.serve_https)
# wait for signal intterupts
signo = await sig.get()
await serve_http_task.cancel()
await serve_https_task.cancel()
if signo == signal.SIGHUP:
print("Server restarting")
# TODO reload configuration
else:
print("Server shutting down")
break
await s.write(
b'''HTTP/1.0 200 OK\r
Content-type: text/plain\r
\r
If you're seeing this, it probably worked. Yay!
''')
await s.write(time.asctime().encode('ascii'))
await client.close()
if __name__ == '__main__':
ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ssl_context.load_cert_chain(certfile=CERTFILE, keyfile=KEYFILE)
print('Connect to https://localhost:10000 to see if it works')
try:
curio.run(curio.tcp_server('', 10000, handler, ssl=ssl_context))
except KeyboardInterrupt:
pass
for listen_uri in listen_uris:
listen = uri_compile(listen_uri, True)
if via:
listen.kw["via"] = via
host = listen.kw.pop("host")
port = listen.kw.pop("port")
ssl_context = listen.kw.pop("ssl_context", None)
if listen.scheme in ("ss", "ssudp") and "cipher_cls" not in listen.kw:
raise argparse.ArgumentTypeError(
"you need to assign cryto algorithm and password: "
f"{listen.scheme}://{host}:{port}"
)
if listen.scheme.endswith("udp"):
server = udp_server(host, port, listen.proto(**listen.kw))
else:
server = tcp_server(
host,
port,
ProtoFactory(listen.proto, **listen.kw),
backlog=1024,
ssl=ssl_context,
)
server_list.append((server, (host, port), listen.scheme))
return server_list
async def chat_server(host, port):
async with TaskGroup() as g:
await g.spawn(dispatcher)
await g.spawn(tcp_server, host, port, chat_handler)
from curio import run, tcp_server
async def echo_client(client, addr):
print('Connection from', addr)
s = client.as_stream()
async for line in s:
await s.write(line)
print('Connection closed')
await s.close()
if __name__ == '__main__':
try:
run(tcp_server, '', 25000, echo_client)
except KeyboardInterrupt:
pass
async def __call__(self):
await curio.tcp_server('', self.port, lambda c, a: ConnectionHandler(self)(c, a),
reuse_address=True, reuse_port=True)
except (OSError, NameError):
pass
s = client.as_stream()
while True:
data = await s.read(102400)
if not data:
break
await s.write(data)
await s.close()
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)
run(tcp_server, '', 25000, echo_handler, ssl=ssl_context)