Ejemplo n.º 1
0
def test_timeout_on_connect():
    with timeout_connect_server():
        http = HTTPClient(*listener,
            insecure=True, ssl=True, ssl_options={'ca_certs': CERT})

        def run(http, wait_time=100):
            response = http.get('/')
            gevent.sleep(wait_time)
            response.read()

        gevent.spawn(run, http)
        gevent.sleep(0)

        e = None
        try:
            http2 = HTTPClient(*listener,
                insecure=True,
                ssl=True,
                connection_timeout=0.1,
                ssl_options={'ca_certs': CERT})
            http2.get('/')
        except gevent.ssl.SSLError as error:
            e = error
        except gevent.socket.timeout as error:
            e = error
        except:
            raise

        assert e is not None, 'should have raised'
        if isinstance(e, gevent.ssl.SSLError):
            assert "operation timed out" in str(e)
Ejemplo n.º 2
0
def test_timeout_on_connect():
    with timeout_connect_server():
        http = HTTPClient(*listener, ssl=True, ssl_options={'ca_certs': CERT})

        def run(http, wait_time=100):
            response = http.get('/')
            gevent.sleep(wait_time)
            response.read()

        gevent.spawn(run, http)
        gevent.sleep(0)

        e = None
        try:
            http2 = HTTPClient(*listener,
                               ssl=True,
                               connection_timeout=0.1,
                               ssl_options={'ca_certs': CERT})
            http2.get('/')
        except gevent.ssl.SSLError as error:
            e = error
        except gevent.socket.timeout as error:
            e = error
        except:
            raise

        assert e is not None, 'should have raised'
        if isinstance(e, gevent.ssl.SSLError):
            assert str(e).endswith("handshake operation timed out")
Ejemplo n.º 3
0
def test_ssl_fail_invalid_certificate():
    certs = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), "onecert.pem")
    client = HTTPClient('www.google.fr', ssl_options={'ca_certs': certs})
    assert client.port == 443
    with pytest.raises(SSLError): #@UndefinedVariable
        client.get('/')
Ejemplo n.º 4
0
def test_ssl_fail_invalid_certificate():
    certs = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                         "onecert.pem")
    client = HTTPClient('www.google.fr', ssl_options={'ca_certs': certs})
    assert client.port == 443
    with pytest.raises(SSLError):
        client.get('/')
Ejemplo n.º 5
0
def test_ssl_fail_invalid_certificate():
    certs = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), "oncert.pem")
    client = HTTPClient('github.com', ssl_options={'ca_certs': certs})
    assert client.port == 443
    with pytest.raises(SSLError) as e_info:
        client.get('/')
    assert e_info.value.reason == 'CERTIFICATE_VERIFY_FAILED'
Ejemplo n.º 6
0
def test_ssl_fail_invalid_certificate():
    certs = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                         "oncert.pem")
    client = HTTPClient('github.com', ssl_options={'ca_certs': certs})
    assert client.port == 443
    with pytest.raises(SSLError) as e_info:
        client.get('/')
    assert e_info.value.reason == 'CERTIFICATE_VERIFY_FAILED'
Ejemplo n.º 7
0
def test_network_timeout():
    with server(network_timeout):
        http = HTTPClient(*listener, ssl=True, insecure=True,
            network_timeout=0.1, ssl_options={'ca_certs': CERT})
        if six.PY3:
            with pytest.raises(gevent.socket.timeout):
                response = http.get('/')
                assert response.status_code == 0, 'should have timed out.'
        else:
            with pytest.raises(gevent.ssl.SSLError):
                response = http.get('/')
                assert response.status_code == 0, 'should have timed out.'
Ejemplo n.º 8
0
def test_network_timeout():
    with server(network_timeout) as listener:
        http = HTTPClient(*listener,
                          ssl=True,
                          insecure=True,
                          network_timeout=0.1,
                          ssl_options={'ca_certs': CERT})
        if six.PY3:
            with pytest.raises(gevent.socket.timeout):
                response = http.get('/')
                assert response.status_code == 0, 'should have timed out.'
        else:
            with pytest.raises(gevent.ssl.SSLError):
                response = http.get('/')
                assert response.status_code == 0, 'should have timed out.'
