def pretty_print(line, prev_time): newtime = mt() delta = newtime - prev_time out = re.search(r'(?P<id>.+)\s(?P<data>.+)', line).groupdict() message = {'id': out['id'], 'data': out['data'], 'delta': delta} print( '| {delta:010.4f} | 0x{id:8s} | 0x{data:16s} |'.format(**message)) return newtime, message
def sniff(self): ''' Read messages and print, until stopped. Messages will be saved Additionally logs time delta between received messages, alongside id and data. Outputs are additionally saved in dictionary format to self.messages, in the order that they are recieved. ''' print("Listening for CAN messages...") print(" ----------------------------------------------") print("| Delta | Id | Data |") prev = mt() while True: try: line = self.cread()[0].strip('\r\n') if line: prev, message = self.pretty_print(line, prev) self.messages.append(message) else: pass except KeyboardInterrupt: print(" ----------------------------------------------") break
return None async def cities_count(session): html = await fetch_html(session) soup = BS(html, "html.parser") div = soup.find(id="stats") if div: count_s = div.b.string try: count = int(count_s) except ValueError: count = -1 return count return -1 async def main(): async with aiohttp.ClientSession(trust_env=True) as session: # Если сертификат сервера не нравится aiohttp, то можно отключить ssl # conn = aiohttp.TCPConnector(ssl=False) # async with aiohttp.ClientSession(connector=conn) as session: count = await cities_count(session) print('Count:', count) print('Asynchronous requests') t = mt() asyncio.run(main()) print('Finishing in {} sec.'.format(mt() - t))