def test_start_unix_server(self): class MyServer: def __init__(self, loop, path): self.server = None self.loop = loop self.path = path @asyncio.coroutine def handle_client(self, client_reader, client_writer): data = yield from client_reader.readline() client_writer.write(data) def start(self): self.server = self.loop.run_until_complete( asyncio.start_unix_server(self.handle_client, path=self.path, loop=self.loop)) def handle_client_callback(self, client_reader, client_writer): task = asyncio.Task(client_reader.readline(), loop=self.loop) def done(task): client_writer.write(task.result()) task.add_done_callback(done) def start_callback(self): self.server = self.loop.run_until_complete( asyncio.start_unix_server(self.handle_client_callback, path=self.path, loop=self.loop)) def stop(self): if self.server is not None: self.server.close() self.loop.run_until_complete(self.server.wait_closed()) self.server = None @asyncio.coroutine def client(path): reader, writer = yield from asyncio.open_unix_connection( path, loop=self.loop) # send a line writer.write(b"hello world!\n") # read it back msgback = yield from reader.readline() writer.close() return msgback # test the server variant with a coroutine as client handler with test_utils.unix_socket_path() as path: server = MyServer(self.loop, path) server.start() msg = self.loop.run_until_complete(asyncio.Task(client(path), loop=self.loop)) server.stop() self.assertEqual(msg, b"hello world!\n") # test the server variant with a callback as client handler with test_utils.unix_socket_path() as path: server = MyServer(self.loop, path) server.start_callback() msg = self.loop.run_until_complete(asyncio.Task(client(path), loop=self.loop)) server.stop() self.assertEqual(msg, b"hello world!\n")
def client_connected(self, listener_name, reader: ReaderAdapter, writer: WriterAdapter): # Wait for connection available on listener server = self._servers.get(listener_name, None) if not server: raise BrokerException("Invalid listener name '%s'" % listener_name) yield from server.acquire_connection() remote_address, remote_port = writer.get_peer_info() self.logger.info("Connection from %s:%d on listener '%s'" % (remote_address, remote_port, listener_name)) # Wait for first packet and expect a CONNECT try: handler, client_session = yield from BrokerProtocolHandler.init_from_connect(reader, writer, self.plugins_manager, loop=self._loop) except HBMQTTException as exc: self.logger.warning("[MQTT-3.1.0-1] %s: Can't read first packet an CONNECT: %s" % (format_client_message(address=remote_address, port=remote_port), exc)) #yield from writer.close() self.logger.debug("Connection closed") return except MQTTException as me: self.logger.error('Invalid connection from %s : %s' % (format_client_message(address=remote_address, port=remote_port), me)) yield from writer.close() self.logger.debug("Connection closed") return if client_session.clean_session: # Delete existing session and create a new one if client_session.client_id is not None and client_session.client_id != "": self.delete_session(client_session.client_id) else: client_session.client_id = gen_client_id() client_session.parent = 0 else: # Get session from cache if client_session.client_id in self._sessions: self.logger.debug("Found old session %s" % repr(self._sessions[client_session.client_id])) (client_session, h) = self._sessions[client_session.client_id] client_session.parent = 1 else: client_session.parent = 0 if client_session.keep_alive > 0: client_session.keep_alive += self.config['timeout-disconnect-delay'] self.logger.debug("Keep-alive timeout=%d" % client_session.keep_alive) handler.attach(client_session, reader, writer) self._sessions[client_session.client_id] = (client_session, handler) authenticated = yield from self.authenticate(client_session, self.listeners_config[listener_name]) if not authenticated: yield from writer.close() server.release_connection() # Delete client from connections list return while True: try: client_session.transitions.connect() break except (MachineError, ValueError): # Backwards compat: MachineError is raised by transitions < 0.5.0. self.logger.warning("Client %s is reconnecting too quickly, make it wait" % client_session.client_id) # Wait a bit may be client is reconnecting too fast yield from asyncio.sleep(1, loop=self._loop) yield from handler.mqtt_connack_authorize(authenticated) yield from self.plugins_manager.fire_event(EVENT_BROKER_CLIENT_CONNECTED, session=client_session, client_id=client_session.client_id) self.logger.debug("%s Start messages handling" % client_session.client_id) yield from handler.start() self.logger.debug("Retained messages queue size: %d" % client_session.retained_messages.qsize()) yield from self.publish_session_retained_messages(client_session) # Init and start loop for handling client messages (publish, subscribe/unsubscribe, disconnect) disconnect_waiter = asyncio.ensure_future(handler.wait_disconnect(), loop=self._loop) subscribe_waiter = asyncio.ensure_future(handler.get_next_pending_subscription(), loop=self._loop) unsubscribe_waiter = asyncio.ensure_future(handler.get_next_pending_unsubscription(), loop=self._loop) wait_deliver = asyncio.ensure_future(handler.mqtt_deliver_next_message(), loop=self._loop) connected = True while connected: try: done, pending = yield from asyncio.wait( [disconnect_waiter, subscribe_waiter, unsubscribe_waiter, wait_deliver], return_when=asyncio.FIRST_COMPLETED, loop=self._loop) if disconnect_waiter in done: result = disconnect_waiter.result() self.logger.debug("%s Result from wait_diconnect: %s" % (client_session.client_id, result)) if result is None: self.logger.debug("Will flag: %s" % client_session.will_flag) # Connection closed anormally, send will message if client_session.will_flag: self.logger.debug("Client %s disconnected abnormally, sending will message" % format_client_message(client_session)) yield from self._broadcast_message( client_session, client_session.will_topic, client_session.will_message, client_session.will_qos) if client_session.will_retain: self.retain_message(client_session, client_session.will_topic, client_session.will_message, client_session.will_qos) self.logger.debug("%s Disconnecting session" % client_session.client_id) yield from self._stop_handler(handler) client_session.transitions.disconnect() yield from self.plugins_manager.fire_event(EVENT_BROKER_CLIENT_DISCONNECTED, session=client_session, client_id=client_session.client_id) connected = False if unsubscribe_waiter in done: self.logger.debug("%s handling unsubscription" % client_session.client_id) unsubscription = unsubscribe_waiter.result() for topic in unsubscription['topics']: self._del_subscription(topic, client_session) yield from self.plugins_manager.fire_event( EVENT_BROKER_CLIENT_UNSUBSCRIBED, client_id=client_session.client_id, topic=topic) yield from handler.mqtt_acknowledge_unsubscription(unsubscription['packet_id']) unsubscribe_waiter = asyncio.Task(handler.get_next_pending_unsubscription(), loop=self._loop) if subscribe_waiter in done: self.logger.debug("%s handling subscription" % client_session.client_id) subscriptions = subscribe_waiter.result() return_codes = [] for subscription in subscriptions['topics']: result = yield from self.add_subscription(subscription, client_session) return_codes.append(result) yield from handler.mqtt_acknowledge_subscription(subscriptions['packet_id'], return_codes) for index, subscription in enumerate(subscriptions['topics']): if return_codes[index] != 0x80: yield from self.plugins_manager.fire_event( EVENT_BROKER_CLIENT_SUBSCRIBED, client_id=client_session.client_id, topic=subscription[0], qos=subscription[1]) yield from self.publish_retained_messages_for_subscription(subscription, client_session) subscribe_waiter = asyncio.Task(handler.get_next_pending_subscription(), loop=self._loop) self.logger.debug(repr(self._subscriptions)) if wait_deliver in done: if self.logger.isEnabledFor(logging.DEBUG): self.logger.debug("%s handling message delivery" % client_session.client_id) app_message = wait_deliver.result() if not app_message.topic: self.logger.warning("[MQTT-4.7.3-1] - %s invalid TOPIC sent in PUBLISH message, closing connection" % client_session.client_id) break if "#" in app_message.topic or "+" in app_message.topic: self.logger.warning("[MQTT-3.3.2-2] - %s invalid TOPIC sent in PUBLISH message, closing connection" % client_session.client_id) break yield from self.plugins_manager.fire_event(EVENT_BROKER_MESSAGE_RECEIVED, client_id=client_session.client_id, message=app_message) # Tian: publish filter permitted = yield from self.topic_filtering(client_session, topic=app_message.topic, phase='publish') if permitted: yield from self._broadcast_message(client_session, app_message.topic, app_message.data) #===================== # yield from self._broadcast_message(client_session, app_message.topic, app_message.data) if app_message.publish_packet.retain_flag: self.retain_message(client_session, app_message.topic, app_message.data, app_message.qos) wait_deliver = asyncio.Task(handler.mqtt_deliver_next_message(), loop=self._loop) except asyncio.CancelledError: self.logger.debug("Client loop cancelled") break disconnect_waiter.cancel() subscribe_waiter.cancel() unsubscribe_waiter.cancel() wait_deliver.cancel() self.logger.debug("%s Client disconnected" % client_session.client_id) server.release_connection()
def test_start_unix_server(self): class MyServer: def __init__(self, loop, path): self.server = None self.loop = loop self.path = path async def handle_client(self, client_reader, client_writer): data = await client_reader.readline() client_writer.write(data) await client_writer.drain() client_writer.close() def start(self): self.server = self.loop.run_until_complete( asyncio.start_unix_server(self.handle_client, path=self.path, loop=self.loop)) def handle_client_callback(self, client_reader, client_writer): self.loop.create_task(self.handle_client(client_reader, client_writer)) def start_callback(self): start = asyncio.start_unix_server(self.handle_client_callback, path=self.path, loop=self.loop) self.server = self.loop.run_until_complete(start) def stop(self): if self.server is not None: self.server.close() self.loop.run_until_complete(self.server.wait_closed()) self.server = None async def client(path): reader, writer = await asyncio.open_unix_connection( path, loop=self.loop) # send a line writer.write(b"hello world!\n") # read it back msgback = await reader.readline() writer.close() return msgback messages = [] self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) # test the server variant with a coroutine as client handler with test_utils.unix_socket_path() as path: server = MyServer(self.loop, path) server.start() msg = self.loop.run_until_complete(asyncio.Task(client(path), loop=self.loop)) server.stop() self.assertEqual(msg, b"hello world!\n") # test the server variant with a callback as client handler with test_utils.unix_socket_path() as path: server = MyServer(self.loop, path) server.start_callback() msg = self.loop.run_until_complete(asyncio.Task(client(path), loop=self.loop)) server.stop() self.assertEqual(msg, b"hello world!\n") self.assertEqual(messages, [])
async def on_ready(): asyncio.Task(Autoplay()) await client.send_message(client.get_channel(gvars.bot), "The Bot is now online!") print("Birdbot online!")
def connection_made(self, transport): self.transport = transport self.task = asyncio.Task(self.send_loop())
def test_process_group_read(self): """Test if process_group_read. Nothing really to test here.""" xknx = XKNX(loop=self.loop) device = Device(xknx, 'TestDevice') self.loop.run_until_complete( asyncio.Task(device.process_group_read(Telegram())))
def run_briefly(loop): @asyncio.coroutine def once(): pass t = asyncio.Task(once(), loop=loop) loop.run_until_complete(t)
def test(): get_task = asyncio.Task(queue_get(), loop=loop) yield from asyncio.sleep(0.01, loop=loop) # let the task start q.put_nowait(1) return (yield from get_task)
def test_put_with_waiting_getters(self): q = asyncio.Queue(loop=self.loop) t = asyncio.Task(q.get(), loop=self.loop) test_utils.run_briefly(self.loop) self.loop.run_until_complete(q.put('a')) self.assertEqual(self.loop.run_until_complete(t), 'a')
await job_handler(NotificationStatusEnum.FAILURE) async def archive_job_worker(dt): """Run handle archive delieved email job every day.""" while True: now = datetime.datetime.now() remaining = (dt - now).total_seconds() if remaining < 86400: break # asyncio.sleep doesn't like long sleeps, so don't sleep more # than a day at a time await asyncio.sleep(86400) await job_handler(NotificationStatusEnum.DELIVERED) if __name__ == '__main__': event_loop = asyncio.get_event_loop() tasks = [ asyncio.Task(queue_worker(event_loop)), asyncio.Task(pending_job_worker()), asyncio.Task(failure_job_worker()) ] event_loop.run_until_complete(asyncio.gather(*tasks)) try: event_loop.run_forever() finally: event_loop.close()
def test_wait_eof_eof(self): stream = self._make_one() stream.feed_eof() wait_task = asyncio.Task(stream.wait_eof(), loop=self.loop) self.loop.run_until_complete(wait_task) self.assertTrue(stream.is_eof())
def fetch(self, inv_item, peer_timeout=10): # create the queue of peers that have this inv_item available q = asyncio.Queue() items = sorted(self.inv_item_db[inv_item.data].items(), key=lambda pair: pair[-1]) for peer, when in items: q.put_nowait(peer) # make the queue available to the object so if more peers # announce they have it, they can be queried self.inv_item_peers_q[inv_item.data] = q # this is the set of futures that we are trying to fetch the item from pending_fetchers = set() # this async method delays a specific amount of time, then # fetches a new peer from the queue @asyncio.coroutine def _wait_for_timeout_and_peer(q, initial_delay=0): yield from asyncio.sleep(initial_delay) while True: peer = yield from q.get() fetcher = self.fetchers_by_peer.get(peer) if fetcher: break logging.debug("requesting %s from %s", inv_item, peer) return asyncio.Task(fetcher.fetch(inv_item)) # the loop works like this: # request the item from a peer # wait 10 s or for response (either notfound or found) # if found, done # if notfound or time out, get another peer most_recent_fetcher = None while True: if most_recent_fetcher is None and q.qsize() > 0: most_recent_fetcher = yield from _wait_for_timeout_and_peer(q) timer_future = asyncio.Task(_wait_for_timeout_and_peer(q, initial_delay=peer_timeout)) futures = pending_fetchers.union(set([timer_future])) if most_recent_fetcher: futures.add(most_recent_fetcher) done, pending_fetchers = \ yield from asyncio.wait(list(futures), return_when=asyncio.FIRST_COMPLETED) # is it time to try a new fetcher? if timer_future in done: # we timed out, so we need to queue up another peer most_recent_fetcher = timer_future.result() logging.debug("timeout, need to request from a new peer, %s", inv_item) timer_future = asyncio.Task(_wait_for_timeout_and_peer(q, initial_delay=peer_timeout)) # we have a new peer available as the result of get_fetcher_future continue # is the most recent done? if most_recent_fetcher and most_recent_fetcher.done(): r = most_recent_fetcher.result() if r: return r # we got a "notfound" from this one # queue up another peer logging.debug("got a notfound, need to try a new peer for %s", inv_item) timer_future.cancel() pending_fetchers.discard(timer_future) most_recent_fetcher = None timer_future = asyncio.Task(_wait_for_timeout_and_peer(q, initial_delay=0)) # we have a new peer available as the result of timer_future continue # one or more fetchers is done # if any of them have a non-None result, we're golden for f in done: r = f.result() if r: logging.info("Got %s", r) return r
src_address = Address("15.15.249") print("Connecting to {}:{} from {}".format(gatewayscanner.found_ip_addr, gatewayscanner.found_port, gatewayscanner.found_local_ip)) tunnel = Tunnel(xknx, src_address, local_ip=gatewayscanner.found_local_ip, gateway_ip=gatewayscanner.found_ip_addr, gateway_port=gatewayscanner.found_port) yield from tunnel.connect_udp() yield from tunnel.connect() yield from tunnel.send_telegram( Telegram(Address('1/0/15'), payload=DPTBinary(0))) yield from asyncio.sleep(2) yield from tunnel.send_telegram( Telegram(Address('1/0/15'), payload=DPTBinary(1))) yield from asyncio.sleep(2) yield from tunnel.connectionstate() yield from tunnel.disconnect() xknx = XKNX() task = asyncio.Task(build_and_destroy_tunnel(xknx)) xknx.loop.run_until_complete(task)
def start(self): task = asyncio.Task(self.async_start()) self.xknx.loop.run_until_complete(task)
def test_wait(self): cond = asyncio.Condition(loop=self.loop) result = [] @asyncio.coroutine def c1(result): yield from cond.acquire() if (yield from cond.wait()): result.append(1) return True @asyncio.coroutine def c2(result): yield from cond.acquire() if (yield from cond.wait()): result.append(2) return True @asyncio.coroutine def c3(result): yield from cond.acquire() if (yield from cond.wait()): result.append(3) return True t1 = asyncio.Task(c1(result), loop=self.loop) t2 = asyncio.Task(c2(result), loop=self.loop) t3 = asyncio.Task(c3(result), loop=self.loop) test_utils.run_briefly(self.loop) self.assertEqual([], result) self.assertFalse(cond.locked()) self.assertTrue(self.loop.run_until_complete(cond.acquire())) cond.notify() test_utils.run_briefly(self.loop) self.assertEqual([], result) self.assertTrue(cond.locked()) cond.release() test_utils.run_briefly(self.loop) self.assertEqual([1], result) self.assertTrue(cond.locked()) cond.notify(2) test_utils.run_briefly(self.loop) self.assertEqual([1], result) self.assertTrue(cond.locked()) cond.release() test_utils.run_briefly(self.loop) self.assertEqual([1, 2], result) self.assertTrue(cond.locked()) cond.release() test_utils.run_briefly(self.loop) self.assertEqual([1, 2, 3], result) self.assertTrue(cond.locked()) self.assertTrue(t1.done()) self.assertTrue(t1.result()) self.assertTrue(t2.done()) self.assertTrue(t2.result()) self.assertTrue(t3.done()) self.assertTrue(t3.result())
def connection_made(self, transport): print('Connection made') self.transport = transport self.stream = aiohttp.StreamParser() asyncio.Task(self.dispatch())
def test_acquire(self): sem = asyncio.Semaphore(3, loop=self.loop) result = [] self.assertTrue(self.loop.run_until_complete(sem.acquire())) self.assertTrue(self.loop.run_until_complete(sem.acquire())) self.assertFalse(sem.locked()) @asyncio.coroutine def c1(result): yield from sem.acquire() result.append(1) return True @asyncio.coroutine def c2(result): yield from sem.acquire() result.append(2) return True @asyncio.coroutine def c3(result): yield from sem.acquire() result.append(3) return True @asyncio.coroutine def c4(result): yield from sem.acquire() result.append(4) return True t1 = asyncio.Task(c1(result), loop=self.loop) t2 = asyncio.Task(c2(result), loop=self.loop) t3 = asyncio.Task(c3(result), loop=self.loop) test_utils.run_briefly(self.loop) self.assertEqual([1], result) self.assertTrue(sem.locked()) self.assertEqual(2, len(sem._waiters)) self.assertEqual(0, sem._value) t4 = asyncio.Task(c4(result), loop=self.loop) sem.release() sem.release() self.assertEqual(2, sem._value) test_utils.run_briefly(self.loop) self.assertEqual(0, sem._value) self.assertEqual([1, 2, 3], result) self.assertTrue(sem.locked()) self.assertEqual(1, len(sem._waiters)) self.assertEqual(0, sem._value) self.assertTrue(t1.done()) self.assertTrue(t1.result()) self.assertTrue(t2.done()) self.assertTrue(t2.result()) self.assertTrue(t3.done()) self.assertTrue(t3.result()) self.assertFalse(t4.done()) # cleanup locked semaphore sem.release() self.loop.run_until_complete(t4)
@asyncio.coroutine def postponed(): a = yield from waiting(1) b = yield from waiting(2) asyncio.Task(waiting('after')) return a + b @asyncio.coroutine def pp_caller(): r = yield from postponed() print("got result from postponed -", r) yield from asyncio.sleep(3) loop.stop() loop = asyncio.get_event_loop() print("-- serial") loop.run_until_complete(serial()) print("-- parallel") loop.run_until_complete(parallel()) print("-- postponed") asyncio.Task(pp_caller()) loop.run_forever() loop.close()
def __init__(self): self._face = '(o.o)' asyncio.Task(self.run())
def postponed(): a = yield from waiting(1) b = yield from waiting(2) asyncio.Task(waiting('after')) return a + b
async def crawler(urls_q, proxies_q, proxies_q_good): parser_jobs = [ asyncio.ensure_future(worker(urls_q, proxies_q, proxies_q_good)) for _ in range(treads) ] await asyncio.gather(*parser_jobs) async def add_urls_to_queue(urls_q): with open(result_file, 'r', encoding='utf-8') as result: result = [x.strip() for x in result.read().split('\n')] result = set([x.split('\t')[0].strip() for x in result]) with open(input_file, 'r', encoding='utf-8') as f: for line in f: keyword = line.strip() if keyword not in result: await urls_q.put(keyword) if __name__ == '__main__': loop = asyncio.get_event_loop() urls_q = asyncio.Queue() proxies_q = asyncio.Queue() proxies_q_good = asyncio.Queue() loop.run_until_complete(add_urls_to_queue(urls_q)) task_crawler = asyncio.Task(crawler(urls_q, proxies_q, proxies_q_good)) task_proxies = asyncio.Task(find_proxies_async(urls_q, proxies_q, treads)) loop.run_until_complete(asyncio.gather(task_crawler, task_proxies))
def on_load(self): if not stdout.isatty(): return self.interface = ScriptInterface('Console', self.server, 'admin', 'console') self.task = asyncio.Task(self.run())
def on_load(self): asyncio.Task(self.start())
def on_start(self): self._heartbeat_task = asyncio.Task(self.heartbeat())
raise OSError('setting time to \'{}\': {}'.format( clock, cmdPipe.stderr.read().decode())) if __name__ == '__main__': from piccolo3.common import piccoloLogging piccoloLogging(debug=True) ps = PiccoloSysinfo() if True: import asyncio import aiocoap.resource as resource import aiocoap root = resource.Site() root.add_resource(*ps.coapSite) root.add_resource( ('.well-known', 'core'), resource.WKCResource(root.get_resources_as_linkheader)) asyncio.Task(aiocoap.Context.create_server_context(root)) asyncio.get_event_loop().run_forever() else: print(ps.get_cpu()) print(ps.get_mem()) print(ps.get_host()) print(ps.get_clock())
def test_target_temperature_down(self): """Test decrease target temperature.""" # pylint: disable=no-self-use xknx = XKNX(loop=self.loop) climate = Climate(xknx, 'TestClimate', group_address_target_temperature='1/2/2', group_address_setpoint_shift='1/2/3') self.loop.run_until_complete( asyncio.Task(climate.set_setpoint_shift(1))) self.assertEqual(xknx.telegrams.qsize(), 1) self.assertEqual( xknx.telegrams.get_nowait(), # DEFAULT_SETPOINT_SHIFT_STEP is 0.5 -> payload = setpoint_shift * 2 Telegram(GroupAddress('1/2/3'), payload=DPTArray(2))) self.loop.run_until_complete( asyncio.Task(climate.target_temperature.set(23.00))) self.assertEqual(xknx.telegrams.qsize(), 1) self.assertEqual( xknx.telegrams.get_nowait(), Telegram(GroupAddress('1/2/2'), payload=DPTArray(DPT2ByteFloat().to_knx(23.00)))) self.assertEqual(climate.base_temperature, 22.0) # First change self.loop.run_until_complete( asyncio.Task(climate.set_target_temperature(20.50))) self.assertEqual(xknx.telegrams.qsize(), 2) self.assertEqual(xknx.telegrams.get_nowait(), Telegram(GroupAddress('1/2/3'), payload=DPTArray(0xFD))) # -3 self.assertEqual( xknx.telegrams.get_nowait(), Telegram(GroupAddress('1/2/2'), payload=DPTArray(DPT2ByteFloat().to_knx(20.50)))) self.assertEqual(climate.target_temperature.value, 20.50) # Second change self.loop.run_until_complete( asyncio.Task(climate.set_target_temperature(19.00))) self.assertEqual(xknx.telegrams.qsize(), 2) self.assertEqual(xknx.telegrams.get_nowait(), Telegram(GroupAddress('1/2/3'), payload=DPTArray(0xFA))) # -6 self.assertEqual( xknx.telegrams.get_nowait(), Telegram(GroupAddress('1/2/2'), payload=DPTArray(DPT2ByteFloat().to_knx(19.00)))) self.assertEqual(climate.target_temperature.value, 19.00) # Test min target temperature # Base (22) - setpoint_shift_min (6) self.assertEqual(climate.target_temperature_min, 16.00) # third change - limit exceeded, setting to min self.loop.run_until_complete( asyncio.Task(climate.set_target_temperature(15.50))) self.assertEqual(climate.target_temperature_min, 16.00) self.assertEqual(climate.setpoint_shift, -6)
def test_start_server(self): class MyServer: def __init__(self, loop): self.server = None self.loop = loop async def handle_client(self, client_reader, client_writer): data = await client_reader.readline() client_writer.write(data) await client_writer.drain() client_writer.close() def start(self): sock = socket.socket() sock.bind(('127.0.0.1', 0)) self.server = self.loop.run_until_complete( asyncio.start_server(self.handle_client, sock=sock, loop=self.loop)) return sock.getsockname() def handle_client_callback(self, client_reader, client_writer): self.loop.create_task(self.handle_client(client_reader, client_writer)) def start_callback(self): sock = socket.socket() sock.bind(('127.0.0.1', 0)) addr = sock.getsockname() sock.close() self.server = self.loop.run_until_complete( asyncio.start_server(self.handle_client_callback, host=addr[0], port=addr[1], loop=self.loop)) return addr def stop(self): if self.server is not None: self.server.close() self.loop.run_until_complete(self.server.wait_closed()) self.server = None async def client(addr): reader, writer = await asyncio.open_connection( *addr, loop=self.loop) # send a line writer.write(b"hello world!\n") # read it back msgback = await reader.readline() writer.close() return msgback messages = [] self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) # test the server variant with a coroutine as client handler server = MyServer(self.loop) addr = server.start() msg = self.loop.run_until_complete(asyncio.Task(client(addr), loop=self.loop)) server.stop() self.assertEqual(msg, b"hello world!\n") # test the server variant with a callback as client handler server = MyServer(self.loop) addr = server.start_callback() msg = self.loop.run_until_complete(asyncio.Task(client(addr), loop=self.loop)) server.stop() self.assertEqual(msg, b"hello world!\n") self.assertEqual(messages, [])
def open(self, *args, **kwargs): """Takes the same arguments as the :class:`EnvironBuilder` class with some additions: You can provide a :class:`EnvironBuilder` or a WSGI environment as only argument instead of the :class:`EnvironBuilder` arguments and two optional keyword arguments (`as_tuple`, `buffered`) that change the type of the return value or the way the application is executed. .. versionchanged:: 0.5 If a dict is provided as file in the dict for the `data` parameter the content type has to be called `content_type` now instead of `mimetype`. This change was made for consistency with :class:`werkzeug.FileWrapper`. The `follow_redirects` parameter was added to :func:`open`. Additional parameters: :param as_tuple: Returns a tuple in the form ``(environ, result)`` :param buffered: Set this to True to buffer the application run. This will automatically close the application for you as well. :param follow_redirects: Set this to True if the `Client` should follow HTTP redirects. """ as_tuple = kwargs.pop('as_tuple', False) buffered = kwargs.pop('buffered', False) follow_redirects = kwargs.pop('follow_redirects', False) environ = None if not kwargs and len(args) == 1: if isinstance(args[0], EnvironBuilder): environ = args[0].get_environ() elif isinstance(args[0], dict): environ = args[0] if environ is None: builder = EnvironBuilder(*args, **kwargs) try: environ = builder.get_environ() finally: builder.close() try: loop = asyncio.get_event_loop() except: loop = asyncio.get_event_loop_policy().new_event_loop() asyncio.set_event_loop(loop) task = asyncio.Task( call_maybe_yield(self.run_wsgi_app, environ, buffered=buffered)) response = loop.run_until_complete(task) # handle redirects redirect_chain = [] while 1: status_code = int(response[1].split(None, 1)[0]) if status_code not in (301, 302, 303, 305, 307) \ or not follow_redirects: break new_location = response[2]['location'] new_redirect_entry = (new_location, status_code) if new_redirect_entry in redirect_chain: raise ClientRedirectError('loop detected') redirect_chain.append(new_redirect_entry) environ, response = self.resolve_redirect(response, new_location, environ, buffered=buffered) if self.response_wrapper is not None: response = self.response_wrapper(*response) if as_tuple: return environ, response return response
def start(self): self.new_task = asyncio.Task(self.connect()) self.tasks.append(self.new_task)
def test_start_server(self): class MyServer: def __init__(self, loop): self.server = None self.loop = loop @asyncio.coroutine def handle_client(self, client_reader, client_writer): data = yield from client_reader.readline() client_writer.write(data) def start(self): sock = socket.socket() sock.bind(('127.0.0.1', 0)) self.server = self.loop.run_until_complete( asyncio.start_server(self.handle_client, sock=sock, loop=self.loop)) return sock.getsockname() def handle_client_callback(self, client_reader, client_writer): task = asyncio.Task(client_reader.readline(), loop=self.loop) def done(task): client_writer.write(task.result()) task.add_done_callback(done) def start_callback(self): sock = socket.socket() sock.bind(('127.0.0.1', 0)) addr = sock.getsockname() sock.close() self.server = self.loop.run_until_complete( asyncio.start_server(self.handle_client_callback, host=addr[0], port=addr[1], loop=self.loop)) return addr def stop(self): if self.server is not None: self.server.close() self.loop.run_until_complete(self.server.wait_closed()) self.server = None @asyncio.coroutine def client(addr): reader, writer = yield from asyncio.open_connection( *addr, loop=self.loop) # send a line writer.write(b"hello world!\n") # read it back msgback = yield from reader.readline() writer.close() return msgback # test the server variant with a coroutine as client handler server = MyServer(self.loop) addr = server.start() msg = self.loop.run_until_complete(asyncio.Task(client(addr), loop=self.loop)) server.stop() self.assertEqual(msg, b"hello world!\n") # test the server variant with a callback as client handler server = MyServer(self.loop) addr = server.start_callback() msg = self.loop.run_until_complete(asyncio.Task(client(addr), loop=self.loop)) server.stop() self.assertEqual(msg, b"hello world!\n")