Ejemplo n.º 9
0
def test_client_simple():
    client = HTTPClient('www.google.fr')
    assert client.port == 80
    response = client.get('/')
    assert response.status_code == 200
    body = response.read()
    assert len(body)
Ejemplo n.º 10
0
def test_client_simple():
    client = HTTPClient("www.google.fr")
    assert client.port == 80
    response = client.get("/")
    assert response.status_code == 200
    body = response.read()
    assert len(body)
Ejemplo n.º 11
0
def test_client_ssl():
    client = HTTPClient("www.google.fr", ssl=True)
    assert client.port == 443
    response = client.get("/")
    assert response.status_code == 200
    body = response.read()
    assert len(body)
Ejemplo n.º 12
0
def test_client_simple():
    client = HTTPClient('httpbin.org')
    assert client.port == 80
    response = client.get('/')
    assert response.status_code == 200
    body = response.read()
    assert len(body)
Ejemplo n.º 13
0
def test_client_ssl():
    client = HTTPClient('www.google.fr', ssl=True)
    assert client.port == 443
    response = client.get('/')
    assert response.status_code == 200
    body = response.read()
    assert len(body)
Ejemplo n.º 14
0
def test_client_simple():
    client = HTTPClient('httpbin.org')
    assert client.port == 80
    response = client.get('/')
    assert response.status_code == 200
    body = response.read()
    assert len(body)
Ejemplo n.º 15
0
def test_network_timeout():
    with server(network_timeout):
        http = HTTPClient(*listener, ssl=True,
            network_timeout=0.1, ssl_options={'ca_certs': CERT})
        with pytest.raises(gevent.ssl.SSLError): #@UndefinedVariable
            response = http.get('/')
            assert response.status_code == 0, 'should have timed out.'
Ejemplo n.º 16
0
def test_client_ssl():
    client = HTTPClient('github.com', ssl=True)
    assert client.port == 443
    response = client.get('/')
    assert response.status_code == 200
    body = response.read()
    assert len(body)
Ejemplo n.º 17
0
def test_internal_server_error():
    with server(internal_server_error):
        client = HTTPClient(*listener)
        response = client.get('/')
        assert not response.should_keep_alive()
        assert response.should_close()
        body = response.read()
        assert len(body) == response.content_length
Ejemplo n.º 18
0
def test_internal_server_error():
    with server(internal_server_error):
        client = HTTPClient(*listener)
        response = client.get("/")
        assert not response.should_keep_alive()
        assert response.should_close()
        body = response.read()
        assert len(body) == response.content_length
Ejemplo n.º 19
0
def verify_login(token):
    sig = md5(APP_KEY + token).hexdigest()
    url = '%stokenKey=%s&sign=%s' % (KUAIYONG_URL, token, sig)
    logger.debug('kuaiyong url:%s', url)
    url = URL(url)
    http = HTTPClient(url.host, port=url.port)
    response = eval(http.get(url.request_uri).read())
    http.close()
    return response
Ejemplo n.º 20
0
def test_network_timeout():
    with server(network_timeout):
        http = HTTPClient(*listener,
                          ssl=True,
                          network_timeout=0.1,
                          ssl_options={'ca_certs': CERT})
        with pytest.raises(gevent.ssl.SSLError):  #@UndefinedVariable
            response = http.get('/')
            assert response.status_code == 0, 'should have timed out.'
Ejemplo n.º 21
0
def verify_login(token):
    sig = md5(APP_KEY + token).hexdigest()
    url = '%stokenKey=%s&sign=%s' % (KUAIYONG_URL, token, sig)
    logger.debug('kuaiyong url:%s', url)
    url = URL(url)
    http = HTTPClient(url.host, port=url.port)
    response = eval(http.get(url.request_uri).read())
    http.close()
    return response
Ejemplo n.º 22
0
def test_simple_ssl():
    with server(simple_ssl_response) as listener:
        http = HTTPClient(*listener,
                          insecure=True,
                          ssl=True,
                          ssl_options={'ca_certs': CERT})
        response = http.get('/')
        assert response.status_code == 200
        response.read()
Ejemplo n.º 23
0
 def __call__(self, *args, **kwargs):
     assert self.runtime_config
     url = self.url_object
     http = HTTPClient(url.host, headers=self.runtime_config["agent"].get_as_dict())
     response = http.get(url.path)
     code = response.status_code
     log.debug("%s %s", self.url, code)
     body = response.read()
     http.close()
