Example #1
0
 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)
Example #2
0
    def get(self, url, username=None, password=None, **kwargs):
        """ Makes a get request to the URL specified."""

        headers = None
        if kwargs:
            if 'headers' in kwargs:
                headers = kwargs['headers']
                del (kwargs['headers'])
                self.logger.debug('Headers passed in:%s' % headers)
            if url.find('?') >= 0:
                url = url + '&' + urlencode(kwargs)
            else:
                url = url + '?' + urlencode(kwargs)

        self.logger.debug('About to do a GET on:' + url)

        request = RESTRequest(url, method='GET')

        # add a user-agent
        request.add_header('User-Agent', self.user_agent)
        if headers:
            for k, v in headers.items():
                self.logger.debug('Adding header:%s:%s' % (k, v))
                request.add_header(k, v)

        # create a password manager
        passwordManager = HTTPPasswordMgrWithDefaultRealm()
        passwordManager.add_password(None, url, username, password)

        opener = build_opener(SmartRedirectHandler(), DefaultErrorHandler(),
                              ContextualBasicAuthHandler(passwordManager))

        return opener.open(request)
Example #3
0
 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 __init__(self,link,username,password):
     self.url = link
     ps_mgr = HTTPPasswordMgrWithDefaultRealm()
     ps_mgr.add_password(None, self.url, username, password)
     handler = HTTPBasicAuthHandler(ps_mgr)
     opener = build_opener(handler)
     install_opener(opener)
Example #5
0
    def put(self,
            url,
            payload,
            contentType,
            username=None,
            password=None,
            **kwargs):
        """
        Makes a PUT request to the URL specified and includes the payload
        that gets passed in. The content type header gets set to the
        specified content type.
        """

        if kwargs:
            if url.find('?') >= 0:
                url = url + '&' + urlencode(kwargs)
            else:
                url = url + '?' + urlencode(kwargs)

        request = RESTRequest(url, payload, method='PUT')

        # set the content type header
        request.add_header('Content-Type', contentType)

        # add a user-agent
        request.add_header('User-Agent', self.user_agent)
        # create a password manager
        passwordManager = HTTPPasswordMgrWithDefaultRealm()
        passwordManager.add_password(None, url, username, password)

        opener = build_opener(SmartRedirectHandler(), DefaultErrorHandler(),
                              ContextualBasicAuthHandler(passwordManager))

        return opener.open(request)
Example #6
0
    def delete(self, url, username=None, password=None, **kwargs):

        """ Makes a delete request to the URL specified. """

        if kwargs:
            if url.find('?') >= 0:
                url = url + '&' + urlencode(kwargs)
            else:
                url = url + '?' + urlencode(kwargs)

        request = RESTRequest(url, method='DELETE')

        # add a user-agent
        request.add_header('User-Agent', self.user_agent)

        # create a password manager
        passwordManager = HTTPPasswordMgrWithDefaultRealm()
        passwordManager.add_password(None, url, username, password)

        opener = build_opener(SmartRedirectHandler(),
                              DefaultErrorHandler(),
                              ContextualBasicAuthHandler(passwordManager))

        #try:
        #    opener.open(request)
        #except urllib2.HTTPError, e:
        #    if e.code is not 204:
        #        raise e
        #return None
        return opener.open(request)
Example #7
0
    def _URLOpenBehindProxyAuthentication(self, request, post_dict):
        '''
        Open the given request providing support for proxy authentication.

        :param Request request:
            The URL request.

        :param str post_dict:
            String with a sequence of two-element tuples or dict encoded for POST requests.
            See urllib.urlencode.

        :rtype: file
        :returns:
            The file containing the url request contents.
        '''
        user, password = self._on_proxy_authentication_request(request)

        password_manager = HTTPPasswordMgrWithDefaultRealm()
        password_manager.add_password(None, self._on_proxy_address_request(), user, password)

        proxy_authentication_handler = ProxyBasicAuthHandler(password_manager)
        opener = urllib2.build_opener(proxy_authentication_handler)

        urllib2.install_opener(opener)
        result = urllib2.urlopen(request, post_dict)

        return result
Example #8
0
    def delete(self, url, username=None, password=None, **kwargs):
        """ Makes a delete request to the URL specified. """

        if kwargs:
            if url.find('?') >= 0:
                url = url + '&' + urlencode(kwargs)
            else:
                url = url + '?' + urlencode(kwargs)

        request = RESTRequest(url, method='DELETE')

        # add a user-agent
        request.add_header('User-Agent', self.user_agent)

        # create a password manager
        passwordManager = HTTPPasswordMgrWithDefaultRealm()
        passwordManager.add_password(None, url, username, password)

        opener = build_opener(SmartRedirectHandler(), DefaultErrorHandler(),
                              ContextualBasicAuthHandler(passwordManager))

        #try:
        #    opener.open(request)
        #except urllib2.HTTPError, e:
        #    if e.code is not 204:
        #        raise e
        #return None
        return opener.open(request)
