async def test_proxy_chain(): proxy = ProxyChain([ Proxy.from_url(SOCKS5_IPV4_URL), Proxy.from_url(SOCKS4_URL), Proxy.from_url(HTTP_PROXY_URL), ]) # noinspection PyTypeChecker status_code = await make_request(proxy=proxy, url=TEST_URL) assert status_code == 200
async def test_socks5_proxy_ipv4(rdns, resolve_host): proxy = Proxy.from_url(SOCKS5_IPV4_URL, rdns=rdns) status_code = await make_request( proxy=proxy, url=TEST_URL, resolve_host=resolve_host ) assert status_code == 200
async def async_rotate_fetch( url: str, protocol: ProxyProto = 'socks5', return_proxy: Boolean = False) -> Union[Data, Tuple[Data, Data]]: """ Rotate proxies and perform a GET request. Returns a Data object of `response.status_code`, `response.text`, and `response.headers`. :param url: URL to fetch. :param protocol: 'socks5' or 'http'; must correspond to your ActProxy proxies' type. :param return_proxy: Boolean; Return tuple(proxy, ProxyConnector) instead of just ProxyConnector. :return: A Data object of the response with resp.text, resp.status_code, and resp.headers """ url_parts = urlparse(url) url_port = 443 if url_parts.scheme == 'https' else 80 url_path = url_parts.path actproxy = one_hot_proxy() if proxy := AsyncProxy.from_url( f'{protocol}://{actproxy.username}:{actproxy.password}@{actproxy.host}:{actproxy.port}' ): sock = await proxy.connect(dest_host=url_parts.hostname, dest_port=url_port) reader, writer = await asyncio.open_connection( host=None, port=None, sock=sock, ssl=ssl.create_default_context(), server_hostname=url_parts.hostname, ) request = (b'GET ' + url_path.encode() + b' HTTP/1.1\r\n' b'Host: ' + url_parts.hostname.encode() + b'\r\n' b'Connection: close\r\n\r\n') writer.write(request) _resp = await reader.read(-1) body = _resp.decode('UTF-8').split('\r\n\r\n', 1)[-1] status_code = int( _resp.decode('UTF-8').split('\r\n', 1)[0].split(' ')[1]) headers = parse_it(_resp) response_data = Data(text=body, status_code=status_code, headers=headers) if return_proxy: return response_data, actproxy else: return response_data
async def _work(): proxy = Proxy.from_url('socks5://127.0.0.1:1080') sock = await proxy.connect(dest_host='127.0.0.1', dest_port=port) reader, writer = await asyncio.open_connection(host=None, port=None, sock=sock) writer.write(b'Ping\r\n') await reader.read(-1)
async def test_socks5_proxy_ipv4_with_auth_none(rdns): proxy = Proxy.from_url(SOCKS5_IPV4_URL_WO_AUTH, rdns=rdns) status_code = await make_request(proxy=proxy, url=TEST_URL) assert status_code == 200
async def test_http_proxy(): proxy = Proxy.from_url(HTTP_PROXY_URL) status_code = await make_request(proxy=proxy, url=TEST_URL) assert status_code == 200
async def test_socks5_proxy_ipv6(): proxy = Proxy.from_url(SOCKS5_IPV6_URL) status_code = await make_request(proxy=proxy, url=TEST_URL) assert status_code == 200
async def test_socks5_proxy_hostname_ipv4(url): proxy = Proxy.from_url(SOCKS5_IPV4_HOSTNAME_URL) status_code = await make_request(proxy=proxy, url=url) assert status_code == 200
async def test_socks4_proxy(url, rdns, resolve_host): proxy = Proxy.from_url(SOCKS4_URL, rdns=rdns) status_code = await make_request(proxy=proxy, url=url, resolve_host=resolve_host) assert status_code == 200