Ejemplo n.º 24
0
def test_timeout_on_connect():
    with timeout_connect_server():
        http = HTTPClient(*listener, ssl=True, ssl_options={'ca_certs': CERT})

        def run(http, wait_time=100):
            response = http.get('/')
            gevent.sleep(wait_time)
            response.read()

        gevent.spawn(run, http)
        gevent.sleep(0)

        with pytest.raises(gevent.socket.timeout):
            http2 = HTTPClient(*listener,
                ssl=True,
                connection_timeout=0.1,
                ssl_options={'ca_certs': CERT})
            http2.get('/')
Ejemplo n.º 25
0
def test_https():
    href = u'https://192.168.1.136:8080/stream/webrtc'
    url = URL(href)

    cli = HTTPClient(url.host, port=url.port, ssl=True, ssl_options={'ca_certs': 'ssl_certificate.crt'},
                     connection_timeout=5.0, network_timeout=10.0)
    response = cli.get(url.request_uri)
    s = response.read()
    cli.close()
    print(s)
Ejemplo n.º 26
0
def verify_login(session, appid):
    """
    登陆校验
    """
    url = "%ssession=%s&appid=%s" % (TBT_URL, session, appid)

    url = URL(url)
    http = HTTPClient(url.host, port=url.port)
    response = eval(http.get(url.request_uri).read())
    http.close()
    return response
Ejemplo n.º 27
0
def verify_login(session, appid):
    """
    登陆校验
    """
    url = "%ssession=%s&appid=%s" % (TBT_URL, session, appid)

    url = URL(url)
    http = HTTPClient(url.host, port=url.port)
    response = eval(http.get(url.request_uri).read())
    http.close()
    return response
Ejemplo n.º 28
0
def __login(passport):
    """login """
    logger.info('player login passport:%s' % passport)
    domain = '%s:%s' % (SERVER_MA_URL, SERVERS_MA_WEBPORT)
    url = '%s/verify?passport=%s' % (domain, passport)
    url = URL(url)
    http = HTTPClient(url.host, port=url.port)
    response = eval(http.get(url.request_uri).read())
    http.close()
    if response.get('result') is True:
        return str({'result': True, 'account_id': '\'%s\'' % passport})
    return str({'result': False})
def test_close_during_chuncked_readline():
    with server(close_during_chuncked_readline):
        client = HTTPClient(*listener)
        response = client.get('/')
        assert response['transfer-encoding'] == 'chunked'
        chunks = []
        with pytest.raises(HTTPException): #@UndefinedVariable
            data = 'enter_loop'
            while data:
                data = response.readline()
                chunks.append(data)
        assert len(chunks) == 3
Ejemplo n.º 30
0
def __login(passport):
    """login """
    logger.info('player login passport:%s' % passport)
    domain = '%s:%s' % (SERVER_MA_URL, SERVERS_MA_WEBPORT)
    url = '%s/verify?passport=%s' % (domain, passport)
    url = URL(url)
    http = HTTPClient(url.host, port=url.port)
    response = eval(http.get(url.request_uri).read())
    http.close()
    if response.get('result') is True:
        return str({'result': True, 'account_id': '\'%s\'' % passport})
    return str({'result': False})
def test_timeout_during_chuncked_readline():
    with server(timeout_during_chuncked_readline):
        client = HTTPClient(*listener, network_timeout=0.1)
        response = client.get('/')
        assert response['transfer-encoding'] == 'chunked'
        chunks = []
        with pytest.raises(gevent.socket.timeout): #@UndefinedVariable
            data = 'enter_loop'
            while data:
                data = response.readline()
                chunks.append(data)
        assert len(chunks) == 3
Ejemplo n.º 32
0
def test_close_during_chuncked_readline():
    with server(close_during_chuncked_readline):
        client = HTTPClient(*listener)
        response = client.get('/')
        assert response['transfer-encoding'] == 'chunked'
        chunks = []
        with pytest.raises(HTTPException):  #@UndefinedVariable
            data = 'enter_loop'
            while data:
                data = response.readline()
                chunks.append(data)
        assert len(chunks) == 3
