def _fetch(self, request, driver): print("Use Chrome Headless") driver.get(request.url_with_query) return Response(request=request, status_code=200, url=driver.current_url, headers=driver.get_cookies(), body=driver.page_source)
async def fetch(self, request): print("tornado 异步客户端发的请求") tornado_request = HTTPRequest(request.url_with_query, method=request.method.upper(), headers=request.headers) tornado_response = await self.async_http_client.fetch(tornado_request) return Response(request=request, status_code=tornado_response.code, url=tornado_response.effective_url, headers=tornado_response.headers, body=tornado_response.buffer.read())
async def fetch(self, request): io_loop = asyncio.get_event_loop() print("tornado 同步客户端发的请求") tornado_request = HTTPRequest(request.url_with_query, method=request.method.upper(), headers=request.headers) tornado_response = await io_loop.run_in_executor( None, self.http_client.fetch, tornado_request) #tornado_response = self.http_client.fetch(tornado_request) return Response(request=request, status_code=tornado_response.code, url=tornado_response.effective_url, headers=tornado_response.headers, body=tornado_response.buffer.read())
def fetch(self, request): '''根据request发起请求,构建Response对象''' if request.method.upper() == "GET": resp = requests.get(request.url_with_query, headers=request.headers) elif request.method.upper() == "POST": resp = requests.post(request.url_with_query, headers=request.headers, body=request.body) else: raise Exception("Only support GET or POST Method!") return Response(request, status_code=resp.status_code, url=resp.url, headers=resp.headers, body=resp.content)
async def fetch(self, request): '''根据request发起请求,构建response对象''' io_loop = asyncio.get_event_loop() if request.method.upper() == "GET": future = io_loop.run_in_executor(None, requests.get, request.url_with_query, request.headers) resp = await future #resp = requests.get(request.url_with_query,headers=request.headers) elif request.method.upper() == "POST": future = io_loop.run_in_executor(None, requests.post, request.url_with_query, request.headers, request.body) resp = await future #resp = requests.post(request.url_with_query,headers=request.headers,body=request.body) else: raise Exception("Only support GET or POST Method!") return Response(request, status_code=resp.status_code, url=resp.url, headers=resp.headers, body=resp.content)