def _installer(self): channel = 'stable' self.logger.info('system info') session = requests_unixsocket.Session() response = session.get('{0}/v2/system-info'.format(SOCKET)) self.logger.info("system info response: {0}".format(response.text)) snap_response = json.loads(response.text) version_response = requests.get( 'http://apps.syncloud.org/releases/{0}/snapd.version'.format( channel)) return self.to_app('sam', 'Installer', channel, snap_response['result']['version'], version_response.text)
def wait_until_ready(url): "Wait for the given HTTP endpoint to return a status of 200." session = requests_unixsocket.Session() for _ in range(10): try: response = session.get(url, timeout=1) if response.status_code == 200: return except (requests.ConnectionError, requests.ReadTimeout): pass time.sleep(0.1) raise PostgrestTimedOut()
def get_docker_status(docker_socket): """ Get docker status from socket file """ session = requests_unixsocket.Session() container_list = [] socket = docker_socket.replace("/", "%2F") url = "http+unix://{}/containers/json?all=1".format(socket) request = session.get(url) assert request.status_code == 200 for container in json.loads(request.content): item = (container["Names"], container["Status"]) container_list.append(item) return container_list
def ins(c_id, stream): base = "http+unix://%2Fvar%2Frun%2Fdocker.sock" url = "/containers/%s/stats?stream=%s" % (c_id, stream) session = requests_unixsocket.Session() resp = session.get(base + url) #for res in resp.json(): #a='{}'.format(res["memory_stats"]) #print(resp.content) a = resp.json() b = a["memory_stats"]["usage"] c = a["memory_stats"]["max_usage"] print("At date & time : %s " % a["read"]) print("Memory usage : %s" % ((float(b) * 100) / float(c))) #b=a[cpu_stats][][] print("CPU_usage in naoseconds : %s" % a["cpu_stats"]["cpu_usage"]["total_usage"])
def __init__(self, container_id, stream="0"): self.container_id = container_id self.stream = stream self.base = "http+unix://%2Fvar%2Frun%2Fdocker.sock" self.url = "/containers/%s/stats?stream=%s" % (self.container_id, self.stream) self.session = requests_unixsocket.Session() try: self.resp = self.session.get(self.base + self.url) except Exception as ex: template = "An exception of type {0} occured. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) print message
def __init__(self, url='http://localhost:8082/', connect_timeout=None): assert not url.startswith('http+unix://') or HAS_UNIX_SOCKET, ( 'You need to install requests-unixsocket for Unix socket support.') self._url = url.rstrip('/') config = configuration.get_config() if connect_timeout is None: connect_timeout = config.getfloat('core', 'rpc-connect-timeout', 10.0) self._connect_timeout = connect_timeout if HAS_REQUESTS: self._fetcher = RequestsFetcher(requests.Session()) else: self._fetcher = URLLibFetcher()
def upgrade(self, app_id, channel, force): self.logger.info('snap upgrade') if app_id == 'sam': raise Exception('Installer upgrade is not yet supported') session = requests_unixsocket.Session() response = session.post('{0}/v2/snaps/{1}'.format(SOCKET, app_id), json={ 'action': 'refresh', 'channel': channel, 'ignore-validation': force }) self.logger.info("refresh response: {0}".format(response.text)) snapd_response = json.loads(response.text) if (snapd_response['status']) != 'Accepted': raise Exception(snapd_response['result']['message'])
def __init__(self, api_endpoint, cert=None, verify=True, timeout=None, project=None): self._api_endpoint = api_endpoint self._timeout = timeout self._project = project if self._api_endpoint.startswith("http+unix://"): self.session = requests_unixsocket.Session() else: self.session = requests.Session() self.session.cert = cert self.session.verify = verify
def unpauseIfPaused(container): try: session = requests_unixsocket.Session() resp = session.get("http+unix://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/" + container.id + "/json") if resp.status_code >= 200 and resp.status_code < 300: containerDetails = json.loads(resp.text) if containerDetails["State"]["Status"] == "paused": resp = session.post("http+unix://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/" + container.id + "/unpause") if resp.status_code >= 200 and resp.status_code < 300: logger.info("unpauseIfPaused: " + container.type + " [ " + container.podname + ", " + container.id + " ] successfully unpaused.") else: logger.error("unpauseIfPaused: Failed to unpause " + container.type + " [" + container.podname + "] with HTTP error: " + str(resp.status_code)) else: logger.error("unpauseIfPaused: Failed to fetch details (docker inspect) of " + container.type + " [" + container.podname + "] with HTTP error: " + str(resp.status_code)) except Exception, e: logger.critical("unpauseIfPaused: Failed to unpause " + container.type + " [" + container.podname + "] with Exception: ", exc_info=True)
def __init__(self, base_url, username=None, password=None, default_prefix='/redfish/v1/', sessionkey=None, capath=None, cafile=None, timeout=None, max_retry=None, proxies=None): """Initialization of the base class RestClientBase :param base_url: The URL of the remote system :type base_url: str :param username: The user name used for authentication :type username: str :param password: The password used for authentication :type password: str :param default_prefix: The default root point :type default_prefix: str :param sessionkey: session key for the current login of base_url :type sessionkey: str :param capath: Path to a directory containing CA certificates :type capath: str :param cafile: Path to a file of CA certs :type cafile: str :param timeout: Timeout in seconds for the initial connection :type timeout: int :param max_retry: Number of times a request will retry after a timeout :type max_retry: int :param proxies: Dictionary containing protocol to proxy URL mappings :type proxies: dict """ self.__base_url = base_url.rstrip('/') self.__username = username self.__password = password self.__session_key = sessionkey self.__authorization_key = None self.__session_location = None if self.__base_url.startswith('http+unix://'): self._session = requests_unixsocket.Session() else: self._session = requests.Session() self._timeout = timeout self._max_retry = max_retry if max_retry is not None else 10 self._proxies = proxies self.login_url = None self.default_prefix = default_prefix self.capath = capath self.cafile = cafile self.get_root_object()
def status(self): self.logger.info('snap changes') if self.snap_upgrade_status(): self.logger.info("snapd upgrade is in progress") return True session = requests_unixsocket.Session() response = session.get( '{0}/v2/changes?select=in-progress'.format(SOCKET)) self.logger.info("changes response: {0}".format(response.text)) snapd_response = json.loads(response.text) if (snapd_response['status']) != 'OK': raise Exception(snapd_response['result']['message']) return len(snapd_response['result']) > 0
def __init__(self, url: str = URL_DEFAULT): self.url = url # this "hook" will be executed after each request (see http://docs.python-requests.org/en/master/user/advanced/#event-hooks) def hook(response, **_): if not response.ok: raise Exception(response.json().get("error")) # some lxd operations are asynchronous, we have to wait for them to finish before continuing # see https://linuxcontainers.org/lxd/docs/master/rest-api/#background-operation if response.json().get("type") == "async": operation = self.session.get(self.url + response.json().get("operation") + "/wait").json().get("metadata") if operation.get("status_code") != 200: raise Exception(operation.get("err")) self.session = requests_unixsocket.Session() self.session.hooks["response"].append(hook)
def __init__(self, nodes, _async=False, timeout=10, **kwargs): self.api_version = kwargs.get('api_version', 'v1') self.json_decode = kwargs.get('json_decode', True) self.timeout = timeout self.set_nodes(nodes) log_level = kwargs.get('log_level', logging.INFO) logger.setLevel(log_level) self.session_unix = requests_unixsocket.Session() self.session = requests.Session() self._async = _async if _async: self.async_client = httpx.AsyncClient(proxies={}) else: self.async_client = None
def send_email(send_to, subject_line, template_name, **template_details): if settings.SEND_MAIL: # pragma: no cover session = requests_unixsocket.Session() if 'url' in template_details: template_details[ 'url'] = settings.FRONTEND_URL + template_details['url'] session.post(settings.MAIL_SOCK_URL, data={ "to": send_to, "subject": subject_line, "template": template_name, **template_details }) else: print( f"Sending email '{subject_line}' to {send_to} using template {template_name} with details {template_details}" )
def test_unix_domain_adapter_ok(): with UnixSocketServerThread() as usock_thread: session = requests_unixsocket.Session('http+unix://') urlencoded_usock = requests.compat.quote_plus(usock_thread.usock) url = 'http+unix://%s/path/to/page' % urlencoded_usock logger.debug('Calling session.get(%r) ...', url) r = session.get(url) logger.debug('Received response: %r with text: %r and headers: %r', r, r.text, r.headers) assert r.status_code == 200 assert r.headers['server'] == 'waitress' assert r.headers['X-Transport'] == 'unix domain socket' assert r.headers['X-Requested-Path'] == '/path/to/page' assert r.headers['X-Socket-Path'] == usock_thread.usock assert isinstance(r.connection, requests_unixsocket.UnixAdapter) assert r.url == url assert r.text == 'Hello world!'
def __init__(self, nodes, _async=False, **kwargs): self.api_version = kwargs.get('api_version', 'v1') self.max_retries = kwargs.get('max_retries', 2) self.json_decode = kwargs.get('json_decode', True) if kwargs.get('tcp_keepalive', True): socket_options = HTTPConnection.default_socket_options + \ [(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1), ] else: socket_options = HTTPConnection.default_socket_options timeout = urllib3.Timeout(connect=kwargs.get('connect_timeout', 10), read=kwargs.get('timeout', 10)) self.http = urllib3.poolmanager.PoolManager( num_pools=kwargs.get('num_pools', 50), maxsize=kwargs.get('maxsize', 10), block=kwargs.get('pool_block', False), retries=kwargs.get('http_retries', 1), timeout=timeout, socket_options=socket_options, headers={'Content-Type': 'application/json'}, cert_reqs='CERT_REQUIRED', ca_certs=certifi.where()) ''' urlopen(method, url, body=None, headers=None, retries=None, redirect=True, assert_same_host=True, timeout=<object object>, pool_timeout=None, release_conn=None, chunked=False, body_pos=None, **response_kw) ''' self.set_nodes(nodes) log_level = kwargs.get('log_level', logging.INFO) logger.setLevel(log_level) self.session_unix = requests_unixsocket.Session() self.session = requests.Session() self.timeout = 5 self._async = _async if _async: import httpx self.async_client = httpx.AsyncClient() else: self.async_client = None
def connect(self, server, use_socket=False): self.use_socket = use_socket self.conn = None self._cookies = None self._secret = None if use_socket: self.userName = pwd.getpwuid(os.getuid())[0] self.serverName = "local" self.path = get_socket_file() if not self.path: print('No socked found') return False self.url = self._get_url("/") self.conn = requests_unixsocket.Session() else: if '@' in server: self.userName = server.split('@')[0] self.serverName = server.split('@')[1] else: self.serverName = server self.userName = pwd.getpwuid(os.getuid())[0] self.path = None self.url = self._get_url("/") self.conn = requests.Session() try: r1 = self.conn.get(self._get_url("/ping"), cookies=self._cookies, verify=False) self._cookies = r1.cookies if r1.status_code == 200: self._secret = json.loads(r1.content.decode('utf-8'))['secret'] return self.login() else: self.disconnect() return False except Exception as exc: print(exc) if self.conn: self.conn.close() self.conn = None self._secret = None self._cookies = None self._token = None return False
def _get_session_and_address(): if not CONF.compressor_address: return None, None if CONF.compressor_address.startswith("/"): # unix socket if os.path.exists(CONF.compressor_address): mode = os.stat(CONF.compressor_address).st_mode if stat.S_ISSOCK(mode): return (requests_unixsocket.Session(), "http+unix://%s/" % parse.quote_plus(CONF.compressor_address)) else: raise exception.CoriolisException( "compressor_address is not a valid unix socket") else: raise exception.CoriolisException( "compressor_address is not a valid unix socket") return (requests.Session(), "http://%s/" % CONF.compressor_address)
def call_thumbor(request): if not os.path.exists(thumbor_socket): start_server() session = requests_unixsocket.Session() unix_path = 'http+unix://%2Ftmp%2Fthumbor' http_health = '/healthcheck' retries = 10 while (retries > 0): try: response = session.get(unix_path + http_health) if (response.status_code == 200): break except Exception as error: time.sleep(0.03) retries -= 1 continue if retries <= 0: logging.error('call_thumbor error: tornado server unavailable,\ proceeding with tornado server restart') restart_server() return response_formater(status_code='502') http_path = request['path'] if str(os.environ.get('REWRITE_ENABLED')).upper() == 'YES': http_path = lambda_rewrite.match_patterns(http_path) if config.ALLOW_UNSAFE_URL: http_path = '/unsafe' + http_path else: http_path = http_path response = session.get(unix_path + http_path) if response.status_code != 200: return response_formater(status_code=response.status_code) content_type = response.headers['content-type'] body = gen_body(content_type, response.content) if body is None: return response_formater(status_code='500', cache_control='no-cache,no-store') return response_formater(status_code='200', body=body, cache_control=response.headers['Cache-Control'], content_type=content_type, expires=response.headers['Expires'], etag=response.headers['Etag'], date=response.headers['Date'])
def __init__(self, api_endpoint, cert=None, verify=True, timeout=None, project=None): self._api_endpoint = api_endpoint self._timeout = timeout self._project = project if self._api_endpoint.startswith("http+unix://"): self.session = requests_unixsocket.Session() else: self.session = requests.Session() self.session.cert = cert self.session.verify = verify if isinstance(verify, str): self.session.mount(api_endpoint, LXDSSLAdapter())
def __init__(self, alivedir: str = default_data_dir+'/AliveDB', peers: list = [], gun_port = None, chat_listener: str = '') -> None: """ Instantiates an AliveDB instance. :alivedir: AliveDB working directory for databases etc. :peers: List of GunDB P2P endpoints :gun_port: GunDB P2P port to bind to """ self.process = None self.alivedir = alivedir self.peers = peers self.gun_port = gun_port self.chat_listener = chat_listener self.socket = alivedir + '/alivedb.sock' self.socketurl = 'http+unix://'+('%2F'.join(self.socket.split('/'))) self.session = requests_unixsocket.Session() if os.path.exists(self.socket): os.remove(self.socket)
def is_thumbor_down(): if not os.path.exists(thumbor_socket): start_server() session = requests_unixsocket.Session() http_health = '/healthcheck' retries = 10 while (retries > 0): try: response = session.get(unix_path + http_health) if (response.status_code == 200): break except Exception as error: time.sleep(0.03) retries -= 1 continue if retries <= 0: logging.error('call_thumbor error: tornado server unavailable,\ proceeding with tornado server restart') restart_server() return response_formater(status_code='502') return False, session
def find_in_store(self, app_id): self.logger.info('snap list') session = requests_unixsocket.Session() response = session.get('{0}/v2/find?name={1}'.format(SOCKET, app_id)) self.logger.info("find app: {0}, response: {1}".format( app_id, response.text)) snap_response = json.loads(response.text) found_apps = [ self.to_app(app['name'], app['summary'], app['channel'], None, app['version']) for app in snap_response['result'] ] if (len(found_apps) == 0): self.logger.warn("No app found") return None if (len(found_apps) > 1): self.logger.warn("More than one app found") return found_apps[0]
def __init__(self, host='127.0.0.1', port=5555, sockfile=None, url=None, build_requests=None, callback=None, supports_blocking=True): """ Initialise a new client """ self.session = requests.Session() if url: self.url = url elif sockfile: self.url = 'http+unix://{}/api/request'.format(sockfile.replace('/', '%2F')) elif voltron.config.view.api_url: self.url = voltron.config.view.api_url else: self.url = 'http://{}:{}/api/request'.format(host, port) self.url = self.url.replace('~', os.path.expanduser('~').replace('/', '%2f')) self.callback = callback self.build_requests = build_requests self.done = False self.server_version = None self.block = False self.supports_blocking = supports_blocking
def __init__(self): GObject.GObject.__init__(self) self._timeout_id = 0 self._snap = LivepatchSnap() self._session = requests_unixsocket.Session() # Init Properties self._availability = LivepatchAvailability.FALSE self._availability_message = None lp_file = Gio.File.new_for_path(path=self.LIVEPATCH_RUNNING_FILE) self._enabled = lp_file.query_exists() # Monitor connectivity status self._nm = Gio.NetworkMonitor.get_default() self._nm.connect('notify::connectivity', self._network_changed_cb) # Monitor status of canonical-livepatch self._lp_monitor = lp_file.monitor_file(Gio.FileMonitorFlags.NONE) self._lp_monitor.connect('changed', self._livepatch_enabled_changed_cb)
def request_start_logging(file_path, hec_url, hec_token, options={}): ''' send a request to the plugin to start logging :param file_path: the file path :type file_path: string :param hec_url: the file path :type hec_url: string :param hec_token: the file path :type hec_token: string ''' config = {} config["splunk-url"] = hec_url config["splunk-token"] = hec_token config["splunk-insecureskipverify"] = "true" config["splunk-format"] = "json" config["tag"] = "" config = {**config, **options} req_obj = { "File": file_path, "Info": { "ContainerID": "test", "Config": config, "LogPath": "/home/ec2-user/test.txt" } } headers = {"Content-Type": "application/json", "Host": "localhost"} session = requests_unixsocket.Session() res = session.post(SOCKET_START_URL, data=json.dumps(req_obj), headers=headers) if res.status_code != 200: raise Exception("Can't establish socket connection") logger.info(res.json())
def __init__(self, *args, **kwargs): super(DHCPResponder, self).__init__(*args, **kwargs) self.acks = {} self.switches = {} self.switches = { 81713664287054: { 'tenantID': 'tenant1', 'ipaddress': '30.1.1.1', 'netmask': '255.255.255.0', 'address': '0a:e4:1c:d1:3e:44', 'dns': '8.8.8.8', 'hosts': { '92:e5:97:98:e8:6a': { 'id':'iot-device_1', 'endpointID': '0cc175b9c0f1b6a831c399e269772661', 'hostname': 'sw4iot-master-dev', 'netID': 'tenant1-net', 'groupID': '', 'dns': '8.8.8.8', 'ipaddress': '192.168.1.2', } }, 'available_address': [ '192.168.1.10', '192.168.1.20' ] }} wsgi = kwargs['wsgi'] wsgi.register(DHCPController, {'dhcp_server': self}) if os.path.exists("/run/contiv/contiv-cni.sock"): self.session = requests_unixsocket.Session() self.logger.info("CONNECTED TO CONTIV!") else: self.logger.info("COULDN'T CONNECT WITH CONTIV!") exit(1)
def call_thumbor(request): if not os.path.exists(thumbor_socket): start_server() session = requests_unixsocket.Session() unix_path = 'http+unix://%2Ftmp%2Fthumbor' http_health = '/healthcheck' retries = 10 while (retries > 0): try: response = session.get(unix_path + http_health) if (response.status_code == 200): break except Exception as error: time.sleep(0.03) retries -= 1 continue if retries <= 0: logging.error('call_thumbor error: 502') return response_formater(status_code='502') if config.ALLOW_UNSAFE_URL: http_path = '/unsafe' + request['path'] else: http_path = request['path'] response = session.get(unix_path + http_path) if response.status_code != 200: return response_formater(status_code=response.status_code) content_type = response.headers['content-type'] body = gen_body(content_type, response.content) if body is None: return response_formater(status_code='500', cache_control='no-cache,no-store') return response_formater(status_code='200', body=body, cache_control=response.headers['Cache-Control'], content_type=content_type, expires=response.headers['Expires'], etag=response.headers['Etag'], date=response.headers['Date'])
def test_unix_domain_adapter_ok(): with UnixSocketServerThread() as usock_thread: session = requests_unixsocket.Session('http+unix://') urlencoded_usock = requests.compat.quote_plus(usock_thread.usock) url = 'http+unix://%s/path/to/page' % urlencoded_usock for method in [ 'get', 'post', 'head', 'patch', 'put', 'delete', 'options' ]: logger.debug('Calling session.%s(%r) ...', method, url) r = getattr(session, method)(url) logger.debug('Received response: %r with text: %r and headers: %r', r, r.text, r.headers) assert r.status_code == 200 assert r.headers['server'] == 'waitress' assert r.headers['X-Transport'] == 'unix domain socket' assert r.headers['X-Requested-Path'] == '/path/to/page' assert r.headers['X-Socket-Path'] == usock_thread.usock assert isinstance(r.connection, requests_unixsocket.UnixAdapter) assert r.url == url if method == 'head': assert r.text == '' else: assert r.text == 'Hello world!'
def render_jsx_to_string(request: HttpRequest, context: Any, props: Any) -> str: respond_with_json = should_respond_with_json(request) payload = {"context": context, "props": props} data = simplejson.dumps(payload) headers = {"Content-Type": "application/json"} if "debug" in request.GET: return f"<html><body><h1>Debug response</h1><pre>{escape(data)}</pre></body></html>" elif (respond_with_json or "raw" in request.GET or getattr(settings, "REACTIVATED_SERVER", False) is None): request._is_reactivated_response = True # type: ignore[attr-defined] return data renderer_port = wait_and_get_port() # Sometimes we are running tests and the CWD is outside BASE_DIR. For # example, the reactivated tests themselves. Instead of using BASE_DIR as # the prefix, we calculate the relative path to avoid the 100 character # UNIX socket limit. # But dots do not work for relative paths with sockets so we clear it. rel_path = os.path.relpath(settings.BASE_DIR) address = renderer_port if rel_path == "." else f"{rel_path}/{renderer_port}" session = requests_unixsocket.Session() socket = urllib.parse.quote_plus(address) response = session.post(f"http+unix://{socket}", headers=headers, data=data) if response.status_code == 200: return response.text # type: ignore[no-any-return] else: raise Exception(response.json()["stack"])