Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def run_server(app):
asyncio.set_event_loop(None)
loop = asyncio.new_event_loop()
protocol = functools.partial(http.HttpProtocol, consumer=app, loop=loop)
create_server_task = loop.create_server(protocol, host='127.0.0.1')
server = loop.run_until_complete(create_server_task)
url = 'ws://127.0.0.1:%d/' % server.sockets[0].getsockname()[1]
try:
# Run the event loop in a new thread.
threading.Thread(target=run_loop, args=[loop]).start()
# Return the contextmanager state.
yield url
finally:
# Close the loop from our main thread.
loop.call_soon_threadsafe(loop.stop)
async def create_server(self, loop, app, host, port):
protocol = functools.partial(http.HttpProtocol, consumer=app, loop=loop)
server = await loop.create_server(protocol, host=host, port=port)
self.servers.append(server)
async def tick(self, loop):
while self.alive:
http.set_time_and_date()
await asyncio.sleep(1)
logger.warning("Stopping worker [{}]".format(os.getpid()))
for server in self.servers:
server.close()
await server.wait_closed()
loop.stop()
def get_status_line(status_code):
try:
phrase = http.HTTPStatus(status_code).phrase.encode()
except ValueError:
phrase = b''
return b''.join([
b'HTTP/1.1 ', str(status_code).encode(), b' ', phrase, b'\r\n'
])