def execute(self, uri, namespace, action, timeout=2, **kwargs): """Executes a given action with optional arguments. The execution of an action of an UPnP/TR64 device needs more than just the name of an action. It needs the control URI which is called to place the action and also the namespace aka service type is needed. The namespace defines the scope or service type of the given action, the same action name can appear in different namespaces. The way how to obtain the needed information's is either through the documentation of the vendor of the device. Or through a discovery requests which return's the URL to the root device description XML. :param str uri: the control URI, for example ``/upnp/control/hosts`` :param str namespace: the namespace for the given action, for example ``urn:dslforum-org:service:Hosts:1`` :param str action: the name of the action to call, for example ``GetGenericHostEntry`` :param float timeout: the timeout to wait for the action to be executed :param kwargs: optional arguments for the given action, depends if the action needs parameter. The arguments are given as dict where the key is the parameter name and the value the value of the parameter. :type kwargs: dict[str, str] :return: returns the results of the action, if any. The results are structured as dict where the key is the name of the result argument and the value is the value of the result. :rtype: dict[str,str] :raises ValueError: if parameters are not set correctly :raises requests.exceptions.ConnectionError: when the action can not be placed on the device :raises requests.exceptions.ConnectTimeout: when download time out Example: :: device = DeviceTR64(...) device.execute("/upnp/control/hosts", "urn:dslforum-org:service:Hosts:1", "GetGenericHostEntry", {"NewIndex": 1}) {'NewActive': '0', 'NewIPAddress': '192.168.0.23', 'NewMACAddress': '9C:20:7B:E7:FF:5F', 'NewInterfaceType': 'Ethernet', 'NewHostName': 'Apple-TV', 'NewAddressSource': 'DHCP', 'NewLeaseTimeRemaining': '0'} .. seealso:: `Additional short explanation of the UPnP protocol <http://www.upnp-hacks.org/upnp.html>`_ :class:`~simpletr64.Discover`, :meth:`~simpletr64.DeviceTR64.loadDeviceDefinitions`, :meth:`~simpletr64.DeviceTR64.loadSCPD` """ if not uri: raise ValueError("No action URI has been defined.") if not namespace: raise ValueError("No namespace has been defined.") if not action: raise ValueError("No action has been defined.") # soap headers header = { 'Content-Type': 'text/xml; charset="UTF-8"', 'Soapaction': '"' + namespace + "#" + action + '"' } # build SOAP body body = '''<?xml version="1.0" encoding="UTF-8"?> <s:Envelope s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <s:Header/> <s:Body>\n''' body += " <u:" + action + ' xmlns="' + namespace + '">\n' arguments = {} for key in kwargs.keys(): body += " <" + key + ">" + str( kwargs[key]) + "</" + key + ">\n" arguments[key] = str(kwargs[key]) body += " </u:" + action + ">\n" body += '''</s:Body> </s:Envelope>''' # setup proxies proxies = {} if self.__httpsProxy: proxies = {"https": self.__httpsProxy} if self.__httpProxy: proxies = {"http": self.__httpProxy} # setup authentication auth = None if self.__password: auth = HTTPDigestAuth(self.__username, self.__password) # build the URL location = self.__protocol + "://" + self.__hostname + ":" + str( self.port) + uri # Post http request request = requests.post(location, data=body, headers=header, auth=auth, proxies=proxies, timeout=float(timeout)) if request.status_code != 200: errorStr = DeviceTR64._extractErrorString(request) raise ValueError('Could not execute "' + action + str(arguments) + '": ' + str(request.status_code) + ' - ' + request.reason + " -- " + errorStr) # parse XML return try: root = ET.fromstring(request.text.encode('utf-8')) except Exception as e: raise ValueError("Can not parse results for the action: " + str(e)) # iterate in the XML structure to get the action result actionNode = root[0][0] # we need to remove XML namespace for the action node namespaceLength = len(namespace) + 2 # add braces tag = actionNode.tag[namespaceLength:] if tag != (action + "Response"): raise ValueError( 'Soap result structure is wrong, expected action "' + action + 'Response" got "' + tag + '".') # pack all the received results results = {} for resultNode in actionNode: results[resultNode.tag] = resultNode.text return results
( ([SCHEMA_URI], { "checks": DEFAULT_CHECKS }), ([SCHEMA_URI, "--auth=test:test"], { "checks": DEFAULT_CHECKS, "api_options": { "auth": ("test", "test") } }), ( [SCHEMA_URI, "--auth=test:test", "--auth-type=digest"], { "checks": DEFAULT_CHECKS, "api_options": { "auth": HTTPDigestAuth("test", "test") } }, ), ( [SCHEMA_URI, "--auth=test:test", "--auth-type=DIGEST"], { "checks": DEFAULT_CHECKS, "api_options": { "auth": HTTPDigestAuth("test", "test") } }, ), ( [SCHEMA_URI, "--header=Authorization:Bearer 123"], {
def __get_request(self, path, **args): """ Sending GET request. :param path: path for get request api :param headers: headers parameter for adding custom headers :return: This method return get request response """ try: if self.session: req = self.req_session test_cookies = args.get('cookies', None) else: req = requests response = {} parameters = args.get('parameters', None) rheaders = args.get('headers', None) # Framing URL request. url_path = self.url + path self.log.info('************* GET request URL is {} ************' '*'.format(url_path)) # Checking authentication flog. self.headers = self.test_data["headers"] if rheaders: self.headers.update(rheaders) print(self.auth_type) if self.auth_type == 'HTTPDigestAuth': self.log.info('Authentication type is HTTPDigestAuth') res = req.get(url_path, params=parameters, headers=self.headers, auth=HTTPDigestAuth(self.username, self.password)) elif self.auth_type == 'HTTPBasicAuth': self.log.info('Authentication type is HTTPBasicAuth') res = req.get(url_path, params=parameters, headers=self.headers, auth=HTTPBasicAuth(self.username, self.password)) elif self.auth_type == 'Session': res = req.get(url_path, params=parameters, headers=self.headers, cookies=test_cookies) else: res = req.get(url_path, params=parameters, headers=self.headers) response['code'] = res.status_code response['data'] = res.text response['headers'] = res.headers self.log.info('Received response code is {}'.format( res.status_code)) self.log.info('Received response data is {}'.format(res.text)) self.log.debug('Received response headers is {}'.format( res.headers)) self.log.info('*************----END----*************') # Returning GET request status code, data and headers. # return res_status_code, rest_data, res_headers return response except Exception as e: self.log.exception( "GET request {} Failed with exception {}".format(url_path, e))
def fetch_http_release(self, eol, _list=False): """ Fetch a user specified RELEASE from FreeBSD's http server or a user supplied one. The user can also specify the user, password and root-directory containing the release tree that looks like so: - XX.X-RELEASE - XX.X-RELEASE - XX.X_RELEASE """ if self.hardened: if self.server == "ftp.freebsd.org": self.server = "http://installer.hardenedbsd.org" rdir = "releases/pub/HardenedBSD/releases/{0}/{0}".format( self.arch) if self.server == "ftp.freebsd.org": self.server = "https://download.freebsd.org" self.root_dir = "ftp/releases/{}".format(self.arch) if self.auth and "https" not in self.server: self.server = "https://" + self.server elif "http" not in self.server: self.server = "http://" + self.server logging.getLogger("requests").setLevel(logging.WARNING) if self.hardened: if self.auth == "basic": req = requests.get("{}/releases".format(self.server), auth=(self.user, self.password), verify=self.verify) elif self.auth == "digest": req = requests.get("{}/releases".format(self.server), auth=HTTPDigestAuth(self.user, self.password), verify=self.verify) else: req = requests.get("{}/releases".format(self.server)) releases = [] status = req.status_code == requests.codes.ok if not status: req.raise_for_status() if not self.release: for rel in req.content.split(): rel = rel.strip("href=").strip("/").split(">") if "_stable" in rel[0]: rel = rel[0].strip('"').strip("/").strip("/</a") rel = rel.replace("hardened_", "").replace( "_master-LAST", "").replace("_", "-").upper() if rel not in releases: releases.append(rel) releases = sort_release(releases, self.iocroot) for r in releases: self.lgr.info("[{}] {}".format(releases.index(r), r)) self.release = input( "\nWhich release do you want to fetch?" " (EXIT) ") self.release = self.__fetch_validate_release__(releases) else: if self.auth == "basic": req = requests.get("{}/{}".format(self.server, self.root_dir), auth=(self.user, self.password), verify=self.verify) elif self.auth == "digest": req = requests.get("{}/{}".format(self.server, self.root_dir), auth=HTTPDigestAuth(self.user, self.password), verify=self.verify) else: req = requests.get("{}/{}".format(self.server, self.root_dir)) releases = [] status = req.status_code == requests.codes.ok if not status: req.raise_for_status() if not self.release: for rel in req.content.split(): rel = rel.strip("href=").strip("/").split(">") if "-RELEASE" in rel[0]: rel = rel[0].strip('"').strip("/").strip("/</a") if rel not in releases: releases.append(rel) releases = sort_release(releases, self.iocroot) for r in releases: if r in eol: self.lgr.info( "[{}] {} (EOL)".format(releases.index(r), r)) else: self.lgr.info("[{}] {}".format(releases.index(r), r)) if _list: return self.release = input( "\nWhich release do you want to fetch?" " (EXIT) ") self.release = self.__fetch_validate_release__(releases) if self.hardened: self.root_dir = "{}/hardenedbsd-{}-LAST".format(rdir, self.release.lower()) self.lgr.info("Fetching: {}\n".format(self.release)) self.fetch_download(self.files) missing = self.__fetch_check__(self.files) if missing: self.fetch_download(missing, missing=True) self.__fetch_check__(missing, _missing=True) if not self.hardened: self.fetch_update()
def pruebaDigest(user, password, protectedUrl): response = requests.get(protectedUrl, auth=HTTPDigestAuth(user, password))
__author__ = 'phoehne' from requests.auth import HTTPDigestAuth from marklogic.models import Database, Connection, Host, HttpServer, ElementRange, ElementAttributeRange conn = Connection("192.168.57.141", HTTPDigestAuth("admin", "admin")) server_hostname = hosts = Host.list_hosts(conn)[0].host_name() db = Database("test-one", server_hostname) db.create(conn).load_file(conn, "example_doc.json", "/test/document.json", ["example", "collection"]) modules = Database("test-one-modules", server_hostname) modules.create(conn) db = Database.lookup("test-one", conn) db.add_index(ElementRange("order-id", u'int')) db.add_index( ElementAttributeRange("customer", "id", scalar_type=u'int', element_namespace="http://foo.bar.com")) db.save(conn) srvr = HttpServer("test-one-http", 8400) srvr.set_content_database(db.config[u'database-name']).set_modules_database( modules.config[u'database-name']) srvr.create(conn) db.load_file(conn, "example_doc.json", "/example/file.json", ["test"])
def VerifyAuth(hostname, id, key): r = requests.get("http://ultimaker.linz.local/api/v1/auth/verify", auth = HTTPDigestAuth(id, key)) print("Verify authentication: %d %s" % (r.status_code, r.text))
def send_get(url,headers = {'content-type': 'application/json'}): """POST请求""" r = requests.get(url, headers=headers, auth=HTTPDigestAuth('kakou', 'pingworker')) return r
def __init__( self, *, auth_type: str = None, headers: dict = None, method: str = "GET", name: str, off_cmd: str, off_data: dict = None, off_json: dict = None, on_cmd: str, on_data: dict = None, on_json: dict = None, password: str = None, port: int, state_cmd: str = None, state_data: dict = None, state_json: dict = None, state_method: str = "GET", state_response_off: str = None, state_response_on: str = None, user: str = None, ) -> None: """Initialize a RESTAPIPlugin instance. Args: auth_type: Either `basic` or `digest` if needed headers: Additional headers for both `on()` and `off()` method: HTTP method to be used for both `on()` and `off()` off_cmd: URL to be called when turning device off off_data: HTTP body to turn device off off_json: JSON body to turn device off on_cmd: URL to be called when turning device on on_data: HTTP body to turn device on on_json: JSON body to turn device on password: Password for `auth` state_cmd: URL to be called to determine device state state_data: Optional POST data to query device state state_json: Optional json data to query device state state_method: HTTP method to be used for `get_state()` state_response_off: If this string is in the response to state_cmd, the device is off. state_response_on: If this string is in the response to state_cmd, the device is on. user: Username for `auth` """ self.method = method self.state_method = state_method self.headers = headers self.auth: Union[HTTPBasicAuth, HTTPDigestAuth] = None self.on_cmd = on_cmd self.off_cmd = off_cmd self.state_cmd = state_cmd self.on_data = on_data self.off_data = off_data self.state_data = state_data self.on_json = on_json self.off_json = off_json self.state_json = state_json self.state_response_on = state_response_on self.state_response_off = state_response_off if auth_type: if auth_type.lower() == "basic": self.auth = HTTPBasicAuth(user, password) elif auth_type.lower() == "digest": self.auth = HTTPDigestAuth(user, password) super().__init__(name=name, port=port)
def do_run(config, endpoint_list): global activation_key activation_key = config.get("activation_key").strip() app_name = "REST API Modular Input" if len(activation_key) > 32: activation_hash = activation_key[:32] activation_ts = activation_key[32:][::-1] current_ts = time.time() m = hashlib.md5() m.update((app_name + activation_ts)) if not m.hexdigest().upper() == activation_hash.upper(): logging.error( "FATAL Trial Activation key for App '%s' failed. Please ensure that you copy/pasted the key correctly." % app_name) sys.exit(2) if ((current_ts - long(activation_ts)) > 604800): logging.error( "FATAL Trial Activation key for App '%s' has now expired. Please visit http://www.baboonbones.com/#activation to purchase a non expiring key." % app_name) sys.exit(2) else: m = hashlib.md5() m.update((app_name)) if not m.hexdigest().upper() == activation_key.upper(): logging.error( "FATAL Activation key for App '%s' failed. Please ensure that you copy/pasted the key correctly." % app_name) sys.exit(2) #setup some globals server_uri = config.get("server_uri") global SPLUNK_PORT global STANZA global SESSION_TOKEN global delimiter SPLUNK_PORT = server_uri[18:] STANZA = config.get("name") SESSION_TOKEN = config.get("session_key") # encrypted_username, encrypted_password = get_credentials(SESSION_TOKEN) #params http_method = config.get("http_method", "GET") request_payload = config.get("request_payload") #none | basic | digest | oauth1 | oauth2 auth_type = config.get("auth_type", "none") #Delimiter to use for any multi "key=value" field inputs delimiter = config.get("delimiter", ",") #for basic and digest auth_user = config.get("auth_user") auth_password = config.get("auth_password") #for oauth1 oauth1_client_key = config.get("oauth1_client_key") oauth1_client_secret = config.get("oauth1_client_secret") oauth1_access_token = config.get("oauth1_access_token") oauth1_access_token_secret = config.get("oauth1_access_token_secret") #for oauth2 oauth2_token_type = config.get("oauth2_token_type", "Bearer") oauth2_access_token = config.get("oauth2_access_token") oauth2_refresh_token = config.get("oauth2_refresh_token") oauth2_refresh_url = config.get("oauth2_refresh_url") oauth2_refresh_props_str = config.get("oauth2_refresh_props") oauth2_client_id = config.get("oauth2_client_id") oauth2_client_secret = config.get("oauth2_client_secret") oauth2_refresh_props = {} if not oauth2_refresh_props_str is None: oauth2_refresh_props = dict((k.strip(), v.strip()) for k, v in ( item.split('=', 1) for item in oauth2_refresh_props_str.split(delimiter))) oauth2_refresh_props['client_id'] = oauth2_client_id oauth2_refresh_props['client_secret'] = oauth2_client_secret http_header_propertys = {} http_header_propertys_str = config.get("http_header_propertys") if not http_header_propertys_str is None: http_header_propertys = dict((k.strip(), v.strip()) for k, v in ( item.split('=', 1) for item in http_header_propertys_str.split(delimiter))) url_args = {} url_args_str = config.get("url_args") if not url_args_str is None: url_args = dict( (k.strip(), v.strip()) for k, v in (item.split('=', 1) for item in url_args_str.split(delimiter))) #json | xml | text response_type = config.get("response_type", "text") streaming_request = int(config.get("streaming_request", 0)) http_proxy = config.get("http_proxy") https_proxy = config.get("https_proxy") proxies = {} if not http_proxy is None: proxies["http"] = http_proxy if not https_proxy is None: proxies["https"] = https_proxy cookies = {} cookies_str = config.get("cookies") if not cookies_str is None: cookies = dict((k.strip(), v.strip()) for k, v in (item.split('=', 1) for item in cookies_str.split(delimiter))) request_timeout = int(config.get("request_timeout", 30)) backoff_time = int(config.get("backoff_time", 10)) sequential_stagger_time = int(config.get("sequential_stagger_time", 0)) polling_interval_string = config.get("polling_interval", "60") if polling_interval_string.isdigit(): polling_type = 'interval' polling_interval = int(polling_interval_string) else: polling_type = 'cron' cron_start_date = datetime.now() cron_iter = croniter(polling_interval_string, cron_start_date) index_error_response_codes = int( config.get("index_error_response_codes", 0)) response_filter_pattern = config.get("response_filter_pattern") if response_filter_pattern: global REGEX_PATTERN REGEX_PATTERN = re.compile(response_filter_pattern) response_handler_args = {} response_handler_args_str = config.get("response_handler_args") if not response_handler_args_str is None: response_handler_args = dict((k.strip(), v.strip()) for k, v in ( item.split('=', 1) for item in response_handler_args_str.split(delimiter))) response_handler = config.get("response_handler", "DefaultResponseHandler") module = __import__("responsehandlers") class_ = getattr(module, response_handler) global RESPONSE_HANDLER_INSTANCE RESPONSE_HANDLER_INSTANCE = class_(**response_handler_args) custom_auth_handler = config.get("custom_auth_handler") if custom_auth_handler: module = __import__("authhandlers") class_ = getattr(module, custom_auth_handler) custom_auth_handler_args = {} custom_auth_handler_args_str = config.get("custom_auth_handler_args") if not custom_auth_handler_args_str is None: custom_auth_handler_args = dict( (k.strip(), v.strip()) for k, v in ( item.split('=', 1) for item in custom_auth_handler_args_str.split(delimiter))) CUSTOM_AUTH_HANDLER_INSTANCE = class_(**custom_auth_handler_args) try: auth = None oauth2 = None if auth_type == "basic": auth = HTTPBasicAuth(auth_user, auth_password) elif auth_type == "digest": auth = HTTPDigestAuth(auth_user, auth_password) elif auth_type == "oauth1": auth = OAuth1(oauth1_client_key, oauth1_client_secret, oauth1_access_token, oauth1_access_token_secret) elif auth_type == "oauth2": token = {} token["token_type"] = oauth2_token_type token["access_token"] = oauth2_access_token token["refresh_token"] = oauth2_refresh_token token["expires_in"] = "5" client = WebApplicationClient(oauth2_client_id) oauth2 = OAuth2Session(client, token=token, auto_refresh_url=oauth2_refresh_url, auto_refresh_kwargs=oauth2_refresh_props, token_updater=oauth2_token_updater) elif auth_type == "custom" and CUSTOM_AUTH_HANDLER_INSTANCE: auth = CUSTOM_AUTH_HANDLER_INSTANCE req_args = { "verify": False, "stream": bool(streaming_request), "timeout": float(request_timeout) } if auth: req_args["auth"] = auth if url_args: req_args["params"] = url_args if cookies: req_args["cookies"] = cookies if http_header_propertys: req_args["headers"] = http_header_propertys if proxies: req_args["proxies"] = proxies if request_payload and not http_method == "GET": req_args["data"] = request_payload while True: if polling_type == 'cron': next_cron_firing = cron_iter.get_next(datetime) while get_current_datetime_for_cron() != next_cron_firing: time.sleep(float(10)) for endpoint in endpoint_list: if "params" in req_args: req_args_params_current = dictParameterToStringFormat( req_args["params"]) else: req_args_params_current = "" if "cookies" in req_args: req_args_cookies_current = dictParameterToStringFormat( req_args["cookies"]) else: req_args_cookies_current = "" if "headers" in req_args: req_args_headers_current = dictParameterToStringFormat( req_args["headers"]) else: req_args_headers_current = "" if "data" in req_args: req_args_data_current = req_args["data"] else: req_args_data_current = "" try: if oauth2: if http_method == "GET": r = oauth2.get(endpoint, **req_args) elif http_method == "POST": r = oauth2.post(endpoint, **req_args) elif http_method == "PUT": r = oauth2.put(endpoint, **req_args) elif http_method == "HEAD": r = oauth2.head(endpoint, **req_args) else: if http_method == "GET": r = requests.get(endpoint, **req_args) elif http_method == "POST": r = requests.post(endpoint, **req_args) elif http_method == "PUT": r = requests.put(endpoint, **req_args) elif http_method == "HEAD": r = requests.head(endpoint, **req_args) except requests.exceptions.Timeout, e: logging.error("HTTP Request Timeout error: %s" % str(e)) time.sleep(float(backoff_time)) continue except Exception as e: logging.error("Exception performing request: %s" % str(e)) time.sleep(float(backoff_time)) continue try: r.raise_for_status() if streaming_request: for line in r.iter_lines(): if line: handle_output(r, line, response_type, req_args, endpoint) else: handle_output(r, r.text, response_type, req_args, endpoint) except requests.exceptions.HTTPError, e: error_output = r.text error_http_code = r.status_code if index_error_response_codes: error_event = "" error_event += 'http_error_code = %s error_message = %s' % ( error_http_code, error_output) print_xml_single_instance_mode(error_event) sys.stdout.flush() logging.error("HTTP Request error: %s" % str(e)) time.sleep(float(backoff_time)) continue
def parse(self, response): conn = self.conn cursor = self.cursor print '------------------------------', "\n" self.conn = settings['MYSQL_CONN'] self.cursor = self.conn.cursor() sql = "SELECT id, id_vatgia, name, source_id, link FROM products WHERE source_id = 185 ORDER BY updated_at DESC" if self.env == "dev": # sql += " LIMIT 5" sql = "SELECT id, id_vatgia, name, source_id, link FROM products WHERE source_id = 185 AND id = 3426 ORDER BY updated_at DESC" self.cursor.execute(sql) products = self.cursor.fetchall() image_links = [] for product in products: url = 'http://graph.vatgia.vn/v1/products/estore/' + str( product['id_vatgia']) response = requests.get(url, auth=HTTPDigestAuth( API_VG_USER, API_VG_PASSWORD)) json = response.json() if 'data' in json: data = json['data'] # Lặp mảng danh sách gian hàng for merchant in data: print merchant['logo'] if merchant['logo'] == '' or merchant['logo'] == None: merchant['logo'] = 'http://giaca.org/images/grey.gif' thumbs = downloadImageFromUrl(merchant['logo']) # Upload bucket imageName = sha1FileName(merchant['logo']) # google_bucket_upload_object('static.giaca.org', thumbs['full'], 'uploads/full/' + imageName) # google_bucket_upload_object('static.giaca.org', thumbs['big'], 'uploads/thumbs/big/' + imageName) # google_bucket_upload_object('static.giaca.org', thumbs['small'], 'uploads/thumbs/small/' + imageName) item = {} item['merchant'] = { "name": merchant['url'].replace('http://www.', ''), "alias": merchant['company'], "logo_hash": sha1FileName(merchant['logo']), "is_craw": 1, "rating_count": merchant['total_rate'], "rating_5_count": merchant['good_rate'], "created_at": strftime("%Y-%m-%d %H:%M:%S"), "updated_at": strftime("%Y-%m-%d %H:%M:%S") } item['product'] = product item['price_item'] = { "title": product['name'], "price": merchant['price'], "source_id": product['source_id'], "link": merchant['url_product'], "create_at": strftime("%Y-%m-%d %H:%M:%S"), "updated_at": strftime("%Y-%m-%d %H:%M:%S"), "crawled_at": strftime("%Y-%m-%d %H:%M:%S") } yield item else: print response print 'ProductID: ' + str(product['id'])
def performOperation(self, operation): data = requests.get(self._url + operation, auth=HTTPDigestAuth(self._username, self._password), verify=False) return data
def __init__(self, addr, timeout, user=None, password=None, pin=None, proto='http', SSLCert=False): #API Constants self.MODE_OFF = 0 self.MODE_HEAT = 1 self.MODE_COOL = 2 self.MODE_AUTO = 3 self.STATE_IDLE = 0 self.STATE_HEATING = 1 self.STATE_COOLING = 2 self.STATE_LOCKOUT = 3 self.STATE_ERROR = 4 self.FAN_AUTO = 0 self.FAN_ON = 1 self.FANSTATE_OFF = 0 self.FANSTATE_ON = 1 self.TEMPUNITS_F = 0 self.TEMPUNITS_C = 1 self.SCHED_F = 0 self.SCHED_C = 1 self.SCHEDPART_MORNING = 0 self.SCHEDPART_DAY = 1 self.SCHEDPART_EVENING = 2 self.SCHEDPART_NIGHT = 3 self.SCHEDPART_INACTIVE = 255 self.AWAY_HOME = 0 self.AWAY_AWAY = 1 self.sensor_names = { "Control": ["Space Temp"], "Local": ["Thermostat"] } self.sensor_types = [ "Control", "Local", "Outdoor", "Remote", "Return", "Supply" ] #Input parameters self.addr = addr self.timeout = timeout #Use Python standard logging class self.log = logging.getLogger(__name__) #Preprocess authentication related parameters if pin is not None: self.pin = str(pin).zfill(4) else: self.pin = None if user != None and password != None: self.auth = HTTPDigestAuth(user, password) else: self.auth = None self.proto = proto self.SSLCert = SSLCert #Initialize State self.status = {} self.model = None self._api_ver = None self._type = None self._info = None self._sensors = None # # /control # self.setpointdelta = None self.heattemp = None self.cooltemp = None self.fan = None self.mode = None self.fanstate = None self.state = None # # /settings # self.name = None self.tempunits = None self.away = None self.schedule = None self.hum_setpoint = None self.dehum_setpoint = None self.hum_active = None
def _loadSCPD(self, serviceType, timeout): """Internal method to load the action definitions. :param str serviceType: the service type to load :param int timeout: the timeout for downloading """ if serviceType not in self.__deviceServiceDefinitions.keys(): raise ValueError( "Can not load SCPD, no service type defined for: " + serviceType) if "scpdURL" not in self.__deviceServiceDefinitions[serviceType].keys( ): raise ValueError("No SCPD URL defined for: " + serviceType) # remove actions for given service type self.__deviceSCPD.pop(serviceType, None) uri = self.__deviceServiceDefinitions[serviceType]["scpdURL"] # setup proxies proxies = {} if self.__httpsProxy: proxies = {"https": self.__httpsProxy} if self.__httpProxy: proxies = {"http": self.__httpProxy} # setup authentication auth = None if self.__password: auth = HTTPDigestAuth(self.__username, self.__password) # build the URL location = self.__protocol + "://" + self.__hostname + ":" + str( self.port) + uri # some devices response differently without a User-Agent headers = {"User-Agent": "Mozilla/5.0; SimpleTR64-2"} # http request request = requests.get(location, auth=auth, proxies=proxies, headers=headers, timeout=timeout) if request.status_code != 200: errorStr = DeviceTR64._extractErrorString(request) raise ValueError('Could not load SCPD for "' + serviceType + '" from ' + location + ': ' + str(request.status_code) + ' - ' + request.reason + " -- " + errorStr) data = request.text.encode('utf-8') if len(data) == 0: return # parse XML return try: root = ET.fromstring(data) except Exception as e: raise ValueError("Can not parse SCPD content for '" + serviceType + "' from '" + location + "': " + str(e)) actions = {} variableTypes = {} variableParameterDict = {} # iterate through the full XML tree for element in root.getchildren(): tagName = element.tag.lower() # go deeper for action lists if tagName.endswith("actionlist"): # remember the actions and where a specific variable gets referenced self._parseSCPDActions(element, actions, variableParameterDict) # go deeper for the variable declarations elif tagName.endswith("servicestatetable"): self._parseSCPDVariableTypes(element, variableTypes) # everything have been parsed now merge the variable declarations into the action parameters for name in variableParameterDict.keys(): if name not in variableTypes.keys(): raise ValueError( "Variable reference in action can not be resolved: " + name) # iterate through all arguments where this variable have been referenced for argument in variableParameterDict[name]: # fill in the type of this variable/argument argument["dataType"] = variableTypes[name]["dataType"] # if the variable declaration includes a default value add it to the action parameter as well if "defaultValue" in variableTypes[name].keys(): argument["defaultValue"] = variableTypes[name][ "defaultValue"] self.__deviceSCPD[serviceType] = actions
def test_list_hosts(self): conn = Connection(tc.hostname, HTTPDigestAuth(tc.admin, tc.password)) hosts = Host.list(conn) self.assertGreater(len(hosts), 0) self.assertIsNotNone(hosts[0])
def build_auth(user, password): return HTTPDigestAuth(user, password)
def test_DIGEST_AUTH_SETS_SESSION_COOKIES(self): url = httpbin('digest-auth', 'auth', 'user', 'pass') auth = HTTPDigestAuth('user', 'pass') s = requests.Session() s.get(url, auth=auth) assert s.cookies['fake'] == 'fake_value'
# list_of_cameras = open (r"D:\\test.txt","w+") list_of_cameras = open("D:\\test.txt", 'r') print(list_of_cameras.read()) for i in list_of_cameras: i = str(i) ip_addr = i.strip() print(ip_addr) # u_name = str(input("Enter the Username:"******"Enter the Password:"******"URL NOT found")
def run( # pylint: disable=too-many-arguments schema: str, auth: Optional[Tuple[str, str]], auth_type: str, headers: Dict[str, str], checks: Iterable[str] = DEFAULT_CHECKS_NAMES, endpoints: Optional[Filter] = None, methods: Optional[Filter] = None, tags: Optional[Filter] = None, base_url: Optional[str] = None, request_timeout: Optional[int] = None, hypothesis_deadline: Optional[int] = None, hypothesis_derandomize: Optional[bool] = None, hypothesis_max_examples: Optional[int] = None, hypothesis_phases: Optional[List[hypothesis.Phase]] = None, hypothesis_report_multiple_bugs: Optional[bool] = None, hypothesis_suppress_health_check: Optional[List[ hypothesis.HealthCheck]] = None, hypothesis_seed: Optional[int] = None, hypothesis_verbosity: Optional[hypothesis.Verbosity] = None, ) -> None: """Perform schemathesis test against an API specified by SCHEMA. SCHEMA must be a valid URL or file path pointing to an Open API / Swagger specification. """ # pylint: disable=too-many-locals selected_checks = tuple(check for check in runner.checks.ALL_CHECKS if check.__name__ in checks) if auth and auth_type == "digest": auth = HTTPDigestAuth(*auth) # type: ignore options = dict_true_values( api_options=dict_true_values(auth=auth, headers=headers, request_timeout=request_timeout), loader_options=dict_true_values(base_url=base_url, endpoint=endpoints, method=methods, tag=tags), hypothesis_options=dict_not_none_values( deadline=hypothesis_deadline, derandomize=hypothesis_derandomize, max_examples=hypothesis_max_examples, phases=hypothesis_phases, report_multiple_bugs=hypothesis_report_multiple_bugs, suppress_health_check=hypothesis_suppress_health_check, verbosity=hypothesis_verbosity, ), seed=hypothesis_seed, ) with abort_on_network_errors(): if pathlib.Path(schema).is_file(): prepared_runner = runner.prepare(schema, checks=selected_checks, loader=from_path, **options) else: prepared_runner = runner.prepare(schema, checks=selected_checks, **options) output_style = cast(Callable, OutputStyle.default) execute(prepared_runner, output_style)
def getHosts(): response = requests.get(hostsEndpoint, auth=HTTPDigestAuth(args.username, args.apiKey), verify=False) response.raise_for_status() return response.json()
def PostMaterial(hostname, id, key, filepath): filename = os.path.basename(filepath) f = {'file': (filename, open(filepath, "rb"), "text/xml")} r = requests.post("http://%s/api/v1/materials" % hostname, files = f, auth = HTTPDigestAuth(id, key)) print("Posted %s: %d %s" % (filepath, r.status_code, r.text))
def _getReq(self, path): try: if self._connfail: self._connfail -= 1 return None resp = self._session.get(BASE_URL.format(self._host, path), verify=False, auth=HTTPDigestAuth(self._user, self._password), timeout=TIMEOUT) self.on = True return json.loads(resp.text) except requests.exceptions.RequestException as err: self._connfail = CONNFAILCOUNT self.on = False return None
def fetch_download(self, _list, ftp=False, missing=False): """Creates the download dataset and then downloads the RELEASE.""" dataset = "{}/download/{}".format(self.iocroot, self.release) fresh = False if not os.path.isdir(dataset): fresh = True Popen(["zfs", "create", "-o", "compression=lz4", "{}/iocage/download/{}".format(self.pool, self.release)]).communicate() if missing or fresh: os.chdir("{}/download/{}".format(self.iocroot, self.release)) if self.http: for f in _list: if self.hardened: _file = "{}/{}/{}".format(self.server, self.root_dir, f) if f == "lib32.txz": continue else: _file = "{}/{}/{}/{}".format(self.server, self.root_dir, self.release, f) if self.auth == "basic": r = requests.get(_file, auth=(self.user, self.password), verify=self.verify, stream=True) elif self.auth == "digest": r = requests.get(_file, auth=HTTPDigestAuth( self.user, self.password), verify=self.verify, stream=True) else: r = requests.get(_file, verify=self.verify, stream=True) status = r.status_code == requests.codes.ok if not status: r.raise_for_status() with open(f, "wb") as txz: pbar = tqdm(total=int(r.headers.get('content-length')), bar_format="{desc}{percentage:3.0f}%" " {rate_fmt}" " Elapsed: {elapsed}" " Remaining: {remaining}", unit="bit", unit_scale="mega") pbar.set_description("Downloading: {}".format(f)) for chunk in r.iter_content(chunk_size=1024): txz.write(chunk) pbar.update(len(chunk)) pbar.close() elif ftp: for f in _list: _ftp = self.__fetch_ftp_connect__() _ftp.cwd(self.release) if bool(re.compile( r"MANIFEST|base.txz|lib32.txz|doc.txz").match(f)): try: _ftp.voidcmd('TYPE I') filesize = _ftp.size(f) with open(f, "wb") as txz: pbar = tqdm(total=filesize, bar_format="{desc}{" "percentage:3.0f}%" " {rate_fmt}" " Elapsed: {elapsed}" " Remaining: {" "remaining}", unit="bit", unit_scale="mega") pbar.set_description( "Downloading: {}".format(f)) def callback(chunk): txz.write(chunk) pbar.update(len(chunk)) _ftp.retrbinary("RETR {}".format(f), callback) pbar.close() _ftp.quit() except: raise else: pass
def _postReq(self, path, data): try: if self._connfail: self._connfail -= 1 return False resp = self._session.post(BASE_URL.format(self._host, path), data=json.dumps(data), verify=False, auth=HTTPDigestAuth(self._user, self._password), timeout=TIMEOUT) self.on = True if resp.status_code == 200: return True else: return False except requests.exceptions.RequestException as err: self._connfail = CONNFAILCOUNT self.on = False return False
def setUpClass(cls): HeadlessRunnerTest.setUpClass() cls.DIGEST_CREDENTIALS = HTTPDigestAuth('*****@*****.**', 'tester150411') cls.prepare_submission_data()
def api_test(): if not session.get('logged_in'): return redirect(url_for('login')) # 获取post传递过来的参数 project_name = request.form['project'] try: pak_name = json.loads(request.form['pak'])['object_name'] pak = json.loads(request.form['pak'])['type'] req_host = request.form['host'] req_url = 'http://' + req_host + request.form['url'] req_method = request.form['method'] except: pak = 'none' api_name = request.form['name'] req_host = g.db.execute('select host from project where name="%s";' % project_name).fetchall()[0][0] req_method, req_url = g.db.execute( 'select method, url from api where name="%s" and project_name="%s";' % (api_name, project_name)).fetchall()[0] req_url = 'http://' + req_host + req_url # 一所视图库项目需要在json对象外层包裹object, objectlist的特殊处理 if request.form['data'] == '""': req_data = {} else: if pak == 'Object': req_data = {pak_name + 'Object': json.loads(request.form['data'])} elif pak == 'ListObject': if isinstance(json.loads(request.form['data']), list): if pak_name in ['VideoSlice', 'File', 'Image', 'Case']: req_data = { pak_name + 'ListObject': { pak_name: json.loads(request.form['data']) } } else: req_data = { pak_name + 'ListObject': { pak_name + 'Object': json.loads(request.form['data']) } } else: if pak_name in ['VideoSlice', 'File', 'Image']: req_data = { pak_name + 'ListObject': { pak_name: [json.loads(request.form['data'])] } } else: req_data = { pak_name + 'ListObject': { pak_name + 'Object': [json.loads(request.form['data'])] } } else: req_data = json.loads(request.form['data']) # 关键字替换变量 Interface.dynamic_params(req_data) # 授权的处理 req_auth = request.form['auth'] # 头部处理 if request.form['headers'] == '""': req_headers = {} else: req_headers = json.loads(request.form['headers']) # 刷新cookie Interface.host = req_host.split(':')[0] user_passwd = g.db.execute( 'select user_passwd from project where name="%s"' % project_name).fetchall()[0][0] if user_passwd: user_passwd = json.loads(user_passwd) try: r = set_cookie('公共-用户-用户登录', project_name, user_passwd) #print('cookie设置成功',project_name,r,Interface.cookie) except: #print('cookie设置失败',project_name,r,Interface.cookie) pass req_headers['Cookie'] = Interface.cookie # 进行请求 try: if req_method == 'GET': if req_auth == '"none"': t = time.time() req = requests.get(req_url, params=req_data, headers=req_headers) print('\n', '[GET]', req.url) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' else: req_auth = json.loads(req_auth) if req_auth['auth_method'] == 'Basic': t = time.time() req = requests.get(req_url, params=req_data, headers=req_headers, auth=HTTPBasicAuth( req_auth['username'], req_auth['password'])) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' elif req_auth['auth_method'] == 'Digst': t = time.time() req = requests.get(req_url, params=req_data, headers=req_headers, auth=HTTPDigestAuth( req_auth['username'], req_auth['password'])) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' elif req_method == 'POST': if req_auth == '"none"': files = {} for key in req_data: if isinstance(req_data[key], str): if '*file.' in req_data[key]: tmp = req_data[key].replace('*file.', '') files[key] = (str(uuid.uuid1()) + tmp.split('.')[-1], open('image/' + tmp, 'rb')) for key in files: del req_data[key] t = time.time() if files != {}: req = requests.post(req_url, params=req_data, files=files, headers=req_headers) else: req = requests.post(req_url, json=req_data, headers=req_headers) print('\n', '[POST]', req.url, req_data) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' else: req_auth = json.loads(req_auth) if req_auth['auth_method'] == 'Basic': t = time.time() req = requests.post(req_url, json=req_data, headers=req_headers, auth=HTTPBasicAuth( req_auth['username'], req_auth['password'])) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' elif req_auth['auth_method'] == 'Digst': t = time.time() req = requests.post(req_url, json=req_data, headers=req_headers, auth=HTTPDigestAuth( req_auth['username'], req_auth['password'])) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' elif req_method == 'PUT': if req_auth == '"none"': t = time.time() req = requests.put(req_url, json=req_data, headers=req_headers) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' else: req_auth = json.loads(req_auth) if req_auth['auth_method'] == 'Basic': t = time.time() req = requests.put(req_url, json=req_data, headers=req_headers, auth=HTTPBasicAuth( req_auth['username'], req_auth['password'])) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' elif req_auth['auth_method'] == 'Digst': t = time.time() req = requests.put(req_url, json=req_data, headers=req_headers, auth=HTTPDigestAuth( req_auth['username'], req_auth['password'])) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' elif req_method == 'DELETE': if req_auth == '"none"': t = time.time() req = requests.delete(req_url, headers=req_headers) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' else: req_auth = json.loads(req_auth) if req_auth['auth_method'] == 'Basic': t = time.time() req = requests.delete(req_url, headers=req_headers, auth=HTTPBasicAuth( req_auth['username'], req_auth['password'])) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' elif req_auth['auth_method'] == 'Digst': t = time.time() req = requests.delete(req_url, headers=req_headers, auth=HTTPDigestAuth( req_auth['username'], req_auth['password'])) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' # 响应处理 res_headers = dict(req.headers) res_status_code = req.status_code try: res_data = req.json() except: res_data = str(req.text) res_time = delt_t except Exception as e: print(repr(e)) res_status_code = 500 res_time = 0 res_data = {} res_headers = {} return jsonify({ 'status_code': res_status_code, 'request_time': res_time, 'response': res_data, 'res_h': res_headers, 'request': req_data })
def __init__(self, username, apikey, print_urls=None): self._username = username self._apikey = apikey self._print_urls = print_urls self._auth = HTTPDigestAuth(self._username, self._apikey)
def _auth(machine='portal.nersc.gov'): """Get authentication credentials. """ n = netrc() u, foo, p = n.authenticators(machine) return HTTPDigestAuth(u, p)
def __post_request(self, path, **kwargs): """ Posts the request to defined url. :param path: path for get request api :param headers: headers parameter for adding custom headers :return: This method return POST request response """ # 1. Check Url build properly # 2. validate json_data is valid # 3. If there are specific headers , make sure we sending with specific # headers # 4. Once We get Response # 5. Read Response code in to a variable (Data type TBD) # 6. Read Http headers in to another variable try: parameters = kwargs.get('parameters', None) file_paths = kwargs.get('file_paths', None) rheaders = kwargs.get('headers', None) if self.session: req = self.req_session else: req = requests # Adding files to POST parameters self.headers = self.test_data["headers"] if rheaders: self.headers.update(rheaders) if file_paths: upload_data = self.__upload_files(parameters, file_paths) self.headers['Content-Type'] = upload_data.content_type parameters = upload_data response = {} url_path = self.url + path self.log.info('************* POST request URL is {} ' '*************'.format(url_path)) self.log.info('POST request parameters... {}'.format(parameters)) # Checking authentication flag to send auth details. if self.auth_type == 'HTTPDigestAuth': res = req.post(url_path, data=parameters, headers=self.headers, auth=HTTPDigestAuth(self.username, self.password)) elif self.auth_type == 'HTTPBasicAuth': # self.json_data = self.json_obj.dump_json_data(parameters) res = req.post(url_path, data=parameters, headers=self.headers, auth=HTTPBasicAuth(self.username, self.password)) else: res = req.post(url_path, data=parameters, headers=self.headers) self.log.info('Received response code is {}'.format( res.status_code)) self.log.info('Received response data is {}'.format(res.text)) self.log.debug('Received response headers is {}'.format( res.headers)) self.log.info('*************----END----*************') response['code'] = res.status_code # rest_data = self.json_obj.load_json_data(str(res_data)) response['data'] = res.text response['headers'] = res.headers # Returning response status code, data and headers # return res_status_code, rest_data, res_headers return response except Exception as e: self.log.exception("POST request {} Failed with exception " "{}".format(url_path, e)) traceback.print_exc() except FileNotFoundError as fe: self.log.exception("POST request {} Failed with File not found " "{}".format(url_path, fe))
def __init__(self, addr, timeout, user=None, password=None, proto='http', SSLCert=False): #API Constants self.MODE_OFF = 0 self.MODE_HEAT = 1 self.MODE_COOL = 2 self.MODE_AUTO = 3 self.STATE_IDLE = 0 self.STATE_HEATING = 1 self.STATE_COOLING = 2 self.STATE_LOCKOUT = 3 self.STATE_ERROR = 4 self.FAN_AUTO = 0 self.FAN_ON = 1 self.FANSTATE_OFF = 0 self.FANSTATE_ON = 1 self.TEMPUNITS_F = 0 self.TEMPUNITS_C = 1 self.SCHED_F = 0 self.SCHED_C = 1 self.SCHEDPART_MORNING = 0 self.SCHEDPART_DAY = 1 self.SCHEDPART_EVENING = 2 self.SCHEDPART_NIGHT = 3 self.SCHEDPART_INACTIVE = 255 #self.AWAY_HOME = 0 #self.AWAY_AWAY = 1 #Input parameters self.addr = addr self.timeout = timeout if user != None and password != None: self.auth = HTTPDigestAuth(user, password) else: self.auth = None self.proto = proto self.SSLCert = SSLCert #Initialize State self.status = {} self._api_ver = None self._type = None self._info = None self._sensors = None # # /control # self.setpointdelta = None self.heattemp = None self.cooltemp = None self.fan = None self.mode = None self.fanstate = None self.state = None # # /settings # self.name = None self.tempunits = None #self.away = None self.schedule = None self.hum_setpoint = None self.dehum_setpoint = None self.hum_active = None