def getUrl(url, cookieJar=None, post=None, timeout=20, headers=None, noredir=False): cookie_handler = HTTPCookieProcessor(cookieJar) if noredir: opener = build_opener(NoRedirection, cookie_handler, HTTPBasicAuthHandler(), HTTPHandler()) else: opener = build_opener(cookie_handler, HTTPBasicAuthHandler(), HTTPHandler()) # opener = install_opener(opener) req = Request(url) req.add_header( 'User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36' ) if headers: for h, hv in headers: req.add_header(h, hv) response = opener.open(req, post, timeout=timeout) link = response.read() response.close() return link
def __init__(self, host, port=8082, user='', password=''): if user and password: auth = HTTPBasicAuthHandler() auth.add_password('Django', '%s:%s' % (host, port), user, password) self.__opener(auth) else: self.__opener = build_opener()
def request(cls, url, start, end, realm='Webtrends Basic Authentication'): """Make an authed request to the webtrends API. Make one attempt to fetch and reload the data. If something fails, it's the caller's responsibility to retry. """ # If start and/or end are date or datetime, convert to string. if isinstance(start, (date, datetime)): start = start.strftime('%Ym%md%d') if isinstance(end, (date, datetime)): end = end.strftime('%Ym%md%d') auth_handler = HTTPBasicAuthHandler() auth_handler.add_password(realm=realm, uri=url, user=settings.WEBTRENDS_USER, passwd=settings.WEBTRENDS_PASSWORD) opener = build_opener(auth_handler) url = urlparams(url, start_period=start, end_period=end) try: # TODO: A wrong username or password results in a recursion depth # error. return opener.open(url).read() except IOError, e: raise StatsIOError(*e.args)
def __init__(self, url, pysparkle): self.url = url self.pysparkle = pysparkle auth_user = pysparkle.config.get('auth_user') if auth_user is not None: auth_pass = pysparkle.config.get('auth_password', '') auth_handler = HTTPBasicAuthHandler() auth_handler.add_password(None, url, auth_user, auth_pass) opener = build_opener(auth_handler) install_opener(opener)
def connect(self): """Get a handle to a remote connection.""" auth_handler = HTTPBasicAuthHandler() auth_handler.add_password(realm=self.url, uri=self.url, user=self._user_name, passwd=self._password) opener = build_opener(auth_handler) install_opener(opener) return self
def get_handler(url): hdlr = HTTPBasicAuthHandler() hdlr.add_password("Archives", urlparse(url)[1], LOGIN, PASSWD) opener = build_opener(hdlr) install_opener(opener) req = Request(url) b64str = encodestring('%s:%s' % (LOGIN, PASSWD))[:-1] req.add_header('Authorization', 'Basic %s' % b64str) return req
def get_handler(url): hdlr = HTTPBasicAuthHandler() hdlr.add_password("Archives",urlparse(url)[1], LOGIN,PASSWD) opener = build_opener(hdlr) install_opener(opener) req = Request(url) b64str = encodestring('%s:%s' % (LOGIN,PASSWD))[:-1] req.add_header('Authorization','Basic %s' % b64str) return req
def _add_basic_auth(self): auth_handler = HTTPBasicAuthHandler(HTTPPasswordMgrWithDefaultRealm()) auth_handler.add_password( realm=None, uri=self.host, user=self.user, passwd=self.password, ) install_opener( build_opener( auth_handler, HTTPHandler(debuglevel=self.request_debug_level), HTTPSHandler(debuglevel=self.request_debug_level), ))
def _add_basic_auth(self): auth_handler = HTTPBasicAuthHandler( HTTPPasswordMgrWithDefaultRealm() ) auth_handler.add_password( realm=None, uri=self.host, user=self.user, passwd=self.password, ) install_opener(build_opener( auth_handler, HTTPHandler(debuglevel=self.request_debug_level), HTTPSHandler(debuglevel=self.request_debug_level), ))
def _setup_opener(self): password_manager = HTTPPasswordMgrWithDefaultRealm() password_manager.add_password(None, self.endpoint, self.username, self.password) auth_manager = HTTPBasicAuthHandler(password_manager) opener = build_opener(auth_manager) install_opener(opener)
def requestServerData(url, webuser = None, webpass = None): if TLS_V1_NEG_ERROR: import httplib httplib.HTTPSConnection.connect = httpsConnectReplacment from urllib2 import (HTTPPasswordMgr, HTTPBasicAuthHandler, build_opener, install_opener, urlopen, HTTPError) password_mgr = HTTPPasswordMgr() #WithDefaultRealm() password_mgr.add_password("Server Admin", url, webuser, webpass) handler = HTTPBasicAuthHandler(password_mgr) opener = build_opener(handler) install_opener(opener) request = urllib2.Request(url) if webuser: base64string = base64.encodestring('%s:%s' % (webuser, webpass))[:-1] request.add_header("Authorization", "Basic %s" % base64string) request.add_header('WWW-Authenticate', 'Basic realm="Server Admin"') try: htmlFile = urllib2.urlopen(request) #, timeout=30) htmlData = htmlFile.read() htmlFile.close() # This bit identifies if it's leopard which adds extra unneeded info as a header if re.match("SupportsBinaryPlist", htmlData): xmlDump = re.split("\r\n\r\n", htmlData, 1) return 0, xmlDump[1] else: return 0, htmlData except: return 1, sys.exc_info()[1]
def _createUser(self, number): record = self._records[number] user = record.uid authBasic = HTTPBasicAuthHandler(password_mgr=HTTPPasswordMgrWithDefaultRealm()) authBasic.add_password( realm=None, uri=self.servers[record.podID]["uri"], user=user.encode('utf-8'), passwd=record.password.encode('utf-8')) authDigest = HTTPDigestAuthHandler(passwd=HTTPPasswordMgrWithDefaultRealm()) authDigest.add_password( realm=None, uri=self.servers[record.podID]["uri"], user=user.encode('utf-8'), passwd=record.password.encode('utf-8')) return record, user, {"basic": authBasic, "digest": authDigest, }
def send_message(self, message): headers = { 'Content-Type': 'application/soap+xml;charset=UTF-8', 'Content-Length': len(message), 'User-Agent': 'Python WinRM client' } password_manager = HTTPPasswordMgrWithDefaultRealm() password_manager.add_password(None, self.endpoint, self.username, self.password) auth_manager = HTTPBasicAuthHandler(password_manager) opener = build_opener(auth_manager) install_opener(opener) request = Request(self.endpoint, data=message, headers=headers) try: response = urlopen(request, timeout=self.timeout) # Version 1.1 of WinRM adds the namespaces in the document instead of the envelope so we have to # add them ourselves here. This should have no affect version 2. response_text = response.read() return response_text #doc = ElementTree.fromstring(response.read()) #Ruby #doc = Nokogiri::XML(resp.http_body.content) #doc.collect_namespaces.each_pair do |k,v| # doc.root.add_namespace((k.split(/:/).last),v) unless doc.namespaces.has_key?(k) #end #return doc #return doc except HTTPError as ex: error_message = 'Bad HTTP response returned from server. Code {0}'.format( ex.code) if ex.msg: error_message += ', {0}'.format(ex.msg) raise WinRMTransportError(error_message) except URLError as ex: raise WinRMTransportError(ex.reason)
def check_password(self, username, password): self.log.debug("Trac.ini authentication_url = '%s'" % self.auth_url) # Nothing to do, if URL is absolute. if self.auth_url.startswith('http://') or \ self.auth_url.startswith('https://'): authUrl = self.auth_url # Handle server-relative URLs. elif self.auth_url.startswith('/'): # Prepend the Trac server component. pr = urlparse(self.env.abs_href()) href = Href(pr[0] + '://' + pr[1]) authUrl = href(self.auth_url) elif '/' in self.auth_url: # URLs with path like 'common/authFile' or 'site/authFile'. authUrl = self.env.abs_href.chrome(self.auth_url) else: # Bare file name option value like 'authFile'. authUrl = self.env.abs_href.chrome('common', self.auth_url) self.log.debug("Final auth_url = '%s'" % authUrl) acctmgr = HTTPPasswordMgrWithDefaultRealm() acctmgr.add_password(None, authUrl, username, password) try: build_opener(HTTPBasicAuthHandler(acctmgr), HTTPDigestAuthHandler(acctmgr)).open(authUrl) except IOError, e: if hasattr(e, 'code') and e.code == 404: self.log.debug("""HttpAuthStore page not found; we are authenticated nonetheless""") return True if hasattr(e, 'code') and e.code == 401: self.log.debug("HttpAuthStore authentication failed") return None
def _createUser(self, number): record = self._records[number] user = record.uid authBasic = HTTPBasicAuthHandler() authBasic.add_password( realm="Test Realm", uri=self.server, user=user.encode('utf-8'), passwd=record.password.encode('utf-8')) authDigest = HTTPDigestAuthHandler() authDigest.add_password( realm="Test Realm", uri=self.server, user=user.encode('utf-8'), passwd=record.password.encode('utf-8')) return user, {"basic": authBasic, "digest": authDigest, }
def __init__(self, uri, cookiejar=None, use_datetime=0): Transport.__init__(self, use_datetime=use_datetime) self.opener = build_opener() # Parse auth (user:passwd) from the uri urltype, rest = splittype(uri) host, rest = splithost(rest) auth, host = splituser(host) self.uri = urltype + '://' + host + rest # Handle HTTP Basic authentication if auth is not None: user, passwd = splitpasswd(auth) passwdmgr = HTTPPasswordMgrWithDefaultRealm() passwdmgr.add_password(realm=None, uri=self.uri, user=user, passwd=passwd) authhandler = HTTPBasicAuthHandler(passwdmgr) self.opener.add_handler(authhandler) # Handle HTTP Cookies if cookiejar is not None: self.opener.add_handler(HTTPCookieProcessor(cookiejar))
def open(self, **params): self._base_url = 'http://{}/outs.cgi?out'.format(params['ip']) password_manager = HTTPPasswordMgrWithDefaultRealm() password_manager.add_password(None, self._base_url, params['user'], params['pass']) authentication_handler = HTTPBasicAuthHandler(password_manager) self._opener = build_opener(authentication_handler)
def auth_Connection(url): username = "******" password = "******" passman = HTTPPasswordMgrWithDefaultRealm() # creating a password manager passman.add_password(None, url, username, password) authhandler = HTTPBasicAuthHandler(passman) opener = build_opener(authhandler) install_opener(opener)
def openURL(url_base, data, method='Get', cookies=None, username=None, password=None, timeout=30): ''' function to open urls - wrapper around urllib2.urlopen but with additional checks for OGC service exceptions and url formatting, also handles cookies and simple user password authentication''' url_base.strip() lastchar = url_base[-1] if lastchar not in ['?', '&']: if url_base.find('?') == -1: url_base = url_base + '?' else: url_base = url_base + '&' if username and password: # Provide login information in order to use the WMS server # Create an OpenerDirector with support for Basic HTTP # Authentication... passman = HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, url_base, username, password) auth_handler = HTTPBasicAuthHandler(passman) opener = urllib2.build_opener(auth_handler) openit = opener.open else: # NOTE: optionally set debuglevel>0 to debug HTTP connection #opener = urllib2.build_opener(urllib2.HTTPHandler(debuglevel=0)) #openit = opener.open openit = urlopen try: if method == 'Post': req = Request(url_base, data) # set appropriate header if posting XML try: xml = etree.fromstring(data) req.add_header('Content-Type', "text/xml") except: pass else: req=Request(url_base + data) if cookies is not None: req.add_header('Cookie', cookies) u = openit(req, timeout=timeout) except HTTPError as e: #Some servers may set the http header to 400 if returning an OGC service exception or 401 if unauthorised. if e.code in [400, 401]: raise ServiceException(e.read()) else: raise e # check for service exceptions without the http header set if 'Content-Type' in u.info() and u.info()['Content-Type'] in ['text/xml', 'application/xml']: #just in case 400 headers were not set, going to have to read the xml to see if it's an exception report. #wrap the url stram in a extended StringIO object so it's re-readable u=RereadableURL(u) se_xml= u.read() se_tree = etree.fromstring(se_xml) serviceException=se_tree.find('{http://www.opengis.net/ows}Exception') if serviceException is None: serviceException=se_tree.find('ServiceException') if serviceException is not None: raise ServiceException(str(serviceException.text).strip()) u.seek(0) #return cursor to start of u return u
def set_authentication(self, user, password): super(HTTPResource, self).set_authentication(user, password) if self.url.startswith("http"): if not HTTPResource.password_manager: HTTPResource.password_manager = HTTPPasswordMgrWithDefaultRealm() auth_handler = HTTPBasicAuthHandler(HTTPResource.password_manager) opener = build_opener(auth_handler) install_opener(opener) HTTPResource.password_manager.add_password(None, self.url, user, password)
def check_password(self, user, password): mgr = HTTPPasswordMgrWithDefaultRealm() mgr.add_password(None, self.auth_url, user, password) try: build_opener(HTTPBasicAuthHandler(mgr), HTTPDigestAuthHandler(mgr)).open(self.auth_url) except IOError: return False else: return True
def u2handlers(self): handlers = [] handlers.append(ProxyHandler(self.proxy)) handlers.append(HTTPBasicAuthHandler(self.pm)) # python ssl Context support - PEP 0466 if hasattr(ssl, '_create_unverified_context'): ssl_context = ssl._create_unverified_context() handlers.append(HTTPSHandler(context=ssl_context)) else: handlers.append(HTTPSHandler()) return handlers
def _install_opener(self): base_url = self.build_url('') password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm() password_manager.add_password(None, base_url, self.username, self.password) # only use the first Auth_handler to avoid the busted NTLM handler self.opener = urllib2.build_opener( HTTPBasicAuthHandler(password_manager)) if self.use_password_as_token: self.opener.addheaders.append( ('Authorization', 'Bearer ' + self.password)) self.opener.add_handler(HTTPCookieProcessor())
def json_for(cls, period): """Return the JSON-formatted WebTrends stats for the given period. Make one attempt to fetch and reload the data. If something fails, it's the caller's responsibility to retry. """ auth_handler = HTTPBasicAuthHandler() auth_handler.add_password(realm=settings.WEBTRENDS_REALM, uri=settings.WEBTRENDS_WIKI_REPORT_URL, user=settings.WEBTRENDS_USER, passwd=settings.WEBTRENDS_PASSWORD) opener = build_opener(auth_handler) start, end = period_dates()[period] url = (settings.WEBTRENDS_WIKI_REPORT_URL + '&start_period=%s&end_period=%s' % (start, end)) try: # TODO: A wrong username or password results in a recursion depth # error. return opener.open(url).read() except IOError, e: raise StatsIOError(*e.args)
def curl(self, url, params=None, auth=None, req_type="GET", data=None, headers=None): post_req = ["POST", "PUT"] get_req = ["GET", "DELETE"] if params is not None: url += "?" + urlencode(params) if req_type not in post_req + get_req: raise IOError("Wrong request type \"%s\" passed" % req_type) _headers = {} handler_chain = [] if auth is not None: manager = HTTPPasswordMgrWithDefaultRealm() manager.add_password(None, url, auth["user"], auth["pass"]) handler_chain.append(HTTPBasicAuthHandler(manager)) if req_type in post_req and data is not None: _headers["Content-Length"] = len(data) if headers is not None: _headers.update(headers) director = build_opener(*handler_chain) if req_type in post_req: if sys.version_info.major == 3: _data = bytes(data, encoding='utf8') else: _data = bytes(data) req = Request(url, headers=_headers, data=_data) else: req = Request(url, headers=_headers) req.get_method = lambda: req_type result = director.open(req) return { "httpcode": result.code, "headers": result.info(), "content": result.read() }
def _set_proxy(self): """ Set an URL opener for the current storage. """ from urllib2 import HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler, build_opener, install_opener if self.login: passman = HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, self.address, self.login, self.password) authhandler = HTTPBasicAuthHandler(passman) self.proxy = build_opener(authhandler) else: self.proxy = build_opener() install_opener(self.proxy)
def openURL(url_base, data, method='Get', cookies=None, username=None, password=None, timeout=30): ''' function to open urls - wrapper around urllib2.urlopen but with additional checks for OGC service exceptions and url formatting, also handles cookies and simple user password authentication''' url_base.strip() lastchar = url_base[-1] if lastchar not in ['?', '&']: if url_base.find('?') == -1: url_base = url_base + '?' else: url_base = url_base + '&' if username and password: # Provide login information in order to use the WMS server # Create an OpenerDirector with support for Basic HTTP # Authentication... passman = HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, url_base, username, password) auth_handler = HTTPBasicAuthHandler(passman) opener = urllib2.build_opener(auth_handler) openit = opener.open else: # NOTE: optionally set debuglevel>0 to debug HTTP connection #opener = urllib2.build_opener(urllib2.HTTPHandler(debuglevel=0)) #openit = opener.open openit = urlopen try: if method == 'Post': req = Request(url_base, data) # set appropriate header if posting XML try: xml = etree.fromstring(data) req.add_header('Content-Type', "text/xml") except: pass else: req = Request(url_base + data) if cookies is not None: req.add_header('Cookie', cookies) u = openit(req, timeout=timeout) except HTTPError, e: #Some servers may set the http header to 400 if returning an OGC service exception or 401 if unauthorised. if e.code in [400, 401]: raise ServiceException, e.read() else: raise e
def callservice(url, method='post', basicauth=None, **kargs): from urllib import urlencode from urllib2 import (urlopen, build_opener, install_opener, HTTPError, HTTPBasicAuthHandler) params = '' if kargs: # urlencode does not accept non-ascii data for k, v in kargs.iteritems(): if isinstance(v, unicode): kargs[k] = v.encode('utf-8') params = urlencode(kargs) if not _testing: # If we need to authenticate if basicauth is not None: # Get host from url host = url[url.find('//') + 2:] for c in (':', '/', '?', '&'): if c in host: host = host[:host.find(c)] # Install authentication handler auth_handler = HTTPBasicAuthHandler() auth_handler.add_password(basicauth['realm'], host, basicauth['user'], basicauth['passwd']) install_opener(build_opener(auth_handler)) try: if method == 'post': u = urlopen(url, data=params) elif method == 'get': if params: url = '%s?%s' % (url, params) u = urlopen(url) else: raise ValueError('Unknown method: %s' % method) except HTTPError, e: raise ValueError(e.read()) resp = u.read() return resp
def _setup_http(self): self._api_uri = '{http}://{host}:{port}{uri}'.format( http=self._proto, host=self._hostname, port=self._port, uri=DEFAULT_API_URI) auth_mgr = HTTPPasswordMgrWithDefaultRealm() auth_mgr.add_password(None, self._api_uri, self._user, self._password) auth_hndlr = HTTPBasicAuthHandler(auth_mgr) http_hndlr = HTTPHandler(debuglevel=0) https_hndlr = HTTPSHandler(debuglevel=0) self._http_api = build_opener(auth_hndlr, https_hndlr, http_hndlr) self._auth_base64 = base64.encodestring( '%s:%s' % (self._user, self._password))[:-1]
def __init__(self, base_location=None, api_key=None, is_verbose=False, http_user=None, http_pass=None): if base_location is not None: self.base_location = base_location self.api_key = api_key self.is_verbose = is_verbose if http_user and http_pass: password_mgr = HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(None, base_location, http_user, http_pass) handler = HTTPBasicAuthHandler(password_mgr) opener = build_opener(handler) install_opener(opener)
def curl(url, params=None, auth=None, req_type='GET', data=None, headers=None): """Provides HTTP interaction like curl.""" post_req = ['POST', 'PUT'] get_req = ['GET', 'DELETE'] if params is not None: url += '?' + urlencode(params) if req_type not in post_req + get_req: raise IOError('Wrong request type "%s" passed' % req_type) _headers = {} handler_chain = [] if auth is not None: manager = HTTPPasswordMgrWithDefaultRealm() manager.add_password(None, url, auth['user'], auth['pass']) handler_chain.append(HTTPBasicAuthHandler(manager)) if req_type in post_req and data is not None: _headers['Content-Length'] = len(data) if headers is not None: _headers.update(headers) director = build_opener(*handler_chain) if req_type in post_req: if sys.version_info.major < 3: _data = bytes(data) else: _data = bytes(data, encoding='utf8') req = Request(url, headers=_headers, data=_data) else: req = Request(url, headers=_headers) req.get_method = lambda: req_type result = director.open(req) return { 'httpcode': result.code, 'headers': result.info(), 'content': result.read().decode('utf-8') }
def _build_opener(self, params, base_url): handlers = poster.streaminghttp.get_handlers() user = params.get('user', '') if user: realm = params.get('realm', '') password = params.get('password', '') scheme = params.get('scheme', 'digest') password_mgr = HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(realm, base_url, user, password) if scheme == 'basic': handler = HTTPBasicAuthHandler(password_mgr) elif scheme == 'digest': handler = HTTPDigestAuthHandler(password_mgr) else: raise ValueError('Unknown auth type "%s"' % scheme) handlers = handlers + [handler] return build_opener(*handlers)
def __init__(self, pUser, pPassword, pHost=None, pApiVer=1): # If provided a hostname, call base class with it. Otherwise, use # 'default' hostname defined in QGConnector constructor. if not pHost: QGConnector.__init__(self, pApiVer) else: QGConnector.__init__(self, pApiVer, pHost) # Setup password manager and HTTPBasicAuthHandler self._passman = HTTPPasswordMgrWithDefaultRealm() self._passman.add_password(None, self.apiURI(), pUser, pPassword) self._opener = urllib2.build_opener(HTTPBasicAuthHandler( self._passman)) # Store base64 encoded username & password for API v2. self._base64string = base64.encodestring( '%s:%s' % (pUser, pPassword)).replace('\n', '')
def get_wsdls(hostname, username='******', password='******', verify=False, timeout=90, port=443): """Returns the set of all available WSDLs on this server Used for providing introspection into the available namespaces and WSDLs dynamically (e.g. when using iPython) @param hostname: The IP address or hostname of the BIGIP. @param username: The admin username on the BIGIP. @param password: The admin password on the BIGIP. @param verify: When True, performs SSL certificate validation in Python / urllib2 versions that support it (v2.7.9 and newer) @param timeout: The time to wait (in seconds) before timing out the connection to the URL """ url = 'https://%s:%s/iControl/iControlPortal.cgi' % (hostname, port) regex = re.compile(r'/iControl/iControlPortal.cgi\?WSDL=([^"]+)"') auth_handler = HTTPBasicAuthHandler() # 10.1.0 has a realm of "BIG-IP" auth_handler.add_password(uri='https://%s:%s/' % (hostname, port), user=username, passwd=password, realm="BIG-IP") # 11.3.0 has a realm of "BIG-\IP". I'm not sure exactly when it changed. auth_handler.add_password(uri='https://%s:%s/' % (hostname, port), user=username, passwd=password, realm="BIG\-IP") if verify: opener = build_opener(auth_handler) else: opener = build_opener(auth_handler, HTTPSHandlerNoVerify) try: result = opener.open(url, timeout=timeout) except URLError as e: raise ConnectionError(str(e)) wsdls = {} for line in result.readlines(): result = regex.search(line.decode()) if result: namespace, rest = result.groups()[0].split(".", 1) if namespace not in wsdls: wsdls[namespace] = [] wsdls[namespace].append(rest) return wsdls
def run_dashboard(dashboard_name): xsl_filename = dashboard_name + "_report.xsl" feed_url = "http://" + DTSERVER + "/rest/management/reports/create/" + dashboard_name + "?type=XML&format=XML+Export&filter=tf:" + TIMEFRAME abspath = os.path.abspath(__file__) dname = os.path.dirname(abspath) os.chdir(dname) # Set up a HTTPS request with username/password authentication try: # create a password manager password_mgr = HTTPPasswordMgrWithDefaultRealm() # Add the username and password. password_mgr.add_password(None, feed_url, USERNAME, PASSWORD) opener = build_opener(HTTPBasicAuthHandler(password_mgr)) file = opener.open(feed_url) except URLError, e: print 'URLError: "%s"' % e raise
def query(self, query, ts_start, ts_end): # target = 'summarize({},"{}","avg")'.format( # query, '99year') @TODO remove if not needed # build graphite url args = { '__auth_token': self.token, 'target': query, 'format': 'json', 'from': ts_start, 'until': ts_end, } url = '{}/render?'.format(self.url) for k, v in args.iteritems(): print k print v url += '{}={}&'.format(quote(k), quote(v)) logger.debug('Query URL is {}'.format(url)) # Basic auth header password_mgr = HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password( None, self.graphite_url, self.username, self.password, ) auth_handler = HTTPBasicAuthHandler(password_mgr) # Ignore ssl cert check ctx = create_default_context() ctx.check_hostname = False ctx.verify_mode = CERT_NONE ssl_handler = HTTPSHandler(context=ctx) opener = build_opener(ssl_handler, auth_handler) install_opener(opener) result = json.loads(urlopen(url).read()) return result
def get(self, uri, params={}, headers={}, with_status_code=False, timeout=10, user=None, password=None): data = None # always none in GET if params: uri = "%s?%s" % (uri, urlencode(params)) # SSL, user/password and basic # NOTE: currently don't manage ssl & user/password if uri.startswith('https://'): handler = HTTPSHandler(context=self.ssl_context) elif user and password: passwordMgr = HTTPPasswordMgrWithDefaultRealm() passwordMgr.add_password(None, uri, user, password) handler = HTTPBasicAuthHandler(passwordMgr) else: handler = HTTPHandler url_opener = build_opener(handler) req = Request(uri, data) req.get_method = lambda: 'GET' for (k, v) in headers.items(): req.add_header(k, v) request = url_opener.open(req, timeout=timeout) response = request.read() status_code = request.code request.close() if not with_status_code: return response else: return (status_code, response)
def open_url(url, config, data=None, handlers=None): """Attempts to open a connection to a specified URL. @param url: URL to attempt to open @param config: SSL context configuration @type config: Configuration @param data: HTTP POST data @type data: str @param handlers: list of custom urllib2 handlers to add to the request @type handlers: iterable @return: tuple ( returned HTTP status code or 0 if an error occurred returned message or error description response object) """ debuglevel = 1 if config.debug else 0 # Set up handlers for URL opener. if config.cookie: cj = config.cookie else: cj = cookielib.CookieJar() # Use a cookie processor that accumulates cookies when redirects occur so # that an application can redirect for authentication and retain both any # cookies for the application and the security system (c.f., # urllib2.HTTPCookieProcessor which replaces cookies). cookie_handler = AccumulatingHTTPCookieProcessor(cj) if not handlers: handlers = [] handlers.append(cookie_handler) if config.debug: http_handler = HTTPHandler(debuglevel=debuglevel) https_handler = HTTPSContextHandler(config.ssl_context, debuglevel=debuglevel) handlers.extend([http_handler, https_handler]) if config.http_basicauth: # currently only supports http basic auth auth_handler = HTTPBasicAuthHandler(HTTPPasswordMgrWithDefaultRealm()) auth_handler.add_password(realm=None, uri=url, user=config.httpauth[0], passwd=config.httpauth[1]) handlers.append(auth_handler) # Explicitly remove proxy handling if the host is one listed in the value of # the no_proxy environment variable because urllib2 does use proxy settings # set via http_proxy and https_proxy, but does not take the no_proxy value # into account. if not _should_use_proxy(url, config.no_proxy): handlers.append(urllib2.ProxyHandler({})) log.debug("Not using proxy") elif config.proxies: handlers.append(urllib2.ProxyHandler(config.proxies)) log.debug("Configuring proxies: %s" % config.proxies) opener = build_opener(*handlers, ssl_context=config.ssl_context) headers = config.headers if headers is None: headers = {} request = urllib2.Request(url, data, headers) # Open the URL and check the response. return_code = 0 return_message = '' response = None try: response = opener.open(request) return_message = response.msg return_code = response.code if log.isEnabledFor(logging.DEBUG): for index, cookie in enumerate(cj): log.debug("%s : %s", index, cookie) except urllib2.HTTPError, exc: return_code = exc.code return_message = "Error: %s" % exc.msg if log.isEnabledFor(logging.DEBUG): log.debug("%s %s", exc.code, exc.msg)
def __init__(self, credentialmanager=None, cookiejar=None, **kwargs): HTTPBasicAuthHandler.__init__(self) ShibbolethHandler.__init__(self, cookiejar=cookiejar) self.credentialmanager = credentialmanager self.__req = None self.__headers = None
def __init__(self, password_mgr): HTTPBasicAuthHandler.__init__(self, password_mgr) self.authContext = set([])
def _post_url(self, event, url=None): "Posts a URL to delicious.com" title = self._get_title(url) con_re = re.compile(r'!n=|!') connection_body = con_re.split(event.sender['connection']) if len(connection_body) == 1: connection_body.append(event.sender['connection']) ip_re = re.compile(r'\.IP$|unaffiliated') if ip_re.search(connection_body[1]) != None: connection_body[1] = '' if ibid.sources[event.source].type == 'jabber': obfusc_conn = '' obfusc_chan = event.channel.replace('@', '^') else: at_re = re.compile(r'@\S+?\.') obfusc_conn = at_re.sub('^', connection_body[1]) obfusc_chan = at_re.sub('^', event.channel) tags = u' '.join((event.sender['nick'], obfusc_conn, obfusc_chan, event.source)) data = { 'url' : url.encode('utf-8'), 'description' : title.encode('utf-8'), 'tags' : tags.encode('utf-8'), 'replace' : 'yes', 'dt' : event.time.strftime('%Y-%m-%dT%H:%M:%SZ'), 'extended' : event.message['raw'].encode('utf-8'), } if self.service.lower() == 'delicious': service = ('del.icio.us API', 'https://api.del.icio.us') elif self.service.lower() == 'faves': service = ('Faves', 'https://secure.faves.com') else: log.error(u'Unknown social bookmarking service: %s', self.service) return auth_handler = HTTPBasicAuthHandler() auth_handler.add_password(service[0], service[1], self.username, self.password) opener = build_opener(auth_handler) posturl = service[1] + '/v1/posts/add?' + urlencode(data) try: resp = opener.open(posturl).read() if 'done' in resp: log.debug(u"Posted url '%s' to %s, posted in %s on %s " u"by %s/%i (%s)", url, self.service, event.channel, event.source, event.account, event.identity, event.sender['connection']) else: log.error(u"Error posting url '%s' to %s: %s", url, self.service, resp) except HTTPError, e: if e.code == 401: log.error(u"Incorrect password for %s, couldn't post", self.service)