示例#1
0
    def test_add_handler(self):
        async_client = AsyncClient()
        data = self.request_params()

        self.assertRaises(InterfaceError, async_client.add_handler, **data)

        data['success_callback'] = lambda **kwargs: kwargs
        data['fail_callback'] = lambda **kwargs: kwargs

        async_client.add_handler(**data)
        self.assertEquals(async_client._data_queue[0], data)
        self.assertEquals(async_client._num_urls, 1)
        self.assertEquals(async_client._remaining, 1)
示例#2
0
    def test_add_handler(self):
        async_client = AsyncClient()
        data = self.request_params()


        self.assertRaises(InterfaceError, async_client.add_handler, **data)

        data['success_callback'] = lambda **kwargs: kwargs
        data['fail_callback'] = lambda **kwargs: kwargs

        async_client.add_handler(**data)
        self.assertEquals(async_client._data_queue[0], data)
        self.assertEquals(async_client._num_urls, 1)
        self.assertEquals(async_client._remaining, 1)
示例#3
0
    def test_AsyncClient_contextmanager(self):
        with AsyncClient(success_callback=self.success_callback,
                         fail_callback=self.fail_callback) as async_client_global:

            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)
示例#4
0
    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):
            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, "Could not 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)
示例#5
0
    def test_setup_opener(self):
        async_client = AsyncClient()

        data = self.random_dict(10)
        data['url'] = build_url("get")
        data['method'] = 'get'
        opener = async_client.get_opener()

        self.assertEquals(getattr(opener, 'success_callback', None), None)
        self.assertEquals(getattr(opener, 'fail_callback', None), None)
        self.assertEquals(getattr(opener, 'request', None), None)

        data['success_callback'] = lambda **kwargs: kwargs
        data['fail_callback'] = lambda **kwargs: kwargs

        async_client.configure_opener(opener, data)
        self.assertTrue(isinstance(opener.request, Request))
        self.assertTrue(isinstance(opener.success_callback, FunctionType))
        self.assertTrue(isinstance(opener.fail_callback, FunctionType))
示例#6
0
    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)
示例#7
0
    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)
示例#8
0
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()
示例#9
0
 def test_get_opener(self):
     async_client = AsyncClient()
     opener = async_client.get_opener()
     self.assertEquals(opener.fp, None)
     self.assertNotEqual(opener, None)
示例#10
0
        client = kw['async_client']
        print(client)
    if kw.has_key('response'):
        print(kw['response'].headers)
        print(kw['response'].content)

    print("success")
    pass


def fail_call_back(**kw):
    print('failed')
    pass


client = AsyncClient(success_callback=success_call_back,
                     fail_callback=fail_call_back)
# client.get('https://github.com/lispython/human_curl',)

# client.start()
# client.get('https://blog.csdn.net/wjskeepmaking/article/details/64905745')
# client.method(**{'method':'get', 'url':'https://blog.csdn.net/wjskeepmaking/article/details/64905745'})
client.method(
    **{
        'method': 'post',
        'url':
        'https://sellercentral.amazon.com/fba/profitabilitycalculator/getafnfee',
        'data':
        '{"productInfoMapping":{"asin":"B06W9KM4WG","dimensionUnit": "inches","encryptedMarketplaceId": "","subCategory": "","weight": 1.25,"title": "BEXEL Super Alkaline AA Batteries 24 PACK","weightUnit": "pounds", "isWhiteGloveRequired": false,"imageUrl": "https://images-na.ssl-images-amazon.com/images/I/21qMWE048eL._SCLZZZZZZZ__SL120_.jpg","binding": "consumer_electronics","productGroup": "","height": 3.4646,"thumbStringUrl": "https://images-na.ssl-images-amazon.com/images/I/21qMWE048eL._SCLZZZZZZZ__SL80_.jpg","width": 2.3622,"length": 1.9685,"originalUrl": "", "link": "http://www.amazon.com/gp/product/B06W9KM4WG/ref=silver_xx_cont_revecalc", "dimensionUnitString": "inches", "gl": "gl_electronics", "isAsinLimits": true, "weightUnitString": "pounds"},"afnPriceStr":7.25,"mfnPriceStr":0,"mfnShippingPriceStr":0,"currency":"USD","marketPlaceId":"ATVPDKIKX0DER","hasFutureFee":false,"futureFeeDate":"2018-02-25 00:00:00","hasTaxPage":true} ',
        'headers': {
            'Content-Type':
            'application/json;charset=UTF-8',

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
with AsyncClient(success_callback=success_callback,
                 fail_callback=fail_callback) as async_client:

    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 when exit
示例#12
0
    if response.status_code != 200:
        bad_response_count += 1
        return
    success_count += 1


def fail_callback(request, opener, **kwargs):
    """Collect errors
    """
    global fail_count
    fail_count += 1


with AsyncClient(success_callback=success_callback,
                 fail_callback=fail_callback,
                 size=1000) as async_client:

    for x in range(10):
        test_data = {
            "name": choice(["orders", "posts", "comments"]),
            "value": choice([2, 1]),
            "timestamp": datetime.utcnow(),
            "filters": {
                "status": ["Completed", "Test"]
            }
        }
        async_client.post(url=client.get_url('incr'),
                          data=client.serialize(**test_data),
                          headers=list(client.headers.items()))
示例#13
0
    """
    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()
示例#14
0

def fail_call(response, **kwargs):
    print(response.content)


def get_ip():
    host_name = socket.gethostname()
    ip = socket.gethostbyname(host_name)
    print(ip)


#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=get_ip(), 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': get_ip(),}
#
# client.add_handler(**da)
# #client.configure_opener(opener, data=da)
# client.get(url)
# client.start()