def test_async_get(self): async_client_global = AsyncClient(success_callback=self.success_callback, fail_callback=self.fail_callback) params = self.random_dict(10) url = build_url("get") self.assertEquals(async_client_global.get(url, params=params), async_client_global) self.assertEquals(len(async_client_global._data_queue), 1) # Test process_func def process_func(num_processed, remaining, num_urls, success_len, error_len): print("\nProcess {0} {1} {2} {3} {4}".format(num_processed, remaining, num_urls, success_len, error_len)) self.assertEquals(num_urls, 2) def fail_callback(request, errno, errmsg, async_client, opener): self.assertTrue(isinstance(request, Request)) self.assertTrue(isinstance(async_client, AsyncClient)) self.assertEquals(async_client, async_client_global) self.assertEquals(errno, 6) self.assertEquals(errmsg, "Couldn't resolve host '{0}'".format(request.url[7:])) async_client_global.get("http://fwbefrubfbrfybghbfb4gbyvrv.com", params=params, fail_callback=fail_callback) self.assertEquals(len(async_client_global._data_queue), 2) async_client_global.start(process_func)
def test_AsyncClient_core(self): async_client = AsyncClient(size=20) self.assertEquals(async_client._num_conn, 20) self.assertEquals(async_client._remaining, 0) self.assertEquals(async_client.success_callback, None) self.assertEquals(async_client.fail_callback, None) self.assertEquals(async_client._openers_pool, None) self.assertEquals(async_client._data_queue, []) self.assertEquals(async_client.connections_count, 0) async_client.add_handler(url=build_url("/get"), method="get", params={"get1": "get1 value", "get2": "get2 value"}, success_callback=self.success_callback, fail_callback=self.fail_callback) self.assertEquals(len(async_client._data_queue), 1) self.assertTrue(isinstance(async_client._data_queue[0], dict)) params = self.random_dict(10) async_client.get(url=build_url("/get"), params=params, success_callback=self.success_callback, fail_callback=self.fail_callback) self.assertTrue(isinstance(async_client._data_queue[1], dict)) self.assertEquals(async_client._data_queue[1]['params'], params) self.assertEquals(async_client.connections_count, 2)
def success_callback(response, **kwargs): """This function call when response successed """ print("success callback") print(response, response.request) print(response.headers) print(response.content) print(kwargs) def fail_callback(request, opener, **kwargs): """Collect errors """ print("fail callback") print(request, opener) print(kwargs) # In kwargs we can pass default arguments # Add pre and post callback call hooks async_client = AsyncClient(success_callback=success_callback, fail_callback=fail_callback) results = [] fails = [] async_client.get('http://h.wrttn.me/get') async_client.get('http://httpbin.org/get', success_callback=success_callback, fail_callback=fail_callback) async_client.start()
# get_ip() url = 'https://blog.csdn.net/fly_zhyu/article/details/76408158' client = AsyncClient(success_callback=success_call, fail_callback=fail_call) # request = Request(network_interface='185.101.107.106',method='GET', url=url) # print(request.response) # request.send() # response = request.response # print(response.content) # print(request.make_response()) # request._network_interface='172.17.35.241' # # # opener = pycurl.Curl() da = { 'network_interface': 'eth0:5', } da = { 'network_interface': '185.101.107.122', } client.add_handler(**da) # client.configure_opener(opener, data=da) client.get(url) client.start() # import lz4 # # egg: http://python-lz4.readthedocs.io/en/stable/quickstart.html#simple-usage # compressed = lz4.frame.compress('10111001110010111010111111010') # decompressed = lz4.frame.decompress(compressed) # print(decompressed)
""" print("success callback") print(response, response.request) print(response.headers) print(response.content) print(kwargs) def fail_callback(request, opener, **kwargs): """Collect errors """ print("fail callback") print(request, opener) print(kwargs) # In kwargs we can pass default arguments # Add pre and post callback call hooks async_client = AsyncClient(success_callback=success_callback, fail_callback=fail_callback) results = [] fails = [] async_client.get('http://h.wrttn.me/get') async_client.get('http://httpbin.org/get', success_callback=success_callback, fail_callback=fail_callback) async_client.start()