Ejemplo n.º 33
0
def test_timeout_during_chuncked_readline():
    with server(timeout_during_chuncked_readline):
        client = HTTPClient(*listener, network_timeout=0.1)
        response = client.get('/')
        assert response['transfer-encoding'] == 'chunked'
        chunks = []
        with pytest.raises(gevent.socket.timeout):  #@UndefinedVariable
            data = 'enter_loop'
            while data:
                data = response.readline()
                chunks.append(data)
        assert len(chunks) == 3
Ejemplo n.º 34
0
 def beehive_ping(self, subsystem=None, vassal=None):
     """Ping beehive instance
     
     :param server: host name
     :param port: server port [default=6379]
     """
     path_inventory = u'%s/inventories/%s' % (self.ansible_path, 
                                              self.environment)
     path_lib = u'%s/library/beehive/' % (self.ansible_path)
     runner = Runner(inventory=path_inventory, verbosity=self.verbosity, 
                     module=path_lib)
     hosts, vars = runner.get_inventory_with_vars(u'beehive')
     vars = runner.variable_manager.get_vars(runner.loader, host=hosts[0])
     instances = vars.get(u'instance')
     vassals = []
     if subsystem is not None and vassal is not None:
         vassals.append([subsystem, vassal])
     else:
         for instance in instances:
             vassals.append(instance.split(u'-'))
     
     resp = []
     for vassal in vassals:
         port = instances.get(u'%s-%s' % tuple(vassal)).get(u'port')
 
         for host in hosts:
             url = URL(u'http://%s:%s/v1.0/server/ping/' % (host, port))
             http = HTTPClient(url.host, port=url.port)
             try:
                 # issue a get request
                 response = http.get(url.request_uri)
                 # read status_code
                 response.status_code
                 # read response body
                 res = json.loads(response.read())
                 # close connections
                 http.close()
                 if response.status_code == 200:
                     resp.append({u'subsystem':vassal[0], u'instance':vassal[1], 
                                  u'host':host, u'port':port, u'ping':True, 
                                  u'status':u'UP'})
                 else:
                     resp.append({u'subsystem':vassal[0], u'instance':vassal[1], 
                                  u'host':host, u'port':port, u'ping':False,
                                  u'status':u'UP'})
             except gevent.socket.error as ex:
                 resp.append({u'subsystem':vassal[0], u'instance':vassal[1], 
                              u'host':host, u'port':port, u'ping':False,
                              u'status':u'DOWN'})
                 
         
     self.result(resp, headers=[u'subsystem', u'instance', u'host', u'port', 
                                u'ping', u'status'])
def test_timeout_during_chuncked_readline():
    with server(timeout_during_chuncked_readline):
        client = HTTPClient(*listener, network_timeout=0.1)
        response = client.get("/")
        assert response["transfer-encoding"] == "chunked"
        chunks = []
        with pytest.raises(gevent.socket.timeout):
            data = "enter_loop"
            while data:
                data = response.readline()
                chunks.append(data)
        assert len(chunks) == 3
def test_close_during_chuncked_readline():
    with server(close_during_chuncked_readline):
        client = HTTPClient(*listener)
        response = client.get("/")
        assert response["transfer-encoding"] == "chunked"
        chunks = []
        with pytest.raises(HTTPException):
            data = "enter_loop"
            while data:
                data = response.readline()
                chunks.append(data)
        assert len(chunks) == 3
Ejemplo n.º 37
0
def test_readline_multibyte_sep():
    with server(readline_multibyte_sep):
        client = HTTPClient(*listener, block_size=1)
        response = client.get('/')
        lines = []
        while True:
            line = response.readline("\r\n")
            if not line:
                break
            data = json.loads(line[:-1])
            lines.append(data)
        assert len(lines) == 100
        assert map(lambda x: x['index'], lines) == range(0, 100)
Ejemplo n.º 38
0
def test_readline_multibyte_sep():
    with server(readline_multibyte_sep):
        client = HTTPClient(*listener, block_size=1)
        response = client.get("/")
        lines = []
        while True:
            line = response.readline("\r\n")
            if not line:
                break
            data = json.loads(line[:-1])
            lines.append(data)
        assert len(lines) == 100
        assert map(lambda x: x["index"], lines) == range(0, 100)
