def handle(route: Route) -> None: response = page.request.fetch(route.request) route.fulfill( response=response, status=201, headers={"foo": "bar"}, content_type="text/plain", body="Yo, page!", )
def log_and_continue_request(route: Route, request: Request): resource_type = request.resource_type '''请求过滤''' if resource_type in ['image', 'media', 'eventsource', 'websocket']: route.abort() else: url_origin = request.url headers = request.headers method = request.method post_data_json = request.post_data_json print(url_origin, headers, method, post_data_json) if not url_origin: route.continue_() return if not self.filter_url_by_domain(url_origin) or self.filter_ext(url_origin): route.continue_() return http_har = dict() if method == 'POST' or method == 'PUT': post_data_origin = post_data_json post_data_handled = self._parse_post_data(post_data_origin) content_type = headers['content-type'] if 'content-type' in headers else '' http_har['originPostData'] = post_data_origin http_har['postData'] = post_data_handled http_har['contentType'] = content_type http_har['url'] = url_origin http_har['originUrl'] = url_origin http_har['method'] = method if method == 'GET': http_har = self.parse_static_url(url_origin) results.append(http_har) route.continue_()
def intercept(route: Route, request: Request): # 拦截前端跳转,主要方法是修改请求响应为204 TODO: 后续在遇到前端跳转的时候,优化hook逻辑 if request.is_navigation_request() and request.frame.parent_frame: request.response().status = 204 route.continue_() return # 尝试拦截后端跳转 if request.redirected_to: if request.post_data_json: request.response().status = 200 self.waiting_queue.put_nowait(request.redirected_to.url) else: ... route.continue_() return resource_type = request.resource_type # 过滤动态请求 if resource_type in ['image', 'media', 'eventsource', 'websocket']: route.abort() else: url_origin = request.url if not url_origin: route.continue_() return if not self.filter_ext(url=url_origin) and self.filter_url_by_domain(url=url_origin): headers = request.headers method = request.method post_data_json: dict = request.post_data_json http_har = dict() if method == 'POST' or method == 'PUT': post_data_origin = post_data_json post_data_handled = self._parse_post_data(post_data_origin) content_type = headers['content-type'] if 'content-type' in headers else '' http_har['originPostData'] = post_data_origin http_har['postData'] = post_data_handled http_har['contentType'] = content_type http_har['url'] = url_origin http_har['originUrl'] = url_origin http_har['method'] = method if method == 'GET': http_har = self.parse_static_url(url_origin) result.append(http_har) route.continue_()
def handle(route: Route) -> None: page.request.fetch(route.request) route.fulfill(status=201)
def handle(route: Route) -> None: response = page.request.fetch(route.request) route.fulfill(response=response, status=201, body="", headers={"content-length": "0"})
def handle_route(route: Route) -> None: response = page.request.fetch(route.request) route.fulfill(response=response)
def delete_foo_header(route: Route, request: Request): headers = request.all_headers() route.fallback(headers={**headers, "foo": None})
def capture_and_continue(route: Route, request: Request): intercepted_request.append(request) route.continue_()
def handler_with_header_mods(route: Route): route.fallback(headers={**route.request.headers, "FOO": "bar"})
def handler(route: Route): values.append(route.request.headers.get("foo")) values.append(route.request.header_value("FOO")) route.continue_()
def handler(route: Route): try: route.fulfill(response=47) except Exception: route.fallback()
def handler(route: Route, request: Request): route.fulfill(status=200, body=payload)
def handle(route: Route) -> None: response = page.request.fetch(route.request) route.fulfill(response=response, status=201, body="")
def handle_request(route: Route) -> None: request = playwright.request.new_context() response = request.get(server.PREFIX + "/simple.json") route.fulfill(response=response) request.dispose()