def pack_response(response, *, loop): future = asyncio.Future(loop=loop) future.set_result(response) return future
def test_listen_for_order_book_snapshots(self, mock_get_trading_pairs, mock_get_snapshot): """ Example order book message added to the queue: LiquidOrderBookMessage( type = < OrderBookMessageType.SNAPSHOT: 1 > , content = { 'buy_price_levels': [ ['181.95138', '0.69772000'], ... ], 'sell_price_levels': [ ['182.11620', '0.32400000'], ... ], 'trading_pair': 'BTC-USDC' }, timestamp = 1573041256.2376761) """ loop = asyncio.get_event_loop() # Instantiate empty async queue and make sure the initial size is 0 q = asyncio.Queue() self.assertEqual(q.qsize(), 0) # Mock Future() object return value as the request response f1 = asyncio.Future() f1.set_result( { **FixtureLiquid.SNAPSHOT_2, 'trading_pair': 'ETH-USD', 'product_id': 27 } ) f2 = asyncio.Future() f2.set_result( { **FixtureLiquid.SNAPSHOT_1, 'trading_pair': 'LCX-BTC', 'product_id': 538 } ) mock_get_snapshot.side_effect = [f1, f2] # Mock get trading pairs mocked_trading_pairs = ['ETH-USD', 'LCX-BTC'] f = asyncio.Future() f.set_result(mocked_trading_pairs) mock_get_trading_pairs.return_value = f # Listening for tracking pairs within the set timeout timeframe timeout = 6 print('{class_name} test {test_name} is going to run for {timeout} seconds, starting now'.format( class_name=self.__class__.__name__, test_name=inspect.stack()[0][3], timeout=timeout)) try: loop.run_until_complete( # Force exit from event loop after set timeout seconds asyncio.wait_for( LiquidAPIOrderBookDataSource().listen_for_order_book_snapshots(ev_loop=loop, output=q), timeout=timeout ) ) except concurrent.futures.TimeoutError as e: print(e) # Make sure that the number of items in the queue after certain seconds make sense # For instance, when the asyncio sleep time is set to 5 seconds in the method # If we configure timeout to be the same length, only 1 item has enough time to be received self.assertGreaterEqual(q.qsize(), 1) # Validate received response has correct data types first_item = q.get_nowait() self.assertIsInstance(first_item, LiquidOrderBookMessage) self.assertIsInstance(first_item.type, OrderBookMessageType) # Validate order book message type self.assertEqual(first_item.type, OrderBookMessageType.SNAPSHOT) # Validate snapshot received matches with the original snapshot received from API self.assertEqual(first_item.content['bids'], FixtureLiquid.SNAPSHOT_2['buy_price_levels']) self.assertEqual(first_item.content['asks'], FixtureLiquid.SNAPSHOT_2['sell_price_levels']) # Validate the rest of the content self.assertEqual(first_item.content['trading_pair'], mocked_trading_pairs[0]) self.assertEqual(first_item.content['product_id'], 27)
present(response_data, options) else: print(colored(response_data.code, options, 'red'), file=sys.stderr) present(response_data, options, file=sys.stderr) sys.exit(1) if options.observe: exit_reason = await observation_is_over print("Observation is over: %r"%(exit_reason,), file=sys.stderr) finally: if not requester.response.done(): requester.response.cancel() if options.observe and not requester.observation.cancelled: requester.observation.cancel() interactive_expecting_keyboard_interrupt = asyncio.Future() async def interactive(): global interactive_expecting_keyboard_interrupt context = await aiocoap.Context.create_client_context() while True: try: # when http://bugs.python.org/issue22412 is resolved, use that instead line = await asyncio.get_event_loop().run_in_executor(None, lambda: input("aiocoap> ")) except EOFError: line = "exit" line = shlex.split(line) if not line: continue
async def wait_for_request(self, path): if path in self.request_subscribers: return await self.request_subscribers[path] future = asyncio.Future() self.request_subscribers[path] = future return await future
async def send_command(self, cmd, data=None, user=None, timeout=10.0, retry=2) -> (User, dict): assert 0.0 < timeout and 0 < retry if self.f_stop: raise PeerToPeerError('already p2p-python closed') uuid = random.randint(10, 0xffffffff) # 1. Make template temperate = { 'type': T_REQUEST, 'cmd': cmd, 'data': data, 'time': time(), 'uuid': uuid, } f_udp = False # 2. Setup allows to send nodes if len(self.core.user) == 0: raise PeerToPeerError('no client connection found') elif cmd == Peer2PeerCmd.BROADCAST: allows = self.core.user.copy() f_udp = True elif user is None: user = random.choice(self.core.user) allows = [user] elif user in self.core.user: allows = [user] else: raise PeerToPeerError("Not found user in list") # 3. Send message to a node or some nodes start = time() future = asyncio.Future() self.result_futures[uuid] = future # get best timeout if user is None: # broadcast-cmd best_timeout = timeout / retry else: # inner-cmd/direct-cmd average = user.average_process_time() if average is None: best_timeout = timeout / retry else: best_timeout = min(5.0, max(1.0, average * 10)) f_timeout = False for _ in range(retry): send_num = await self._send_many_users(item=temperate, allows=allows, denys=[], allow_udp=f_udp) send_time = time() if send_num == 0: raise PeerToPeerError( f"We try to send no users? {len(self.core.user)}user connected" ) if Debug.P_SEND_RECEIVE_DETAIL: log.debug(f"send({send_num}) => {temperate}") # 4. Get response try: # avoid future canceled by wait_for await asyncio.wait_for(asyncio.shield(future), best_timeout) if 5.0 < time() - start: log.debug( f"id={uuid}, command {int(time()-start)}s blocked by {user}" ) if user is not None: user.process_time.append(time() - send_time) break except (asyncio.TimeoutError, asyncio.CancelledError): log.debug(f"id={uuid}, timeout now, cmd({cmd}) to {user}") except Exception: log.debug("send_command exception", exc_info=True) # 5. will lost packet log.debug(f"id={uuid}, will lost packet and retry") else: f_timeout = True # 6. timeout if f_timeout and user: if user.closed or not await self.core.ping(user): # already closed or ping failed -> reconnect await self.core.try_reconnect( user, reason="ping failed on send_command") else: log.debug("timeout and retry but ping success") # 7. return result if future.done(): return future.result() else: future.cancel() raise asyncio.TimeoutError("timeout cmd")
async def test_lifted_unknown(self): settings.SETTINGS.dry_run = True fut = asyncio.Future() fut.set_result(UNKNOWN) out = Output.from_input({"foo": "foo", "bar": UNKNOWN, "baz": fut}) self.assertFalse(await out.is_known()) r1 = out["foo"] self.assertTrue(await r1.is_known()) self.assertEqual(await r1.future(with_unknowns=True), "foo") r2 = out["bar"] self.assertFalse(await r2.is_known()) self.assertEqual(await r2.future(with_unknowns=True), UNKNOWN) r3 = out["baz"] self.assertFalse(await r3.is_known()) self.assertEqual(await r3.future(with_unknowns=True), UNKNOWN) r4 = out["baz"]["qux"] self.assertFalse(await r4.is_known()) self.assertEqual(await r4.future(with_unknowns=True), UNKNOWN) out = Output.from_input(["foo", UNKNOWN]) r5 = out[0] self.assertTrue(await r5.is_known()) self.assertEqual(await r5.future(with_unknowns=True), "foo") r6 = out[1] self.assertFalse(await r6.is_known()) self.assertEqual(await r6.future(with_unknowns=True), UNKNOWN) out = Output.all( Output.from_input("foo"), Output.from_input(UNKNOWN), Output.from_input( [Output.from_input(UNKNOWN), Output.from_input("bar")])) self.assertFalse(await out.is_known()) r7 = out[0] self.assertTrue(await r7.is_known()) self.assertEqual(await r7.future(with_unknowns=True), "foo") r8 = out[1] self.assertFalse(await r8.is_known()) self.assertEqual(await r8.future(with_unknowns=True), UNKNOWN) r9 = out[2] self.assertFalse(await r9.is_known()) r10 = r9[0] self.assertFalse(await r10.is_known()) self.assertEqual(await r10.future(with_unknowns=True), UNKNOWN) r11 = r9[1] self.assertTrue(await r11.is_known()) self.assertEqual(await r11.future(with_unknowns=True), "bar") out_dict = Output.all(foo=Output.from_input("foo"), unknown=Output.from_input(UNKNOWN), arr=Output.from_input([ Output.from_input(UNKNOWN), Output.from_input("bar") ])) self.assertFalse(await out_dict.is_known()) r12 = out_dict["foo"] self.assertTrue(await r12.is_known()) self.assertEqual(await r12.future(with_unknowns=True), "foo") r13 = out_dict["unknown"] self.assertFalse(await r13.is_known()) self.assertEqual(await r13.future(with_unknowns=True), UNKNOWN) r14 = out_dict["arr"] self.assertFalse(await r14.is_known()) r15 = r14[0] self.assertFalse(await r15.is_known()) self.assertEqual(await r15.future(with_unknowns=True), UNKNOWN) r16 = r14[1] self.assertTrue(await r16.is_known()) self.assertEqual(await r16.future(with_unknowns=True), "bar")
async def test_dict(self): fut = asyncio.Future() fut.set_result(99) test_dict = {"a": 42, "b": fut} prop = await rpc.serialize_property(test_dict, []) self.assertDictEqual({"a": 42, "b": 99}, prop)
def run_async(): print("async example") print("=============") result = [] def handle_future(future): result.extend(future.result()) print(future.result()) # [unicode(x.strip()) if x is not None else '' for x in row] record_list = [] for row in result: if len(row.records) == 0: continue record_list.extend(row.records) record_list = [item.values for item in record_list] # record_list = [[record for record in row.records] for row in result] df = pd.DataFrame.from_records(record_list, columns=None,) df.to_csv('../output/crm_test.csv', encoding='gbk') asyncio.ensure_future(logout()) loop = asyncio.get_event_loop() wsdl = 'http://10.21.2.75:8080/service/LBEBusiness?wsdl' cache = SqliteCache(path='./sqlite.db', timeout=3600) transport = AsyncTransport(loop, cache=cache) client = zeep.Client(wsdl, transport=transport) # login_ans = client.service.login("ZXQH_GPXZ", "GPXZ123321", "myapp", "plain", ""), # sessionId = login_ans[0].sessionId async def login(future): result = await client.service.login("ZXQH_GPXZ", "GPXZ123321", "myapp", "plain", ""), global sessionId sessionId = result[0].sessionId future.set_result(result[0].sessionId) async def logout(): global sessionId result = await client.service.logout(sessionId) result = await transport.session.close() loop.stop() def send_quests(future): sessionId = future.result() df = pd.read_excel('./客户名单(服务器托管).xlsx') df = df.apply(pd.to_numeric, errors='ignore') user_ids = df['资金账号'].tolist() user_ids = user_ids[0:20] tasks = [ setup_req(sessionId, str(user_id)) for user_id in user_ids ] future2 = asyncio.gather(*tasks, return_exceptions=True) future2.add_done_callback(handle_future) async def setup_req(sessionId, user_id,): lbParameter_type = client.get_type('ns0:lbParameter') queryOption_type = client.get_type('ns0:queryOption') par_dict = {"KSRQ": "2018-11-01", "JSRQ": "2018-11-10", "KHH": user_id} params = [] for key in par_dict: val = par_dict[key] temp_lbParameter = lbParameter_type(name=key, value=val) params.append(temp_lbParameter) valueOption_type = client.get_type('ns0:valueOption') valueOption = valueOption_type('VALUE') batchNo = 1 batchSize = 3000 mqueryOption = queryOption_type(batchNo=batchNo, batchSize=batchSize, queryCount=True, valueOption=valueOption) # waits 1 second # delay_seconds = random.randrange(5) # await asyncio.sleep(delay_seconds) # await asyncio.sleep(1) result = await client.service.query(sessionId, "cxSqlKHCJMX", params, "", mqueryOption) return result lbParameter_type = client.get_type('ns0:lbParameter') queryOption_type = client.get_type('ns0:queryOption') par_dict = {"KSRQ": "2018-11-01", "JSRQ": "2018-11-10", "KHH": "0000050067"} params = [] for key in par_dict: val = par_dict[key] temp_lbParameter = lbParameter_type(name=key, value=val) params.append(temp_lbParameter) valueOption_type = client.get_type('ns0:valueOption') valueOption = valueOption_type('VALUE') batchNo = 1 batchSize = 3000 mqueryOption = queryOption_type(batchNo=batchNo, batchSize=batchSize, queryCount=True, valueOption=valueOption) # st = time.time() # loop.run_until_complete(future) # loop.run_until_complete(transport.session.close()) # print("time: %.2f" % (time.time() - st)) # print("result: %s" % result) # print("") # print("sessionId:{sessionId}".format(sessionId=sessionId)) loop = asyncio.get_event_loop() future = asyncio.Future() asyncio.ensure_future(login(future)) future.add_done_callback(send_quests) try: loop.run_forever() finally: loop.close() return result
def receive_chunk(self, guild_id): future = asyncio.Future(loop=self.loop) listener = Listener(ListenerType.chunk, future, lambda s: s.id == guild_id) self._listeners.append(listener) return future
raise error log.error.assert_called_once_with( "a different message", exc_info=(ValueError, error, mock.ANY) ) log.error.reset_mock() with assertRaises(TypeError, "wat"): with hp.just_log_exceptions(log, message="a different message", reraise=[TypeError]): raise TypeError("wat") log.assert_not_called() describe "TaskHolder": it "takes in a final future": final_future = asyncio.Future() holder = hp.TaskHolder(final_future) assert holder.ts == [] assert holder.final_future is final_future async it "can take in tasks": called = [] async def wait(amount): try: await asyncio.sleep(amount) finally: called.append(amount) final_future = asyncio.Future() async with hp.TaskHolder(final_future) as ts:
def __init__(self, *args, **kwargs): self.__exitcode = asyncio.Future() self.initializing = asyncio.Task(self.start(*args, **kwargs))
def create_future(loop): return asyncio.Future(loop=loop)
def wait_for(self, source, kls): assert (source, kls) not in self.waiters fut = asyncio.Future() self.waiters[(source, kls)] = fut return fut
def __init__(self, *, host=None, port=None, secure=None, timeout=10, max_size=2**20, max_queue=2**5, loop=None, legacy_recv=False): self.host = host self.port = port self.secure = secure self.timeout = timeout self.max_size = max_size # Store a reference to loop to avoid relying on self._loop, a private # attribute of StreamReaderProtocol, inherited from FlowControlMixin. if loop is None: loop = asyncio.get_event_loop() self.loop = loop self.legacy_recv = legacy_recv stream_reader = asyncio.StreamReader(loop=loop) super().__init__(stream_reader, self.client_connected, loop) self.reader = None self.writer = None self.request_headers = None self.raw_request_headers = None self.response_headers = None self.raw_response_headers = None self.subprotocol = None # Code and reason must be set when the closing handshake completes. self.close_code = None self.close_reason = '' # Futures tracking steps in the connection's lifecycle. # Set to True when the opening handshake has completed properly. self.opening_handshake = asyncio.Future(loop=loop) # Set to True when the closing handshake has completed properly and to # False when the connection terminates abnormally. self.closing_handshake = asyncio.Future(loop=loop) # Set to None when the connection state becomes CLOSED. self.connection_closed = asyncio.Future(loop=loop) # Queue of received messages. self.messages = asyncio.queues.Queue(max_queue, loop=loop) # Mapping of ping IDs to waiters, in chronological order. self.pings = collections.OrderedDict() # Task managing the connection, initalized in self.client_connected. self.worker_task = None # In a subclass implementing the opening handshake, the state will be # CONNECTING at this point. if self.state == OPEN: self.opening_handshake.set_result(True)
async def test__ensure_smart_enabled__enabling_smart_failed(): with patch("middlewared.etc_files.smartd.run") as run: run.return_value = asyncio.Future() run.return_value.set_result(Mock(stdout="SMART Disabled", returncode=1)) assert await ensure_smart_enabled(["/dev/ada0"]) is False
#Note: async requires python >=3.5 import asyncio import signal import websockets async def echo(websocket, path): while True: try: print("Serving") msg = await websocket.recv() except websockets.ConnectionClosed: pass else: await websocket.send(msg) async def echo_server(stop): async with websockets.serve(echo, 'localhost', 8765): await stop loop = asyncio.get_event_loop() # The stop condition is set when receiving SIGTERM. stop = asyncio.Future() #loop.add_signal_handler(signal.SIGTERM, stop.set_result, None) # Run the server until the stop condition is met. loop.run_until_complete(echo_server(stop))
async def test__annotate_disk_for_smart__skips_device_without_args(): with patch("middlewared.etc_files.smartd.get_smartctl_args") as get_smartctl_args: get_smartctl_args.return_value = asyncio.Future() get_smartctl_args.return_value.set_result(None) assert await annotate_disk_for_smart({"/dev/ada1": {"driver": "ata"}}, {"disk_name": "/dev/ada1"}) is None
def awaitable_voice_client_play(func, player, loop): f = asyncio.Future() after = lambda e: loop.call_soon_threadsafe(lambda: f.set_result(e)) func(player, after=after) return f
async def test_future(self): fut = asyncio.Future() fut.set_result(42) prop = await rpc.serialize_property(fut, []) self.assertEqual(42, prop)
def __init__(self, log: List[str], rootfs: str) -> None: self._log = log self._rootfs = rootfs self._stub_bus_connected = asyncio.Future()
def apply(v): fut = asyncio.Future() fut.set_result("inner") return fut
async def evaluate_javascript(self, javascript): js_value = asyncio.Future() self.native.evaluateJavascript(str(javascript), ReceiveString(js_value.set_result)) return await js_value
async def type_request(self, user: User, item: dict, push_time: float): temperate = { 'type': T_RESPONSE, 'cmd': item['cmd'], 'data': None, 'time': None, 'received': push_time, 'uuid': item['uuid'] } allows: List[User] = list() denys: List[User] = list() ack_list: List[User] = list() ack_status: Optional[bool] = None allow_udp = False if item['cmd'] == Peer2PeerCmd.PING_PONG: temperate['data'] = { 'ping': item['data'], 'pong': time(), } allows.append(user) elif item['cmd'] == Peer2PeerCmd.BROADCAST: if item['uuid'] in self.broadcast_status: # already get broadcast data, only send ACK future = self.broadcast_status[item['uuid']] # send ACK after broadcast_check finish await asyncio.wait_for(future, TIMEOUT) ack_status = future.result() ack_list.append(user) elif item['uuid'] in self.result_futures: # I'm broadcaster, get from ack ack_status = True ack_list.append(user) else: # set future future = asyncio.Future() self.broadcast_status[item['uuid']] = future # try to check broadcast data if asyncio.iscoroutinefunction(self.broadcast_check): broadcast_result = await asyncio.wait_for( self.broadcast_check(user, item['data']), TIMEOUT) else: broadcast_result = self.broadcast_check(user, item['data']) # set broadcast result future.set_result(broadcast_result) # prepare response if broadcast_result: user.score += 1 # send ACK ack_status = True ack_list.append(user) # broadcast to all allows = self.core.user.copy() denys.append(user) temperate['type'] = T_REQUEST temperate['data'] = item['data'] allow_udp = True else: user.warn += 1 # send ACK ack_status = False ack_list.append(user) elif item['cmd'] == Peer2PeerCmd.GET_PEER_INFO: # [[(host,port), header],..] temperate['data'] = [ (host_port, header.getinfo()) for host_port, header in self.peers.copy().items() ] allows.append(user) elif item['cmd'] == Peer2PeerCmd.GET_NEARS: # [[(host,port), header],..] temperate['data'] = [(user.get_host_port(), user.header.getinfo()) for user in self.core.user] allows.append(user) elif item['cmd'] == Peer2PeerCmd.CHECK_REACHABLE: try: port = item['data']['port'] except Exception: port = user.header.p2p_port try: temperate['data'] = await asyncio.wait_for( is_reachable(host=user.host_port[0], port=port), TIMEOUT) except Exception: temperate['data'] = False allows.append(user) elif item['cmd'] == Peer2PeerCmd.DIRECT_CMD: data = item['data'] if self.event.have_event(data['cmd']): allows.append(user) temperate['data'] = await asyncio.wait_for( self.event.ignition(user, data['cmd'], data['data']), TIMEOUT) else: log.debug(f"not found request cmd '{item['cmd']}'") # send message temperate['time'] = time() send_count = await self._send_many_users(item=temperate, allows=allows, denys=denys, allow_udp=allow_udp) # send ack ack_count = 0 if len(ack_list) > 0: assert ack_status is not None ack_temperate = temperate.copy() ack_temperate['type'] = T_ACK ack_temperate['data'] = ack_status ack_count = await self._send_many_users(item=ack_temperate, allows=ack_list, denys=[]) # debug if Debug.P_SEND_RECEIVE_DETAIL: log.debug(f"reply => {temperate}") log.debug( f"status => all={len(self.core.user)} send={send_count} ack={ack_count}" )
import asyncio # pip install asyncio async def slow_operations(future): await asyncio.sleep(2) future.set_result('Future is donde') def got_result(future): print(future.result()) loop.stop() loop = asyncio.get_event_loop() future = asyncio.Future() asyncio.ensure_future(slow_operations(future)) future.add_done_callback(got_result) try: loop.run_forever() finally: loop.close()
def __init__(self, loop=None): self.state = 'INITIAL' self.nbytes = 0 if loop is not None: self.done = asyncio.Future(loop=loop)
def post_async(self, event: str, **kwargs: dict) -> asyncio.Future: """Post event and wait until all handlers are done.""" future = asyncio.Future( loop=self.machine.clock.loop) # type: asyncio.Future self.post(event, partial(self._set_result, _future=future), **kwargs) return future
def test_get_active_exchange_markets(self, mock_get_exchange_markets_data): """ Test end to end flow from pinging Liquid API for markets and exchange data all the way to extract out needed information such as trading_pairs, prices, and volume information. """ loop = asyncio.get_event_loop() # Mock Future() object return value as the request response f = asyncio.Future() f.set_result(FixtureLiquid.EXCHANGE_MARKETS_DATA) mock_get_exchange_markets_data.return_value = f all_markets_df = loop.run_until_complete( LiquidAPIOrderBookDataSource.get_active_exchange_markets()) # loop.close() # Check DF type self.assertIsInstance(all_markets_df, pd.DataFrame) # Check DF dimension self.assertEqual(all_markets_df.shape, (7, 29)) # (num of rows, num of cols) # Check DF indices self.assertListEqual( all_markets_df.index.to_list(), ['BTC-USD', 'ETH-USD', 'BTC-USDC', 'ETH-USDC', 'LCX-BTC', 'STAC-ETH', 'WLO-BTC'] ) # Check DF column names self.assertListEqual( sorted(all_markets_df.columns), [ 'USDVolume', 'base_currency', 'btc_minimum_withdraw', 'cfd_enabled', 'code', 'currency', 'currency_pair_code', 'disabled', 'fiat_minimum_withdraw', 'high_market_ask', 'id', 'indicator', 'last_event_timestamp', 'last_price_24h', 'last_traded_price', 'last_traded_quantity', 'low_market_bid', 'maker_fee', 'margin_enabled', 'market_ask', 'market_bid', 'name', 'product_type', 'pusher_channel', 'quoted_currency', 'symbol', 'taker_fee', 'volume', 'volume_24h' ] ) # Check DF values self.assertEqual( all_markets_df.loc['BTC-USD'].last_traded_price, '7470.49746') # Check DF order, make sure it's sorted by USDVolume col in desending order usd_volumes = all_markets_df.loc[:, 'USDVolume'].to_list() self.assertListEqual( usd_volumes, sorted(usd_volumes, reverse=True), "The output usd volumes should remain the same after being sorted again")
def test_future(self): f = asyncio.Future() f.set_result(1) self.assertEqual(sync(f), 1)
async def single_request(args, context=None): parser = build_parser() options = parser.parse_args(args) pretty_print_modules = aiocoap.defaults.prettyprint_missing_modules() if pretty_print_modules and \ (options.color is True or options.pretty_print is True): parser.error("Color and pretty printing require the following" " additional module(s) to be installed: %s" % ", ".join(pretty_print_modules)) if options.color is None: options.color = sys.stdout.isatty() and not pretty_print_modules if options.pretty_print is None: options.pretty_print = sys.stdout.isatty() and not pretty_print_modules configure_logging((options.verbose or 0) - (options.quiet or 0)) try: code = getattr(aiocoap.numbers.codes.Code, options.method.upper()) except AttributeError: try: code = aiocoap.numbers.codes.Code(int(options.method)) except ValueError: raise parser.error("Unknown method") if context is None: context = await aiocoap.Context.create_client_context() if options.credentials is not None: apply_credentials(context, options.credentials, parser.error) request = aiocoap.Message(code=code, mtype=aiocoap.NON if options.non else aiocoap.CON) try: request.set_request_uri(options.url) except ValueError as e: raise parser.error(e) if not request.opt.uri_host and not request.unresolved_remote: raise parser.error("Request URLs need to be absolute.") if options.accept: try: request.opt.accept = int(options.accept) except ValueError: try: request.opt.accept = aiocoap.numbers.media_types_rev[options.accept] except KeyError: raise parser.error("Unknown accept type") if options.observe: request.opt.observe = 0 observation_is_over = asyncio.Future() if options.content_format: try: request.opt.content_format = int(options.content_format) except ValueError: try: request.opt.content_format = aiocoap.numbers.media_types_rev[options.content_format] except KeyError: raise parser.error("Unknown content format") if options.payload: if options.payload.startswith('@'): filename = options.payload[1:] if filename == "-": f = sys.stdin.buffer else: f = open(filename, 'rb') try: request.payload = f.read() except OSError as e: raise parser.error("File could not be opened: %s"%e) else: if aiocoap.numbers.media_types.get(request.opt.content_format, "").startswith("application/cbor"): try: import cbor except ImportError as e: raise parser.error("CBOR recoding not available (%s)" % e) import json try: decoded = json.loads(options.payload) except json.JSONDecodeError as e: raise parser.error("JSON recoding failed. Make sure quotation marks are escaped from the shell. JSON error: %s" % e) request.payload = cbor.dumps(decoded) else: request.payload = options.payload.encode('utf8') if options.proxy is None: interface = context else: interface = aiocoap.proxy.client.ProxyForwarder(options.proxy, context) try: requester = interface.request(request) if options.observe: requester.observation.register_errback(observation_is_over.set_result) requester.observation.register_callback(lambda data, options=options: incoming_observation(options, data)) try: response_data = await requester.response except socket.gaierror as e: print("Name resolution error:", e, file=sys.stderr) sys.exit(1) except OSError as e: text = str(e) if not text: text = repr(e) if not text: # eg ConnectionResetError flying out of a misconfigured SSL server text = type(e) print("Error:", text, file=sys.stderr) sys.exit(1) if response_data.code.is_successful(): present(response_data, options) else: print(colored(response_data.code, options, 'red'), file=sys.stderr) present(response_data, options, file=sys.stderr) sys.exit(1) if options.observe: exit_reason = await observation_is_over print("Observation is over: %r"%(exit_reason,), file=sys.stderr) finally: if not requester.response.done(): requester.response.cancel() if options.observe and not requester.observation.cancelled: requester.observation.cancel()
def __await__(self): future = asyncio.Future(loop=self.loop) future.set_result(self) result = yield from future return result