def setUp(self): self.loop = asyncio.new_event_loop() asyncio.set_event_loop(None) session_factory = CookieSessionFactory(secret_key=b'secret', cookie_name='test_cookie', dumps=json.dumps, loads=json.loads, loop=self.loop) self.server = RESTServer(debug=True, keep_alive=75, hostname='localhost', session_factory=session_factory, loop=self.loop) rest = REST(self) self.server.add_url('GET', '/init', rest.init_session, use_request='req') self.server.add_url('GET', '/get', rest.get_from_session, use_request='req') self.server.add_url('GET', '/counter', rest.counter, use_request='req') self.server.add_url('GET', '/counter/{start}', rest.counter, use_request='req')
def main(): loop = asyncio.get_event_loop() handler = Handler() session_factory = CookieSessionFactory(secret_key=b'secret', cookie_name='test_cookie', dumps=json.dumps, loads=json.loads, loop=loop) server = aiorest.RESTServer(hostname='127.0.0.1', keep_alive=75, session_factory=session_factory, loop=loop) server.add_url('GET', '/count', handler.counter, use_request='req') srv = loop.run_until_complete( loop.create_server(server.make_handler, '127.0.0.1', 8080)) @asyncio.coroutine def query(): connector = aiohttp.TCPConnector(share_cookies=True, loop=loop) for _ in range(6): resp = yield from aiohttp.request('GET', 'http://127.0.0.1:8080/count', connector=connector, loop=loop) data = yield from resp.json() print('Count is', data) loop.run_until_complete(query()) srv.close() loop.run_until_complete(srv.wait_closed()) loop.close()