Example #9
0
class QGAPIConnect(QGConnector):
    """ Qualys Connection class which allows requests to the QualysGuard API
    using HTTP-Basic Authentication (over SSL).
    
    Notes:
    ======
    - Remote certificate verification is not supported.
    - This only currently functions with API v1 (not sure why).
    """
    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', '')
Example #10
0
    def get(self,
            url,
            username=None,
            password=None,
            **kwargs):

        """ Makes a get request to the URL specified."""

        if kwargs:
            if url.find('?') >= 0:
                url = url + '&' + urlencode(kwargs)
            else:
                url = url + '?' + urlencode(kwargs)

        request = RESTRequest(url, method='GET')

        # add a user-agent
        request.add_header('User-Agent', self.user_agent)

        # create a password manager
        passwordManager = HTTPPasswordMgrWithDefaultRealm()
        passwordManager.add_password(None, url, username, password)

        opener = build_opener(SmartRedirectHandler(),
                              DefaultErrorHandler(),
                              ContextualBasicAuthHandler(passwordManager))

        return opener.open(request)
Example #11
0
 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)
Example #12
0
    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))
Example #13
0
 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)
Example #14
0
def openURL(url_base, data, method='Get', cookies=None, username=None, password=None):
    ''' 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:
        openit = urlopen
   
    try:
        if method == 'Post':
            req = Request(url_base, data)
        else:
            req=Request(url_base + data)
        if cookies is not None:
            req.add_header('Cookie', cookies)
        u = openit(req)
    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
Example #15
0
    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
Example #16
0
    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
Example #17
0
    def get(self, date):
        """
        Get data
        """
        from lxml import html
        from urllib2 import HTTPBasicAuthHandler, HTTPPasswordMgrWithDefaultRealm, build_opener, urlopen

        if not self.verify:
            import ssl

            ssl._create_default_https_context = ssl._create_unverified_context

        url = self.get_url(date)
        if self.auth:
            password_mgr = HTTPPasswordMgrWithDefaultRealm()
            password_mgr.add_password(None, url, self.auth["username"], self.auth["password"])

            handler = HTTPBasicAuthHandler(password_mgr)
            opener = build_opener(handler)

            src = opener.open(url).read()
        else:
            src = urlopen(url).read()

        return html.fromstring(src)
Example #18
0
 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)
Example #19
0
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)
Example #20
0
    def __init__(self):
        '''
        Constructor. It delegates construction to the base class
        L{HttxObject} and initializes the member variables
        '''
        HttxObject.__init__(self)

        self.passmanager = HTTPPasswordMgrWithDefaultRealm()
Example #21
0
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
Example #22
0
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
Example #23
0
 def __init__(self, username, password, ip, type=None):
     self.username = username
     self.password = password
     self.ip = ip
     password_mgr = HTTPPasswordMgrWithDefaultRealm()
     handler = HTTPBasicAuthHandler(password_mgr)
     password_mgr.add_password(None, self.ip, self.username, self.password)
     self.opener = build_opener(handler)
     pass
 def _setup_opener(self, **kwargs):
     password_manager = HTTPPasswordMgrWithDefaultRealm()
     password_manager.add_password(None, self.endpoint, self.username,
                                   self.password)
     auth_manager = ForcedBasicAuthHandler(password_manager)
     handlers = [auth_manager]
     root_handler = kwargs.get('root_handler')
     if root_handler:
         handlers.insert(0, root_handler)
     self.opener = build_opener(*handlers)
Example #25
0
 def _setup_opener(self, **kwargs):
     password_manager = HTTPPasswordMgrWithDefaultRealm()
     password_manager.add_password(
         None, self.endpoint, self.username, self.password)
     auth_manager = ForcedBasicAuthHandler(password_manager)
     handlers = [auth_manager]
     root_handler = kwargs.get('root_handler')
     if root_handler:
         handlers.insert(0, root_handler)
     self.opener = build_opener(*handlers)
Example #26
0
 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
Example #27
0
 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
Example #28
0
    def post(self,
             url,
             payload,
             contentType,
             username=None,
             password=None,
             **kwargs):

        """
        Makes a POST request to the URL specified and posts the payload
        that gets passed in. The content type header gets set to the
        specified content type.
        """

        headers = None
        if kwargs:
            if 'headers' in kwargs:
                headers = kwargs['headers']
                del(kwargs['headers'])
                self.logger.debug('Headers passed in:%s' % headers)
            if url.find('?') >= 0:
                url = url + '&' + urlencode(kwargs)
            else:
                url = url + '?' + urlencode(kwargs)

        self.logger.debug('About to do a POST on:' + url)

        request = RESTRequest(url, payload, method='POST')


        # set the content type header
        request.add_header('Content-Type', contentType)

        # add a user-agent
        request.add_header('User-Agent', self.user_agent)
        if headers:
            for k, v in headers.items():
                self.logger.debug('Adding header:%s:%s' % (k, v))
                request.add_header(k, v)

        # create a password manager
        passwordManager = HTTPPasswordMgrWithDefaultRealm()
        passwordManager.add_password(None, url, username, password)

        opener = build_opener(SmartRedirectHandler(),
                              DefaultErrorHandler(),
                              ContextualBasicAuthHandler(passwordManager))

        try:
            return opener.open(request)
        except HTTPError, e:
            if e.code is not 201:
                return e
            else:
                return e.read()
Example #29
0
    def post(self,
             url,
             payload,
             contentType,
             username=None,
             password=None,
             **kwargs):

        """
        Makes a POST request to the URL specified and posts the payload
        that gets passed in. The content type header gets set to the
        specified content type.
        """

        headers = None
        if kwargs:
            if 'headers' in kwargs:
                headers = kwargs['headers']
                del(kwargs['headers'])
                self.logger.debug('Headers passed in:%s' % headers)
            if url.find('?') >= 0:
                url = url + '&' + urlencode(kwargs)
            else:
                url = url + '?' + urlencode(kwargs)

        self.logger.debug('About to do a POST on:' + url)

        request = RESTRequest(url, payload, method='POST')

        # set the content type header
        request.add_header('Content-Type', contentType)

        # add a user-agent
        request.add_header('User-Agent', self.user_agent)
        if headers:
            for k, v in headers.items():
                self.logger.debug('Adding header:%s:%s' % (k, v))
                request.add_header(k, v)

        # create a password manager
        passwordManager = HTTPPasswordMgrWithDefaultRealm()
        passwordManager.add_password(None, url, username, password)

        opener = build_opener(SmartRedirectHandler(),
                              DefaultErrorHandler(),
                              ContextualBasicAuthHandler(passwordManager))

        try:
            return opener.open(request)
        except HTTPError, e:
            if e.code is not 201:
                return e
            else:
                return e.read()
Example #30
0
	def getOpener(self):
		"""
		Return a custom urllib2 opener for logged request to supervisor.
		"""
		from urllib2 import HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler, build_opener, install_opener

		passman = HTTPPasswordMgrWithDefaultRealm()
		passman.add_password(None, self.index, self.login, self.password)
		authhandler = HTTPBasicAuthHandler(passman)
		opener = build_opener(authhandler)
		install_opener(opener)
		return opener
Example #31
0
 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)
Example #32
0
    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()
        }
Example #33
0
    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)
Example #34
0
    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)
Example #35
0
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
Example #36
0
  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]          
Example #37
0
    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]
Example #38
0
File: wiki.py Project: 0k/wikitools
	def __init__(self, url="https://en.wikipedia.org/w/api.php", httpuser=None, httppass=None):
		"""
		url - A URL to the site's API, defaults to en.wikipedia
		httpuser - optional user name for HTTP Auth
        	httppass - password for HTTP Auth, leave out to enter interactively

		"""
		self.apibase = url
		self.cookies = WikiCookieJar()
		self.username = ''
		urlbits = urlparse(self.apibase)
		self.domain = '://'.join([urlbits.scheme, urlbits.netloc])
		if httpuser is not None:
			if httppass is None:
				from getpass import getpass
				self.httppass = getpass("HTTP Auth password for "+httpuser+": ")
			self.passman = HTTPPasswordMgrWithDefaultRealm()
			self.passman.add_password(None, self.domain, httpuser, httppass)
		else:
			self.passman = None
		self.maxlag = 5
		self.maxwaittime = 120
		self.useragent = "python-wikitools/%s" % VERSION
		self.cookiepath = ''
		self.limit = 500
		self.siteinfo = {}
		self.namespaces = {}
		self.NSaliases = {}
		self.assertval = None
		try:
			self.setSiteinfo()
		except api.APIError: # probably read-restricted
			pass
Example #39
0
 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, }
Example #40
0
    def __init__(self,
                 url="https://en.wikipedia.org/w/api.php",
                 httpuser=None,
                 httppass=None,
                 preauth=False):
        """
		url - A URL to the site's API, defaults to en.wikipedia
		httpuser - optional user name for HTTP Auth
        	httppass - password for HTTP Auth, leave out to enter interactively
		preauth - true to send headers for HTTP Auth on the first request
		          instead of relying on the negotiation for them

		"""
        self.apibase = url
        self.cookies = WikiCookieJar()
        self.username = ''
        urlbits = urlparse(self.apibase)
        self.domain = '://'.join([urlbits.scheme, urlbits.netloc])
        if httpuser is not None:
            if httppass is None:
                from getpass import getpass
                self.httppass = getpass("HTTP Auth password for " + httpuser +
                                        ": ")
            if preauth:
                self.httppass = httppass
                self.auth = httpuser
            else:
                self.passman = HTTPPasswordMgrWithDefaultRealm()
                self.passman.add_password(None, self.domain, httpuser,
                                          httppass)
        else:
            self.passman = None
            self.auth = None
        self.maxlag = 5
        self.maxwaittime = 120
        self.useragent = "python-wikitools/%s" % VERSION
        self.cookiepath = ''
        self.limit = 500
        self.siteinfo = {}
        self.namespaces = {}
        self.NSaliases = {}
        self.assertval = None
        self.newtoken = False
        try:
            self.setSiteinfo()
        except api.APIError:  # probably read-restricted
            pass
Example #41
0
    def get(self,
            url,
            username=None,
            password=None,
            **kwargs):

        """ Makes a get request to the URL specified."""

        headers = None
        if kwargs:
            if 'headers' in kwargs:
                headers = kwargs['headers']
                del(kwargs['headers'])
                self.logger.debug('Headers passed in:%s' % headers)
            if url.find('?') >= 0:
                url = url + '&' + urlencode(kwargs)
            else:
                url = url + '?' + urlencode(kwargs)

        self.logger.debug('About to do a GET on:' + url)

        request = RESTRequest(url, method='GET')
        
        
        # TODO: Hack for avoid CRC failed in Gzip uncompression
        # Needs more work work, also Content-Type
        # Accept Encoding
        # JC
        request.add_header('Accept-Encoding', '')
        
        # add a user-agent
        request.add_header('User-Agent', self.user_agent)
        if headers:
            for k, v in headers.items():
                self.logger.debug('Adding header:%s:%s' % (k, v))
                request.add_header(k, v)

        # create a password manager
        passwordManager = HTTPPasswordMgrWithDefaultRealm()
        passwordManager.add_password(None, url, username, password)

        opener = build_opener(SmartRedirectHandler(),
                              DefaultErrorHandler(),
                              ContextualBasicAuthHandler(passwordManager))

        return opener.open(request)
Example #42
0
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')
    }
Example #43
0
    def __init__(self):
        '''
        Constructor. It delegates construction to the base class
        L{HttxObject} and initializes the member variables
        '''
        HttxObject.__init__(self)

        self.passmanager = HTTPPasswordMgrWithDefaultRealm()
Example #44
0
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')
    }
Example #45
0
 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)
Example #46
0
 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)
Example #47
0
 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, base_location=None, api_key=None, is_verbose=False,
              http_user=None, http_pass=None, user_agent=None):
     if base_location is not None:
         self.base_location = base_location
     if api_key:
         self.api_key = api_key
     else:
         self.api_key = self._get_api_key_from_config()
     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)
     self.user_agent = user_agent or 'ckanclient'
Example #49
0
def curl(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 {0!r} passed".format(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
    response = director.open(req)

    #return {
    #    "httpcode": response.code,
    #    "headers": response.info(),
    #    "content": response.read()
    #}
    return response.read()
Example #50
0
    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', '')
Example #51
0
 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)
Example #52
0
    def __init__(self, version='1.0.0', url=None, un=None, pw=None):
        """Initialize"""
        self.version = version
        self._infoset = None
        self.url = url
        self.username = un
        self.password = pw
        self._open = urlopen

        if self.username and self.password:
            # Provide login information in order to use the SOS server
            # Create an OpenerDirector with support for Basic HTTP 
            # Authentication...
            passman = HTTPPasswordMgrWithDefaultRealm()
            passman.add_password(None, self.url, self.username, self.password)
            auth_handler = HTTPBasicAuthHandler(passman)
            opener = build_opener(auth_handler)
            self._open = opener.open
Example #53
0
def openURL(url_base, data, method="Get", cookies=None, username=None, password=None):
    """ 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)
    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
Example #54
0
    def __init__(self, url, version='1.0.0', xml=None,
                username=None, password=None, timeout=None, ignore_proxy=False):
        """Initialize."""
        self.url = url
        self.username = username
        self.password = password
        self.timeout = timeout
        self.ignore_proxy = ignore_proxy
        self.version = version
        self._capabilities = None

        self._open = urlopen
        if self.ignore_proxy:
            #print "sos:76 - ignoring proxy..."
            proxy_support = ProxyHandler({})  # disables proxy
            opener = build_opener(proxy_support)
            install_opener(opener)
            self._open = opener.open

        if self.username and self.password:
            # Provide login information in order to use the SOS server
            # Create an OpenerDirector with support for Basic HTTP
            # Authentication...
            passman = HTTPPasswordMgrWithDefaultRealm()
            passman.add_password(None, self.url, self.username, self.password)
            auth_handler = HTTPBasicAuthHandler(passman)
            opener = build_opener(auth_handler)
            self._open = opener.open
            reader = SOSCapabilitiesReader(
                self.version, url=self.url, un=self.username, pw=self.password,
                to=self.timeout, px=self.ignore_proxy)
            self._capabilities = reader.readString(self.url)
        else:
            reader = SOSCapabilitiesReader(self.version, to=self.timeout,
                                           px=self.ignore_proxy)
            if xml:
                #read from stored xml
                self._capabilities = reader.readString(xml)
            else:
                #read from non-password protected server
                self._capabilities = reader.read(self.url, timeout=self.timeout)

        #build metadata objects
        self._buildMetadata()
Example #55
0
    def post(self,
             url,
             payload,
             contentType,
             username=None,
             password=None,
             **kwargs):

        """
        Makes a POST request to the URL specified and posts the payload
        that gets passed in. The content type header gets set to the
        specified content type.
        """

        if kwargs:
            if url.find('?') >= 0:
                url = url + '&' + urlencode(kwargs)
            else:
                url = url + '?' + urlencode(kwargs)

        request = RESTRequest(url, payload, method='POST')

        # set the content type header
        request.add_header('Content-Type', contentType)

        # add a user-agent
        request.add_header('User-Agent', self.user_agent)

        # create a password manager
        passwordManager = HTTPPasswordMgrWithDefaultRealm()
        passwordManager.add_password(None, url, username, password)

        opener = build_opener(SmartRedirectHandler(),
                              DefaultErrorHandler(),
                              ContextualBasicAuthHandler(passwordManager))

        try:
            return opener.open(request)
        except HTTPError, e:
            if e.code is not 201:
                return e
            else:
                return e.read()
Example #56
0
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
Example #57
0
    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
Example #58
0
    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)
Example #59
0
    def delete(self, url, username=None, password=None, **kwargs):

        """ Makes a delete request to the URL specified. """

        headers = None
        if kwargs:
            if 'headers' in kwargs:
                headers = kwargs['headers']
                del(kwargs['headers'])
                self.logger.debug('Headers passed in:%s' % headers)
            if url.find('?') >= 0:
                url = url + '&' + urlencode(kwargs)
            else:
                url = url + '?' + urlencode(kwargs)

        self.logger.debug('About to do a DELETE on:' + url)

        request = RESTRequest(url, method='DELETE')

        # add a user-agent
        request.add_header('User-Agent', self.user_agent)
        if headers:
            for k, v in headers.items():
                self.logger.debug('Adding header:%s:%s' % (k, v))
                request.add_header(k, v)

        # create a password manager
        passwordManager = HTTPPasswordMgrWithDefaultRealm()
        passwordManager.add_password(None, url, username, password)

        opener = build_opener(SmartRedirectHandler(),
                              DefaultErrorHandler(),
                              ContextualBasicAuthHandler(passwordManager))

        #try:
        #    opener.open(request)
        #except urllib2.HTTPError, e:
        #    if e.code is not 204:
        #        raise e
        #return None
        return opener.open(request)
Example #60
0
    def get(self, url, username=None, password=None, **kwargs):
        """ Makes a get request to the URL specified."""

        if kwargs:
            if url.find('?') >= 0:
                url = url + '&' + urlencode(kwargs)
            else:
                url = url + '?' + urlencode(kwargs)

        request = RESTRequest(url, method='GET')

        # add a user-agent
        request.add_header('User-Agent', self.user_agent)

        # create a password manager
        passwordManager = HTTPPasswordMgrWithDefaultRealm()
        passwordManager.add_password(None, url, username, password)

        opener = build_opener(SmartRedirectHandler(), DefaultErrorHandler(),
                              ContextualBasicAuthHandler(passwordManager))

        return opener.open(request)