Best Python code snippet using devtools-proxy_python
proxy.py
Source:proxy.py
...32 tabs = app['tabs']33 if tabs.get(tab_id) is None:34 app['tabs'][tab_id] = {}35 # https://aiohttp.readthedocs.io/en/v1.0.0/faq.html#how-to-receive-an-incoming-events-from-different-sources-in-parallel36 task = app.loop.create_task(ws_browser_handler(request))37 app['tasks'].append(task)38 return await ws_client_handler(request)39async def ws_client_handler(request):40 app = request.app41 path_qs = request.path_qs42 tab_id = path_qs.split('/')[-1]43 url = f'ws://{app["chrome_host"]}:{app["chrome_port"]}{path_qs}'44 encode_id = app['f']['encode_id']45 client_id = len(app['clients'])46 log_prefix = f'[CLIENT {client_id}]'47 log_msg = app['f']['print']48 ws_client = WebSocketResponse()49 await ws_client.prepare(request)50 if client_id >= app['max_clients']:51 log_msg(log_prefix, 'CONNECTION FAILED')52 return ws_client53 app['clients'][ws_client] = {54 'id': client_id,55 'tab_id': tab_id,56 'subscriptions': set(), # TODO: Move subscriptions to separate entity57 }58 log_msg(log_prefix, 'CONNECTED')59 if app['tabs'][tab_id].get('ws') is None or app['tabs'][tab_id]['ws'].closed:60 session = aiohttp.ClientSession(loop=app.loop)61 app['sessions'].append(session)62 try:63 app['tabs'][tab_id]['ws'] = await session.ws_connect(url)64 except aiohttp.WSServerHandshakeError:65 log_msg(log_prefix, f'CONNECTION ERROR: {tab_id}')66 return ws_client67 async for msg in ws_client:68 if msg.type == WSMsgType.TEXT:69 if app['tabs'][tab_id]['ws'].closed:70 log_msg(log_prefix, 'RECONNECTED')71 break72 data = msg.json(loads=json.loads)73 data['id'] = encode_id(client_id, data['id'])74 log_msg(log_prefix, '>>', data)75 if data.get('method', '').endswith('.enable'):76 domain = data['method'].split('.')[0]77 app['clients'][ws_client]['subscriptions'].add(domain)78 elif data.get('method', '').endswith('.disable'):79 domain = data['method'].split('.')[0]80 if domain in app['clients'][ws_client]['subscriptions']:81 app['clients'][ws_client]['subscriptions'].remove(domain)82 app['tabs'][tab_id]['ws'].send_json(data, dumps=json.dumps)83 else:84 log_msg(log_prefix, 'DISCONNECTED')85 return ws_client86async def ws_browser_handler(request):87 log_prefix = '<<'88 app = request.app89 tab_id = request.path_qs.split('/')[-1]90 decode_id = app['f']['decode_id']91 log_msg = app['f']['print']92 timeout = 1093 interval = 0.194 for _ in range(math.ceil(timeout / interval)):95 if app['tabs'][tab_id].get('ws') is not None and not app['tabs'][tab_id]['ws'].closed:96 log_msg(f'[BROWSER {tab_id}]', 'CONNECTED')97 break98 await asyncio.sleep(interval)99 else:100 log_msg(f'[BROWSER {tab_id}]', 'DISCONNECTED')...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!