def __init__(self, url, user=None, password=None, namespaces={}, ssl_opts=None): if user and password: # self.__password_mgr=http_client_lib.HTTPPasswordMgrWithDefaultRealm() # self.__password_mgr.add_password(None, url, user, password) # handler = http_client_lib.HTTPBasicAuthHandler(self.__password_mgr) # opener = http_client_lib.build_opener(handler) # http_client_lib.install_opener(opener) #pycurl.global_init(pycurl.GLOBAL_SSL) pass #if namespaces: self.__namespaces = namespaces if ssl_opts: self.__ssl_options.update(ssl_opts) self.__url = url self.__username = user self.__password = password try: self._http = HTTPClient(*self.__url, ssl = True, connection_timeout = None, network_timeout = None, ssl_options = self.__ssl_options, insecure = True, concurrency = concurrency) except TypeError: self._http = HTTPClient(*self.__url, ssl = True, connection_timeout = None, network_timeout = None, ssl_options = self.__ssl_options, concurrency = concurrency)
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")
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)
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)
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('/')
def _get_sni_sent_from_client(**additional_client_args): with sni_checker_server() as ctx: server_sock, server_greenlet = ctx server_addr, server_port = server_sock.getsockname()[:2] mock_addrinfo = (gevent.socket.AF_INET, gevent.socket.SOCK_STREAM, gevent.socket.IPPROTO_TCP, 'localhost', ('127.0.0.1', server_port)) with mock.patch('gevent.socket.getaddrinfo', mock.Mock(return_value=[mock_addrinfo])): server_host = 'some_foo' http = HTTPClient( server_host, server_port, insecure=True, ssl=True, connection_timeout=.1, ssl_context_factory=gevent.ssl.create_default_context, **additional_client_args) def run(http): try: http.get('/') except socket_error: pass # handshake will not be completed client_greenlet = gevent.spawn(run, http) joinall([client_greenlet, server_greenlet]) return server_host, server_port, server_greenlet.value
def _get_sni_sent_from_client(**additional_client_args): with sni_checker_server() as ctx: server_sock, server_greenlet = ctx server_addr, server_port = server_sock.getsockname()[:2] server_host = socket.gethostbyaddr(server_addr)[0] http = HTTPClient( server_host, server_port, insecure=True, ssl=True, connection_timeout=.1, ssl_context_factory=SSLConnectionPool.ssl_context_factory, **additional_client_args, ) def run(http): try: http.get('/') except socket.timeout: pass # handshake will not be completed client_greenlet = gevent.spawn(run, http) joinall([client_greenlet, server_greenlet]) return server_host, server_port, server_greenlet.value
def __init__(self, appType: Optional[str] = None, secondary: bool = False): if appType == 'IOS': self.appName = ApplicationType.IOS self.secondary = True elif appType == 'IOSIPAD': self.appName = ApplicationType.IOSIPAD self.secondary = False elif appType == 'ANDROIDLITE': self.appName = ApplicationType.ANDROIDLITE self.secondary = True elif appType == 'DESKTOPWIN': self.appName = ApplicationType.DESKTOPWIN self.secondary = False elif appType == 'DESKTOPMAC': self.appName = ApplicationType.DESKTOPMAC self.secondary = False elif appType == 'CHROMEOS': self.appName = ApplicationType.CHROMEOS self.secondary = False self.server = Server(self.appName, self.secondary) self.callback = Callback(self.__defaultCallback) self.systemName = None self.__concurrency = 30 url = URL(self.server.LINE_HOST_DOMAIN) self.__client = HTTPClient(url.host, url.port, concurrency=self.__concurrency, ssl=True, connection_timeout=180.0, network_timeout=180.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}) with pytest.raises(gevent.ssl.SSLError): response = http.get('/') assert response.status_code == 0, 'should have timed out.'
def get_or_create_client(self): from web3 import __version__ as web3_version global _client_cache key = "{}:{}".format(self.host, self.port) try: # Get in-process client instance for this host client = _client_cache[key] logger.debug("Re-using HTTP client for RPC connection to %s", key) except KeyError: request_user_agent = 'Web3.py/{version}/{class_name}'.format( version=web3_version, class_name=type(self), ) client = HTTPClient( host=self.host, port=self.port, ssl=self.ssl, connection_timeout=self.connection_timeout, network_timeout=self.network_timeout, concurrency=self.concurrency, headers={ 'Content-Type': 'application/json', 'User-Agent': request_user_agent, }, ) _client_cache[key] = client logger.debug( "Created new keep-alive HTTP client for RPC connection to %s", key, ) return client
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)
def _get_client(host, port, **kwargs): ordered_kwargs = collections.OrderedDict(sorted(kwargs.items())) cache_key = '{0}:{1}:{2}'.format( host, port, ':'.join(("{0}={1}".format(str(key), str(value)) for key, value in ordered_kwargs.items()))) if cache_key not in _client_cache: _client_cache[cache_key] = HTTPClient(host, port, **kwargs) return _client_cache[cache_key]
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'
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
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()
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
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
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
def test_file_post(): body = tempfile.NamedTemporaryFile("a+b", delete=False) name = body.name try: body.write(b"123456789") body.close() with wsgiserver(check_upload(b"123456789", 9)): client = HTTPClient(*listener) with open(name, 'rb') as body: client.post('/', body) finally: os.remove(name)
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
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_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)]
def __init__(self, app_type: ApplicationType = ApplicationType.IOS, concurrency: int = 30, secondary: bool = False): self.header_builder = HeaderBuilder(app_type, secondary=secondary) self.__concurrency = concurrency url = URL(LEGY_HOST) self.__client = HTTPClient(url.host, url.port, concurrency=self.__concurrency, ssl=True, connection_timeout=DEFAULT_TIMEOUT, network_timeout=DEFAULT_TIMEOUT)
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)
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)
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
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 test_multi_queries_greenlet_safe(): client = HTTPClient('www.google.fr', concurrency=3) group = gevent.pool.Group() event = gevent.event.Event() def run(i): event.wait() response = client.get('/') return response, response.read() count = 0 gevent.spawn_later(0.2, event.set) for response, content in group.imap_unordered(run, xrange(5)): assert response.status_code == 200 assert len(content) count += 1 assert count == 5
def make_request(method, params=None): global nonce nonce += 1 # NOQA payload = { "id": nonce, "jsonrpc": "2.0", "method": method, "params": params or [], } payload_data = json.dumps(force_obj_to_text(payload, True)) if 'GETH_ASYNC_GEVENT' in os.environ: from geventhttpclient import HTTPClient client = HTTPClient( host='127.0.0.1', port=open_port, connection_timeout=10, network_timeout=10, headers={ 'Content-Type': 'application/json', }, ) with contextlib.closing(client): response = client.post('/', body=payload_data) response_body = response.read() result = json.loads(force_text(response_body)) else: import requests response = requests.post( endpoint, data=payload_data, headers={ 'Content-Type': 'application/json', }, ) result = response.json() if 'error' in result: raise AssertionError(result['error']) return result['result']