def level_25() -> None: print(f"\033[31mLevel 25\033[0m") from urllib import request, error import wave from PIL import Image password_mgr = request.HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(None, "http://www.pythonchallenge.com/", "butter", "fly") handler = request.HTTPBasicAuthHandler(password_mgr) opener = request.build_opener(handler) for i in range(1, 0): print(f"Processing {i:2d}...", end='\t') url = f"http://www.pythonchallenge.com/pc/hex/lake{i}.wav" try: response = opener.open(url) except error.HTTPError: print("HTTP ERROR 404") else: with open(f"level_25/lake{i}.wav", "wb") as file: data = response.read() file.write(data) print("Successfully Saved.") im = Image.new("RGB", (300, 300)) for i in range(1, 26): with wave.open(f"level_25/lake{i}.wav", "rb") as file: data = file.readframes(file.getnframes()) block_im = Image.frombytes("RGB", (60, 60), data) x, y = (i - 1) % 5 * 60, (i - 1) // 5 * 60 im.paste(block_im, (x, y)) im.save("level_25/result.jpg")
def metricCollector(): data = {} #defaults data['plugin_version'] = PLUGIN_VERSION data['heartbeat_required'] = HEARTBEAT data['units'] = METRICS_UNITS URL = "http://" + COUCHDB_HOST + ":" + COUCHDB_PORT + COUCHDB_STATS_URI try: if COUCHDB_USERNAME and COUCHDB_PASSWORD: password_mgr = connector.HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(REALM, URL, COUCHDB_USERNAME, COUCHDB_PASSWORD) auth_handler = connector.HTTPBasicAuthHandler(password_mgr) opener = connector.build_opener(auth_handler) connector.install_opener(opener) response = connector.urlopen(URL, timeout=10) byte_responseData = response.read() str_responseData = byte_responseData.decode('UTF-8') couch_dict = json.loads(str_responseData) for attribute, attribute_value in couch_dict.items(): for metric, val in attribute_value.items(): if 'current' in val and val['current'] is not None: if metric in METRICS_KEY_VS_NAME: metric = METRICS_KEY_VS_NAME[metric] data[metric] = val['current'] except Exception as e: data['status'] = 0 data['msg'] = str(e) return data
def _collect_metrics(self): try: auth_handler = None if self.username and self.password: password_mgr = urlconnection.HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(None, self.url, self.username, self.password) auth_handler = urlconnection.HTTPBasicAuthHandler(password_mgr) if auth_handler is not None: opener = urlconnection.build_opener(auth_handler) urlconnection.install_opener(opener) response = urlconnection.urlopen(self.url, timeout=self.timeout) if response.getcode() == 200: response_data = response.read().decode('UTF-8') self._parse_data_(response_data) else: self.data['status'] = 0 self.data['msg'] = 'Error_code' + str(response.getcode()) except Exception as e: self.data['status'] = 0 self.data['msg'] = str(e) + ": " + self.url return self.data
def download(filename, outputfolder, username='', password=''): categories = [filename] folder = os.getcwd() + '\\' + outputfolder for category in categories: os.makedirs(folder, exist_ok=True) with open('%s' % category, 'r') as file: urls = file.readlines() n_urls = len(urls) for i, url in enumerate(urls): f = folder + '\\' + url.split('/')[-1] url = url.strip() passman = request.HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, url.strip(), username, password) auth_handler = request.HTTPBasicAuthHandler(passman) opener = request.build_opener(auth_handler) request.install_opener(opener) request.urlopen(url.strip()) try: urlretrieve(url.strip(), f.strip()) print('%s %i/%i' % (category, i, n_urls)) except: print('%s %i/%i' % (category, i, n_urls), 'no file')
def _create_auth(self): user, password = self.credentials.split(':', 1) pwd_manager = request.HTTPPasswordMgrWithDefaultRealm() pwd_manager.add_password(None, self.base_url, user, password) handler = request.HTTPBasicAuthHandler(pwd_manager) opener = request.build_opener(handler) request.install_opener(opener)
def setup_earthdata_login_auth(endpoint): """ Set up the request library so that it authenticates against the given Earthdata Login endpoint and is able to track cookies between requests. This looks in the .netrc file first and if no credentials are found, it prompts for them. Valid endpoints include: urs.earthdata.nasa.gov - Earthdata Login production """ try: username, _, password = netrc.netrc().authenticators(endpoint) except (FileNotFoundError, TypeError): # FileNotFound = There's no .netrc file # TypeError = The endpoint isn't in the netrc file, causing the above to try unpacking None print( "There's no .netrc file or the The endpoint isn't in the netrc file" ) manager = request.HTTPPasswordMgrWithDefaultRealm() manager.add_password(None, endpoint, username, password) auth = request.HTTPBasicAuthHandler(manager) jar = CookieJar() processor = request.HTTPCookieProcessor(jar) opener = request.build_opener(auth, processor) request.install_opener(opener)
def metric_collector(self): try: if self._userName and self._userPass: password_mgr = urlconnection.HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(None, url, self._userName, self._userPass) auth_handler = urlconnection.HTTPBasicAuthHandler(password_mgr) opener = urlconnection.build_opener(auth_handler) urlconnection.install_opener(opener) response = urlconnection.urlopen(url, timeout=10) if response.getcode() == 200: byte_responseData = response.read() str_responseData = byte_responseData.decode('UTF-8') self._parseStats(str_responseData) else: self.dictApacheData['status'] = 0 self.dictApacheData['msg'] = 'Error_code' + str( response.getcode()) except HTTPError as e: self.dictApacheData['status'] = 0 self.dictApacheData['msg'] = 'Error_code : HTTP Error ' + str( e.code) except URLError as e: self.dictApacheData['status'] = 0 self.dictApacheData['msg'] = 'Error_code : URL Error ' + str( e.reason) except InvalidURL as e: self.dictApacheData['status'] = 0 self.dictApacheData['msg'] = 'Error_code : Invalid URL' except Exception as e: self.dictApacheData['status'] = 0 self.dictApacheData[ 'msg'] = 'Exception occured in collecting data : ' + str(e)
def metrics_collector(self): try: url = "http://" + self.host + ":" + self.port + "/metrics/" + self.apikey + "/metrics?pretty=true" auth_handler = urlconnection.HTTPBasicAuthHandler( (urlconnection.HTTPPasswordMgrWithDefaultRealm()).add_password( None, url, self.username, self.password)) response = (urlconnection.urlopen(url)).read().decode('UTF-8') response = json.loads(response) data = response["gauges"] self.resultjson["jobs_count"] = data["jenkins.job.count.value"][ "value"] data = response["meters"] self.resultjson["jobs_scheduled_rate"] = data1[ "jenkins.job.scheduled"]["mean_rate"] data = response["timers"] self.resultjson["jobs_blocked_duration"] = data[ "jenkins.job.blocked.duration"]["mean"] self.resultjson["jobs_buildable_duration"] = data[ "jenkins.job.buildable.duration"]["mean"] self.resultjson["jobs_execution_time"] = data[ "jenkins.job.execution.time"]["mean"] self.resultjson["jobs_queuing_duration"] = data[ "jenkins.job.queuing.duration"]["mean"] self.resultjson["jobs_total_duration"] = data[ "jenkins.job.total.duration"]["mean"] self.resultjson["jobs_waiting_duration"] = data[ "jenkins.job.waiting.duration"]["mean"] except Exception as e: self.resultjson["msg"] = str(e) self.resultjson["status"] = 0 return self.resultjson
def __init__(self, user='', password=''): self.pswd_mgr = request.HTTPPasswordMgrWithDefaultRealm() self.pswd_mgr.add_password(None, self._moex_auth_url, user, password) self.auth_handler = request.HTTPBasicAuthHandler(self.pswd_mgr) self.ctx = ssl.create_default_context(purpose=ssl.Purpose.SERVER_AUTH) self.https_handler = request.HTTPSHandler(self.ctx) self._coo_jar = cookiejar.CookieJar() self.cookie_processor = request.HTTPCookieProcessor(self._coo_jar) self.iss_opener = request.build_opener() #self.iss_opener.add_handler(self.auth_handler) #self.iss_opener.add_handler(self.https_handler) self.iss_opener.add_handler(self.cookie_processor) log_dir = os.path.expanduser('~Александр\\issbonddataload\\') if not os.path.isdir(log_dir): os.mkdir(log_dir) self._f_log = open(os.path.join(log_dir, 'update.log'), 'at', encoding='utf-8') with open('.\\issbonddataload\\config.ini', 'rt') as fhand: for l in fhand: p = l.strip().split('=') if len(p) == 2: self.dbconn_params[p[0]] = p[1] return
def _http_opener(self, url, headers=None, auth=True): """ Configure a HTTP opener for sync operations @param url: the target URL """ repository = self.repository config = repository.config # Configure opener headers addheaders = [] if headers: addheaders.extend(headers) # Configure opener handlers handlers = [] # Proxy handling proxy = repository.proxy or config.proxy or None if proxy: # Figure out the protocol from the URL url_split = url.split("://", 1) if len(url_split) == 2: protocol = url_split[0] else: protocol = "http" proxy_handler = urllib2.ProxyHandler({protocol: proxy}) handlers.append(proxy_handler) # Authentication handling if auth: username = repository.username password = repository.password if username and password: # Add a 401 handler (in case Auth header is not accepted) passwd_manager = urllib2.HTTPPasswordMgrWithDefaultRealm() passwd_manager.add_password(realm = None, uri = url, user = username, passwd = password, ) auth_handler = urllib2.HTTPBasicAuthHandler(passwd_manager) handlers.append(auth_handler) # Create the opener opener = urllib2.build_opener(*handlers) if auth and username and password: # Send credentials unsolicitedly to force login - otherwise # the request would be treated as anonymous if login is not # required (i.e. no 401 triggered), but we want to login in # any case: import base64 base64string = base64.encodestring('%s:%s' % (username, password))[:-1] addheaders.append(("Authorization", "Basic %s" % base64string)) if addheaders: opener.addheaders = addheaders return opener
def connect(): """ Deal with authentification """ passwordMgr = urllib2.HTTPPasswordMgrWithDefaultRealm() passwordMgr.add_password(None, S2_API_URL, S2_API_USER, S2_API_PASSWORD) handler = urllib2.HTTPBasicAuthHandler(passwordMgr) opener = urllib2.build_opener(handler) # create "opener" (OpenerDirector instance) urllib2.install_opener(opener) # Now all calls to urllib2.urlopen use our opener.# Install the opener.
def __init__(self, ip, user, password, debug=False): """ Instantiate a object of the class IP_CAM. :param ip: String representing the camera IP :param user: String with the camera username :param password: String with the camera password :param debug: Boolean (print debug messages). """ self.cam_url = 'http://{0}/'.format(ip) self.debug = debug # Try to connect to the cam and create the streamer if PYTHON2: password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(None, self.cam_url, user, password) handler = urllib2.HTTPBasicAuthHandler(password_mgr) self.opener = urllib2.build_opener(handler) else: password_mgr = urllib.HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(None, self.cam_url, 'admin', 'uffdr1') handler = urllib.HTTPBasicAuthHandler(password_mgr) self.opener = urllib.build_opener(handler) if not self.test_connection(): raise url_error("|| Cannot comunicate with camera ||")
def dl(url, username, password): if not url: return 1 file_name = url.split('/')[-1] file_path = os.path.join(download_path, file_name) base_url = uparse(url).scheme + "://" + uparse(url).netloc pwmg = req.HTTPPasswordMgrWithDefaultRealm() pwmg.add_password(None, base_url, username, password) handler = req.HTTPBasicAuthHandler(pwmg) opener = req.build_opener(handler) opener.open(url) req.install_opener(opener) u = req.urlopen(url) f = open(file_path, 'wb') file_size_dl = 0 block_sz = 8192 while True: buf = u.read(block_sz) if not buf: break file_size_dl += len(buf) f.write(buf) f.close()
def resetProxies(self, httpProxyTuple): # for ntlm user and password are required self.hasNTLM = False if isinstance(httpProxyTuple,(tuple,list)) and len(httpProxyTuple) == 5: useOsProxy, _urlAddr, _urlPort, user, password = httpProxyTuple _proxyDirFmt = proxyDirFmt(httpProxyTuple) # only try ntlm if user and password are provided because passman is needed if user and not useOsProxy: for pluginXbrlMethod in pluginClassMethods("Proxy.HTTPNtlmAuthHandler"): HTTPNtlmAuthHandler = pluginXbrlMethod() if HTTPNtlmAuthHandler is not None: self.hasNTLM = True if not self.hasNTLM: # try for python site-packages ntlm try: from ntlm import HTTPNtlmAuthHandler self.hasNTLM = True except ImportError: pass if self.hasNTLM: pwrdmgr = proxyhandlers.HTTPPasswordMgrWithDefaultRealm() pwrdmgr.add_password(None, _proxyDirFmt["http"], user, password) self.proxy_handler = proxyhandlers.ProxyHandler({}) self.proxy_auth_handler = proxyhandlers.ProxyBasicAuthHandler(pwrdmgr) self.http_auth_handler = proxyhandlers.HTTPBasicAuthHandler(pwrdmgr) self.ntlm_auth_handler = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(pwrdmgr) self.opener = proxyhandlers.build_opener(self.proxy_handler, self.ntlm_auth_handler, self.proxy_auth_handler, self.http_auth_handler) if not self.hasNTLM: self.proxy_handler = proxyhandlers.ProxyHandler(proxyDirFmt(httpProxyTuple)) self.proxy_auth_handler = proxyhandlers.ProxyBasicAuthHandler() self.http_auth_handler = proxyhandlers.HTTPBasicAuthHandler() self.opener = proxyhandlers.build_opener(self.proxy_handler, self.proxy_auth_handler, self.http_auth_handler)
def _get_request_data_(self): PYTHON_MAJOR_VERSION = sys.version_info[0] if PYTHON_MAJOR_VERSION == 3: import urllib.request as urlconnection elif PYTHON_MAJOR_VERSION == 2: import urllib2 as urlconnection try: url = self._config_data_['url'] if self._config_data_['username'] and self._config_data_[ 'password']: password_mgr = urlconnection.HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(None, url, self._config_data_['username'], self._config_data_['password']) auth_handler = urlconnection.HTTPBasicAuthHandler(password_mgr) proxy_support = urlconnection.ProxyHandler({}) opener = urlconnection.build_opener(auth_handler, proxy_support) urlconnection.install_opener(opener) response = urlconnection.urlopen( url, timeout=self._config_data_['timeout']) return response.read() except Exception as e: self.data['status'] = 0 #self.data['msg'] = str(e.code) + " " + str(e.reason) self.data['msg'] = str(e)
def urlretrieve(url, filename, data=None, auth=None): if auth is not None: # https://docs.python.org/2.7/howto/urllib2.html#id6 password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() # Add the username and password. # If we knew the realm, we could use it instead of None. username, password = auth top_level_url = urlparse(url).netloc password_mgr.add_password(None, top_level_url, username, password) handler = urllib2.HTTPBasicAuthHandler(password_mgr) # create "opener" (OpenerDirector instance) opener = urllib2.build_opener(handler) else: opener = urllib2.build_opener() res = opener.open(url, data=data) headers = res.info() with open(filename, "wb") as fp: fp.write(res.read()) return filename, headers
def connect(base_url='https://scihub.copernicus.eu/apihub/', uname=None, pword=None): '''A helper function to connect and authenticate to the Copernicus' scihub. Args: base_url (str): basic url to the Copernicus' scihub uname (str): username of Copernicus' scihub pword (str): password of Copernicus' scihub Returns: opener: an urllib opener instance ot Copernicus' scihub ''' if not uname: logger.debug('If you do not have a Copernicus Scihub user' ' account go to: https://scihub.copernicus.eu') uname = input(' Your Copernicus Scihub Username:'******' Your Copernicus Scihub Password:') # open a connection to the scihub manager = request.HTTPPasswordMgrWithDefaultRealm() manager.add_password(None, base_url, uname, pword) handler = request.HTTPBasicAuthHandler(manager) opener = request.build_opener(handler) return opener
def __add_openers(self): # TODO add error handling self.opener = urllib2.build_opener() # Proxy handling # TODO currently self.proxies isn't parsed from configuration file # if len(self.proxies) > 0: # for proxy in self.proxies: # url = proxy['url'] # # TODO test this: # if "user" in proxy and "pass" in proxy: # if url.lower().startswith('https://'): # url = 'https://' + proxy['user'] + ':' + proxy['pass'] + '@' + url[8:] # else: # url = 'http://' + proxy['user'] + ':' + proxy['pass'] + '@' + url[7:] # # FIXME move proxy auth to sth like this: # # passman = urllib2.HTTPPasswordMgrWithDefaultRealm() # # passman.add_password(None, url, proxy['user'], proxy['password']) # # opener.add_handler(urllib2.HTTPBasicAuthHandler(passman)) # # if url.lower().startswith('https://'): # opener.add_handler(urllib2.ProxyHandler({'https': url})) # else: # opener.add_handler(urllib2.ProxyHandler({'https': url})) # HTTP Basic Auth if self.user is not None and self.password is not None: passman = urllib2.HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, self.url, self.user, self.password) self.opener.add_handler(urllib2.HTTPBasicAuthHandler(passman)) self.debug("Enabling HTTP basic auth")
def urlopen(url, if_modified_since=None): parse_result = urllib_parse.urlparse(url) if parse_result.scheme not in ("http", "https"): return _urlopen(url) else: netloc = urllib_parse_splituser(parse_result.netloc)[1] url = urllib_parse.urlunparse( (parse_result.scheme, netloc, parse_result.path, parse_result.params, parse_result.query, parse_result.fragment)) password_manager = urllib_request.HTTPPasswordMgrWithDefaultRealm() request = urllib_request.Request(url) request.add_header('User-Agent', 'Gentoo Portage') if if_modified_since: request.add_header('If-Modified-Since', _timestamp_to_http(if_modified_since)) if parse_result.username is not None: password_manager.add_password(None, url, parse_result.username, parse_result.password) auth_handler = CompressedResponseProcessor(password_manager) opener = urllib_request.build_opener(auth_handler) hdl = opener.open(request) if hdl.headers.get('last-modified', ''): try: add_header = hdl.headers.add_header except AttributeError: # Python 2 add_header = hdl.headers.addheader add_header('timestamp', _http_to_timestamp(hdl.headers.get('last-modified'))) return hdl
def __init__(self, base_url="http://service.iris.edu/irisws", user="", password="", timeout=20, debug=False, user_agent=DEFAULT_USER_AGENT, major_versions={}): """ Initializes the IRIS Web service client. See :mod:`obspy.clients.iris` for all parameters. """ self.base_url = base_url self.timeout = timeout self.debug = debug self.user_agent = user_agent self.major_versions = DEFAULT_SERVICE_VERSIONS self.major_versions.update(major_versions) # Create an OpenerDirector for Basic HTTP Authentication password_mgr = urllib_request.HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(None, base_url, user, password) auth_handler = urllib_request.HTTPBasicAuthHandler(password_mgr) opener = urllib_request.build_opener(auth_handler) # install globally urllib_request.install_opener(opener)
def check_url_connectivity(URL, USERNAME, PASSWORD, CONTENT_CHECK): result = {} if (URL == None): result["msg"] = "url cant be none, provide url" result["status"] = 0 else: try: if USERNAME and PASSWORD: password_mgr = connector.HTTPPasswordMgrWithDefaultRealm() password_mgr.password(realm, URL, USERNAME, PASSWORD) auth_handler = connector.HTTPBasicAuthHandler(password_mgr) opener = connector.build_opener(opener) start = time.time() response = connector.urlopen(URL) end = time.time() response_time = round((end - start) * 1000) if CONTENT_CHECK and CONTENT_CHECK != 'None': result = check_response_content(response, CONTENT_CHECK) result["content_check_string"] = str(CONTENT_CHECK) if "status" not in result.keys(): result["response_time"] = response_time result["status_code"] = response.code result["metric_units"] = METRIC_UNITS except Exception as e: result["msg"] = str(e) result["status"] = 0 result["plugin_version"] = PLUGIN_VERSION result["heartbeat_required"] = HEARTBEAT return result
def readUrl(self,host,port,url,user,password): error=False tomcatUrl = "http://"+host+":"+str(port)+url try: pwdManager = urlconnection.HTTPPasswordMgrWithDefaultRealm() pwdManager.add_password(None,tomcatUrl,user,password) authHandler = urlconnection.HTTPBasicAuthHandler(pwdManager) opener=urlconnection.build_opener(authHandler) urlconnection.install_opener(opener) req = urlconnection.Request(tomcatUrl) handle = urlconnection.urlopen(req, None) data = handle.read() except HTTPError as e: if(e.code==401): data="ERROR: Unauthorized user. Does not have permissions. %s" %(e) elif(e.code==403): data="ERROR: Forbidden, yours credentials are not correct. %s" %(e) else: data="ERROR: The server couldn\'t fulfill the request. %s" %(e) error=True except URLError as e: data = 'ERROR: We failed to reach a server. Reason: %s' %(e.reason) error = True except socket.timeout as e: data = 'ERROR: Timeout error' error = True except socket.error as e: data = "ERROR: Unable to connect with host "+self.host+":"+self.port error = True except: data = "ERROR: Unexpected error: %s"%(sys.exc_info()[0]) error = True return data,error
def urllib_auth(): p = req.HTTPPasswordMgrWithDefaultRealm() p.add_password(None, uri=common.INDEX_URL, user=(common.INDEX_URL + '\\' + common.USER_NAME), passwd=common.USER_PASS) handler = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(p) opener = req.build_opener(handler) req.install_opener(opener) reqData = req.Request(common.INDEX_URL, None, common.HEADER) x = req.urlopen(reqData, context=context) # # print(x.read().decode('utf8')) # # print(x.info()) pCookie = x.info().get('Set-Cookie') apCookie = pCookie[:pCookie.find(';')] # task url common.HEADER['Cookie'] = apCookie reqData = req.Request(common.TASK_URL, headers=common.HEADER) res = req.urlopen(reqData) pCookie = res.info().get('Set-Cookie') common.HEADER['Cookie'] = pCookie[:pCookie.find(';')] reqData = req.Request(common.AUTH_URL, headers=common.HEADER) res = req.urlopen(reqData) print(res.read().decode('utf8'))
def __init__(self, username='', password='', login_url='', auth_type='digest', rets_version='RETS/1.7.2', user_agent='RETSDK/1.0'): """ Sets up a connection to a RETS server and loads account options """ self.headers = {'User-Agent': user_agent, 'RETS-Version': rets_version} # Get a base URL from the login URL parsed_url = urlparse(login_url) if parsed_url.scheme and parsed_url.netloc: base_url = parsed_url.scheme + "://" + parsed_url.netloc else: url_msg = "{0} is not a valid RETS Login URL".format(login_url) raise AuthenticationError(url_msg) # Setup an opener that can handle authentication pw_mgr = request.HTTPPasswordMgrWithDefaultRealm() pw_mgr.add_password(None, base_url, username, password) if auth_type == 'digest': handler = request.HTTPDigestAuthHandler(pw_mgr) elif auth_type == 'basic': handler = request.HTTPBasicAuthHandler(pw_mgr) else: raise AuthenticationError("auth_type must be 'basic' or 'digest'") opener = request.build_opener(handler) request.install_opener(opener) # Perform a login request to get server & account info login_response = self.__login(login_url) for option in login_response['rows']: for key, val in option.items(): if key == 'MetadataVersion': self.metadata_version = val if key == 'MetadataTimestamp': self.metadata_timestamp = val if key == 'MinMetadataTimestamp': self.min_metadata_timestamp = val if key == 'Login': self.login_url = val if key == 'Logout': self.logout_url = val if key == 'Search': self.search_url = val if key == 'GetMetadata': self.get_metadata_url = val if key == 'GetObject': self.get_object_url = val if key == 'Update': self.update_url = val if key == 'PostObject': self.post_object_url = val
def call_api(self, path): url = "{0}://{1}:{2}/api/{3}".format(self.protocol, self.host_name, self.port, path) password_mgr = request.HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(None, url, self.user_name, self.password) handler = request.HTTPBasicAuthHandler(password_mgr) logging.debug("Issue a rabbit API call to get data on" + path) return json.loads(request.build_opener(handler).open(url).read())
def setup_urllib(self): """ Setup urllib """ mgr = httprequest.HTTPPasswordMgrWithDefaultRealm() mgr.add_password(None, self.es_server, self.es_auth, self.es_password) authhandler = httprequest.HTTPBasicAuthHandler(mgr) opener = httprequest.build_opener(authhandler) httprequest.install_opener(opener) return httprequest
def request(*args, **kwargs): "Return the json response from a url, accessed by basic authentication." mgr = req.HTTPPasswordMgrWithDefaultRealm() mgr.add_password(None, urlbase, 'user1', 'abc') opener = req.build_opener(req.HTTPBasicAuthHandler(mgr)) headers = {'Content-Type': 'application/json'} r = req.Request(urlbase + args[0], *args[1:], **kwargs, headers=headers) return json.loads(opener.open(r).read().decode('utf8'))
def make_url_opener(self, query, user, password): '''метод нужен для настройки аутентификации сервера TempoReale''' psw_mgr = request.HTTPPasswordMgrWithDefaultRealm() psw_mgr.add_password(None, query, user, password) auth_handler = request.HTTPBasicAuthHandler(psw_mgr) opener = request.build_opener(auth_handler) return opener
def handler_version(url): defAuth = request.HTTPPasswordMgrWithDefaultRealm() defAuth.add_password(None, 'www.huposedeyue.cn', login, pwd) checkAuth = request.HTTPBasicAuthHandler(defAuth) cookies = request.Request.add_header('') opener = request.build_opener(checkAuth) request.install_opener(opener) return url
def _getHTTPDigestAuthOpener(url, user, pwd): ''' returns an opener, capable of handling http-digest authentification ''' passwdmngr = urllib2.HTTPPasswordMgrWithDefaultRealm() passwdmngr.add_password('realm', url, user, pwd) auth_handler = urllib2.HTTPDigestAuthHandler(passwdmngr) return auth_handler