Ejemplo n.º 39
0
def test_readline_multibyte_sep():
    with server(readline_multibyte_sep):
        client = HTTPClient(*listener, block_size=1)
        response = client.get('/')
        lines = []
        while True:
            line = response.readline(b"\r\n")
            if not line:
                break
            data = json.loads(line[:-1].decode())
            lines.append(data)
        assert len(lines) == 100
        assert [x['index'] for x in lines] == [x for x in range(0, 100)]
Ejemplo n.º 40
0
def test_readline_multibyte_sep():
    with server(readline_multibyte_sep):
        client = HTTPClient(*listener, block_size=1)
        response = client.get('/')
        lines = []
        while True:
            line = response.readline(b"\r\n")
            if not line:
                break
            data = json.loads(line[:-1].decode())
            lines.append(data)
        assert len(lines) == 100
        assert [x['index'] for x in lines] == [x for x in range(0, 100)]
Ejemplo n.º 41
0
def test_https():
    href = u'https://192.168.1.136:8080/stream/webrtc'
    url = URL(href)

    cli = HTTPClient(url.host,
                     port=url.port,
                     ssl=True,
                     ssl_options={'ca_certs': 'ssl_certificate.crt'},
                     connection_timeout=5.0,
                     network_timeout=10.0)
    response = cli.get(url.request_uri)
    s = response.read()
    cli.close()
    print(s)
Ejemplo n.º 42
0
def test_readline_multibyte_splitsep():
    with server(readline_multibyte_splitsep):
        client = HTTPClient(*listener, block_size=1)
        response = client.get('/')
        lines = []
        last_index = 0
        while True:
            line = response.readline("\r\n")
            if not line:
                break
            data = json.loads(line[:-2])
            assert data['a'] == last_index + 1
            last_index = data['a']
        len(lines) == 3
Ejemplo n.º 43
0
def test_readline_multibyte_splitsep():
    with server(readline_multibyte_splitsep):
        client = HTTPClient(*listener, block_size=1)
        response = client.get("/")
        lines = []
        last_index = 0
        while True:
            line = response.readline("\r\n")
            if not line:
                break
            data = json.loads(line[:-2])
            assert data["a"] == last_index + 1
            last_index = data["a"]
        len(lines) == 3
class UserTsak(TaskSet):
    def on_start(self):
        '''初始化数据'''
        url = URL(host)
        # 若为https请求,ssl设置为True
        self.http = HTTPClient(url.host,url.port,ssl=False,connection_timeout=20,network_timeout=20)

    @task
    def test(self):
        try:
            start_time = time.time()
            # get 请求
            res = self.http.get("/sz/api2/test")
            # post 请求示例
            # body = json.dumps({"username":"******","password":"******"})
            # res = self.http.post("/sz/api2/login",body = body)
            data = res.read()
            end_time = time.time()
            response_time =int((end_time - start_time)*1000)
            response_length = len(data)
            assert json.loads(data)['Error'] == 0

            if res.status_code == 200:
                # 统计正常的请求
                events.request_success.fire(request_type="GET", name="test_success", response_time = response_time, response_length=response_length)

        # 统计断言失败的请求
        except AssertionError:
            end_time = time.time()
            response_time =int((end_time - start_time)*1000)
            events.request_failure.fire(request_type="GET", name="test_failure", response_time=response_time,response_length=0,
                                        exception="断言错误。status_code:{}。接口返回:{}。".format(res.status_code,json.loads(data)))

        # 统计超时异常
        except socket.timeout:
            events.locust_error.fire(locust_instance=UserTsak, exception='Timeout', tb =sys.exc_info()[2])

        # 统计其他异常
        except Exception as e:
            events.locust_error.fire(locust_instance=UserTsak, exception='Error:{}。\nstatus_code:{}。\n接口返回:{}。'.format(e,res.status_code,data), tb=sys.exc_info()[2])

    def on_stop(self):
        '''运行结束,关闭http/https连接'''
        self.http.close()
Ejemplo n.º 45
0
 def test_beehive(self, server, port):
     """Test redis instance
     
     :param server: host name
     :param port: server port [default=6379]
     """
     url = URL(u'http://%s:%s/v1.0/server/ping/' % (server, port))
     http = HTTPClient(url.host, port=url.port)
     # issue a get request
     response = http.get(url.request_uri)
     # read status_code
     response.status_code
     # read response body
     res = json.loads(response.read())
     # close connections
     http.close()
     if res[u'status'] == u'ok':
         resp = True
     else:
         resp = False
     self.logger.info(u'Ping beehive %s : %s' % (url.request_uri, resp))
     self.json = u'Ping beehive %s : %s' % (url.request_uri, resp)
Ejemplo n.º 46
0
def get_cdkey_gift_1123(data, player):
    request = cdkey_pb2.CdkeyRequest()
    request.ParseFromString(data)

    response = cdkey_pb2.CdkeyResqonse()
    url = '%s/verify?area_id=%s&uid=%s&code=%s&token=%s' % \
          (CDKEY_URL, SERVER_NO, player.base_info.id,
           request.cdkey, SERVER_TOKEN)
    logger.debug('cdkey url:%s', url)

    url = URL(url)
    http = HTTPClient(url.host, port=url.port)
    url_response = eval(http.get(url.request_uri).read())
    http.close()

    logger.debug('cdkey url result:%s', url_response)

    # gain_data = tomorrow_gift['reward']
    # return_data = gain(player, gain_data, const.TOMORROW_GIFT)
    # get_return(player, return_data, response.gain)
    response.res.result = True
    response.res.result_no = url_response.get('success')
    response.res.message = str(url_response.get('message'))
    return response.SerializeToString()
Ejemplo n.º 47
0
class CartoDBAPIKey(CartoDBBase):
    """
    this class provides you access to auth CartoDB API using your API.
    You can find your API key in:
        https://USERNAME.cartodb.com/your_apps/api_key.
    this method is easier than use the oauth authentification but if
    less secure, it is recommended to use only using the https endpoint
    """

    def __init__(
            self, api_key, cartodb_domain, host='cartodb.com',
            protocol='https', api_version='v2', proxy_info=None, *args, **kwargs):
        CartoDBBase.__init__(self, cartodb_domain,
            host, protocol, api_version)
        self.api_key = api_key
        self.client = HTTPClient(
            '.'.join([cartodb_domain, host]),
            connection_timeout=10.0,
            network_timeout=10.0,
            ssl=protocol == 'https',
            **kwargs)
        if protocol != 'https':
            warnings.warn("you are using API key auth method with http")

    def req(self, url, http_method="GET", http_headers={}, body=None):
        if http_method.upper() == "POST":
            body = body or {}
            body.setdefault('api_key', self.api_key)
            headers = {'Content-type': 'application/x-www-form-urlencoded'}
            headers.update(http_headers)
            resp = self.client.post(
                url.request_uri, body=urlencode(body), headers=headers)
        else:
            url['api_key'] = self.api_key
            resp = self.client.get(url.request_uri, headers=http_headers)
        return resp
Ejemplo n.º 48
0
class CartoDBAPIKey(CartoDBBase):
    """
    this class provides you access to auth CartoDB API using your API.
    You can find your API key in:
        https://USERNAME.cartodb.com/your_apps/api_key.
    this method is easier than use the oauth authentification but if
    less secure, it is recommended to use only using the https endpoint
    """

    def __init__(
            self, api_key, cartodb_domain, host='cartodb.com',
            protocol='https', api_version='v2', proxy_info=None,
            *args, **kwargs):
        CartoDBBase.__init__(self, cartodb_domain, host, protocol, api_version)
        self.api_key = api_key
        self.client = HTTPClient(
            '.'.join([cartodb_domain, host]),
            connection_timeout=10.0,
            network_timeout=10.0,
            ssl=protocol == 'https',
            **kwargs)
        if protocol != 'https':
            warnings.warn("you are using API key auth method with http")

    def req(self, url, http_method="GET", http_headers={}, body=None):
        if http_method.upper() == "POST":
            body = body or {}
            body.setdefault('api_key', self.api_key)
            headers = {'Content-type': 'application/x-www-form-urlencoded'}
            headers.update(http_headers)
            resp = self.client.post(
                url.request_uri, body=urlencode(body), headers=headers)
        else:
            url['api_key'] = self.api_key
            resp = self.client.get(url.request_uri, headers=http_headers)
        return resp
Ejemplo n.º 49
0
class AsyncHTTP:

    # wait_io: qsize, in_io: qsize
    def __init__(self, host_full, timeout = 10, conn_pool_size = 1, qsize = 10000, headers = {}):
        vec = host_full.split(':')
        host = vec[0]
        port = int(vec[1])
        self.impl = HTTPClient(host, port = port,
            concurrency = conn_pool_size, network_timeout = timeout,
            connection_timeout = timeout)
        self.headers = headers
        self.qsize = qsize
        self.queue = Queue(self.qsize)
        self.tasks = None
        self.is_stop = False
        self.in_io = 0
        self.timeout = timeout
        self.thread = threading.Thread(target = self._loop)

    def start(self):
        self.thread.start()

    def stats(self):
        d = self.impl._connection_pool._semaphore.counter
        return {'wait_io': self.queue.qsize(), 'in_io': self.in_io, 'd': d}

    def stop(self):
        self.is_stop = True
        self.thread.join()

    def get(self, url_s, cb):
        self.queue.put((url_s, cb))

    def _loop(self):
        self.tasks = pool.Pool(self.qsize, MyGreenlet)
        while not self.is_stop:
            self._loop_once()

    def _loop_once(self):
        while len(self.tasks) < self.qsize and not self.queue.empty():
            url, cb = self.queue.get()
            self.tasks.spawn(lambda : self._do_get(url, cb))
        if len(self.tasks) > 0:
            self.in_io = self._run_io()
        else:
            time.sleep(0.01)

    def _run_io(self, timeout = 0.01):
        self.tasks.join(timeout, True)
        self._check_timeout()
        return len(self.tasks)

    def _do_get(self, url, cb):
        try:
            resp = self.impl.get(url, headers = self.headers) 
            cb(resp)
        except socket.timeout:
            cb(None)
        except socket.error:
            cb(None)
        except Exception, e:
            logger.warn('http exception: %r', e)
            cb(None)
Ejemplo n.º 50
0
def test_close_during_content():
    with server(close_during_content):
        client = HTTPClient(*listener, block_size=1)
        response = client.get('/')
        with pytest.raises(HTTPException):  #@UndefinedVariable
            response.read()
Ejemplo n.º 51
0
def test_close_after_recv():
    with server(close_after_recv):
        client = HTTPClient(*listener)
        with pytest.raises(HTTPException):
            client.get('/')
Ejemplo n.º 52
0
def test_exception():
    with server(wrong_response_status_line):
        connection = HTTPClient(*listener)
        with pytest.raises(HTTPException):
            connection.get('/')
Ejemplo n.º 53
0
def test_verify_hostname():
    with server(simple_ssl_response) as listener:
        http = HTTPClient(*listener, ssl=True, ssl_options={'ca_certs': CERT})
        with pytest.raises(CertificateError):
            http.get('/')
Ejemplo n.º 54
0
def test_content_too_small():
    with server(content_too_small):
        client = HTTPClient(*listener, network_timeout=0.2)
        with pytest.raises(gevent.socket.timeout):  #@UndefinedVariable
            response = client.get('/')
            response.read()
def test_content_too_small():
    with server(content_too_small):
        client = HTTPClient(*listener, network_timeout=0.2)
        with pytest.raises(gevent.socket.timeout): #@UndefinedVariable
            response = client.get('/')
            response.read()
def test_close_during_content():
    with server(close_during_content):
        client = HTTPClient(*listener, block_size=1)
        response = client.get('/')
        with pytest.raises(HTTPException): #@UndefinedVariable
            response.read()
def test_close_after_recv():
    with server(close_after_recv):
        client = HTTPClient(*listener)
        with pytest.raises(HTTPException):
            client.get('/')
def test_exception():
    with server(wrong_response_status_line):
        connection = HTTPClient(*listener)
        with pytest.raises(HTTPException):
            connection.get('/')
Ejemplo n.º 59
0
# -*- coding: utf-8 -*-
from geventhttpclient import HTTPClient
from geventhttpclient import URL

url = URL("http://gevent.org/")

print url.request_uri
print url.path
http = HTTPClient(url.host)

response = http.get(url.path)

print response.status_code

print response.get('url')

print response.headers
#print response.read()

http.close()