Python urllib.unquote() Examples

The following are code examples for showing how to use urllib.unquote(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: pyblish-win   Author: pyblish   File: SimpleHTTPServer.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def translate_path(self, path):
        """Translate a /-separated PATH to the local filename syntax.

        Components that mean special things to the local file system
        (e.g. drive or directory names) are ignored.  (XXX They should
        probably be diagnosed.)

        """
        # abandon query parameters
        path = path.split('?',1)[0]
        path = path.split('#',1)[0]
        # Don't forget explicit trailing slash when normalizing. Issue17324
        trailing_slash = path.rstrip().endswith('/')
        path = posixpath.normpath(urllib.unquote(path))
        words = path.split('/')
        words = filter(None, words)
        path = os.getcwd()
        for word in words:
            drive, word = os.path.splitdrive(word)
            head, word = os.path.split(word)
            if word in (os.curdir, os.pardir): continue
            path = os.path.join(path, word)
        if trailing_slash:
            path += '/'
        return path 
Example 2
Project: pyblish-win   Author: pyblish   File: CGIHTTPServer.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def is_cgi(self):
        """Test whether self.path corresponds to a CGI script.

        Returns True and updates the cgi_info attribute to the tuple
        (dir, rest) if self.path requires running a CGI script.
        Returns False otherwise.

        If any exception is raised, the caller should assume that
        self.path was rejected as invalid and act accordingly.

        The default implementation tests whether the normalized url
        path begins with one of the strings in self.cgi_directories
        (and the next character is a '/' or the end of the string).
        """
        collapsed_path = _url_collapse_path(urllib.unquote(self.path))
        dir_sep = collapsed_path.find('/', 1)
        head, tail = collapsed_path[:dir_sep], collapsed_path[dir_sep+1:]
        if head in self.cgi_directories:
            self.cgi_info = head, tail
            return True
        return False 
Example 3
Project: pyblish-win   Author: pyblish   File: test_urllib.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_unquoting(self):
        # Make sure unquoting of all ASCII values works
        escape_list = []
        for num in range(128):
            given = hexescape(chr(num))
            expect = chr(num)
            result = urllib.unquote(given)
            self.assertEqual(expect, result,
                             "using unquote(): %s != %s" % (expect, result))
            result = urllib.unquote_plus(given)
            self.assertEqual(expect, result,
                             "using unquote_plus(): %s != %s" %
                             (expect, result))
            escape_list.append(given)
        escape_string = ''.join(escape_list)
        del escape_list
        result = urllib.unquote(escape_string)
        self.assertEqual(result.count('%'), 1,
                         "using quote(): not all characters escaped; %s" %
                         result)
        result = urllib.unquote(escape_string)
        self.assertEqual(result.count('%'), 1,
                         "using unquote(): not all characters escaped: "
                         "%s" % result) 
Example 4
Project: pyblish-win   Author: pyblish   File: test_urllib.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_unquoting_badpercent(self):
        # Test unquoting on bad percent-escapes
        given = '%xab'
        expect = given
        result = urllib.unquote(given)
        self.assertEqual(expect, result, "using unquote(): %r != %r"
                         % (expect, result))
        given = '%x'
        expect = given
        result = urllib.unquote(given)
        self.assertEqual(expect, result, "using unquote(): %r != %r"
                         % (expect, result))
        given = '%'
        expect = given
        result = urllib.unquote(given)
        self.assertEqual(expect, result, "using unquote(): %r != %r"
                         % (expect, result)) 
Example 5
Project: pyblish-win   Author: pyblish   File: ssl_servers.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def translate_path(self, path):
        """Translate a /-separated PATH to the local filename syntax.

        Components that mean special things to the local file system
        (e.g. drive or directory names) are ignored.  (XXX They should
        probably be diagnosed.)

        """
        # abandon query parameters
        path = urlparse.urlparse(path)[2]
        path = os.path.normpath(urllib.unquote(path))
        words = path.split('/')
        words = filter(None, words)
        path = self.root
        for word in words:
            drive, word = os.path.splitdrive(word)
            head, word = os.path.split(word)
            path = os.path.join(path, word)
        return path 
Example 6
Project: RelayBot2.0   Author: nukeop   File: youtube.py    GNU General Public License v3.0 6 votes vote down vote up
def get_video_info(self, message):
        match = self.yt_regex.search(message)
        yt_id = match.group('ID')

        reply = requests.get("http://youtube.com/get_video_info", params={
            "video_id": yt_id,
            "el": ["vevo", "embedded"]
            })

        if reply.text:
            info = reply.text.encode('utf-8')
            info = urllib.unquote(urllib.unquote(urllib.unquote(info))).decode('utf-8')

            title = self.get_args(info, "title", "&").replace("+", " ")
            rating = util.rating_to_stars(float(self.get_args(info, "avg_rating", "&")))
            length = int(self.get_args(info, "length_seconds", "&"))
            length = "{}:{}".format(length/60, length%60)

            return "{} | {} | {}".format(title, rating, length).decode('utf-8') 
Example 7
Project: Splunk_CBER_App   Author: MHaggis   File: internals.py    MIT License 6 votes vote down vote up
def read(self, ifile):
        """ Reads an input header from an input file.

        The input header is read as a sequence of *<name>***:***<value>* pairs separated by a newline. The end of the
        input header is signalled by an empty line or an end-of-file.

        :param ifile: File-like object that supports iteration over lines.

        """
        name, value = None, None

        for line in ifile:
            if line == '\n':
                break
            item = line.split(':', 1)
            if len(item) == 2:
                # start of a new item
                if name is not None:
                    self[name] = value[:-1]  # value sans trailing newline
                name, value = item[0], unquote(item[1])
            elif name is not None:
                # continuation of the current item
                value += unquote(line)

        if name is not None: self[name] = value[:-1] if value[-1] == '\n' else value 
Example 8
Project: Splunk_CBER_App   Author: MHaggis   File: client.py    MIT License 6 votes vote down vote up
def _entity_path(self, state):
        """Calculate the path to an entity to be returned.

        *state* should be the dictionary returned by
        :func:`_parse_atom_entry`. :func:`_entity_path` extracts the
        link to this entity from *state*, and strips all the namespace
        prefixes from it to leave only the relative path of the entity
        itself, sans namespace.

        :rtype: ``string``
        :return: an absolute path
        """
        # This has been factored out so that it can be easily
        # overloaded by Configurations, which has to switch its
        # entities' endpoints from its own properties/ to configs/.
        raw_path = urllib.unquote(state.links.alternate)
        if 'servicesNS/' in raw_path:
            return _trailing(raw_path, 'servicesNS/', '/', '/')
        elif 'services/' in raw_path:
            return _trailing(raw_path, 'services/')
        else:
            return raw_path 
Example 9
Project: Paradrop   Author: ParadropLabs   File: chutes.py    Apache License 2.0 6 votes vote down vote up
def installChute(host, port, config):
    '''
    Take a local config yaml file and launch chute of given host with pdfcd running on specified port.
    '''
    config_json = readChuteConfig(config)
    if config_json is None:
        return

    print 'Installing chute...\n'
    params = {'config': config_json}
    r = requests.post('http://' + host + ':' + str(port) + '/v1/chute/create', data=json.dumps(params), stream=True)
    for line in r.iter_lines():
        if line:
            try:
                line = json.loads(line)
                if line.get('success'):
                    print line.get('message')
                else:
                    print 'ERROR: Failed to install chute.(' + urllib.unquote(str(line.get('message'))) + ')'
            except Exception as e:
                print line 
Example 10
Project: Paradrop   Author: ParadropLabs   File: chutes.py    Apache License 2.0 6 votes vote down vote up
def updateChute(host, port, config):
    '''
    Take a local config yaml file and launch chute of given host with pdfcd running on specified port.
    '''
    config_json = readChuteConfig(config)
    if config_json is None:
        return

    print 'Updating chute...\n'
    params = {'config': config_json}
    r = requests.post('http://' + host + ':' + str(port) + '/v1/chute/update', data=json.dumps(params), stream=True)
    for line in r.iter_lines():
        if line:
            try:
                line = json.loads(line)
                if line.get('success'):
                    print line.get('message')
                else:
                    print 'ERROR: Failed to install chute.(' + urllib.unquote(str(line.get('message'))) + ')'
            except Exception as e:
                print line 
Example 11
Project: Paradrop   Author: ParadropLabs   File: chutes.py    Apache License 2.0 6 votes vote down vote up
def deleteChute(host, port, name):
    '''
    Remove chute with given name from host with pdfcd running on specified port.
    '''

    print 'Removing chute...\n'

    params = {'name': name}
    r = requests.post('http://' + host + ':' + str(port) + '/v1/chute/delete', data=json.dumps(params), stream=True)

    for line in r.iter_lines():
        if line:
            try:
                line = json.loads(line)
                if line.get('success'):
                    print line.get('message')
                else:
                    print 'ERROR: Failed to delete chute.(' + urllib.unquote(str(line.get('message'))) + ')'
            except Exception as e:
                print line 
Example 12
Project: Paradrop   Author: ParadropLabs   File: chutes.py    Apache License 2.0 6 votes vote down vote up
def stopChute(host, port, name):
    '''
    Stop chute with given name from host with pdfcd running on specified port.
    '''

    print 'Stopping chute...\n'

    params = {'name': name}
    r = requests.post('http://' + host + ':' + str(port) + '/v1/chute/stop', data=json.dumps(params), stream=True)

    for line in r.iter_lines():
        if line:
            try:
                line = json.loads(line)
                if line.get('success'):
                    print line.get('message')
                else:
                    print 'ERROR: Failed to stop chute.(' + urllib.unquote(str(line.get('message'))) + ')'
            except Exception as e:
                print line 
Example 13
Project: Paradrop   Author: ParadropLabs   File: chutes.py    Apache License 2.0 6 votes vote down vote up
def startChute(host, port, name):
    '''
    Start chute with given name from host with pdfcd running on specified port.
    '''

    print 'Starting chute...\n'

    params = {'name': name}
    r = requests.post('http://' + host + ':' + str(port) + '/v1/chute/start', data=json.dumps(params), stream=True)

    for line in r.iter_lines():
        if line:
            try:
                line = json.loads(line)
                if line.get('success'):
                    print line.get('message')
                else:
                    print 'ERROR: Failed to start chute.(' + urllib.unquote(str(line.get('message'))) + ')'
            except Exception as e:
                print line 
Example 14
Project: ngo-addons-backport   Author: camptocamp   File: http_server.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def translate_path(self, path):
        """Translate a /-separated PATH to the local filename syntax.

        Components that mean special things to the local file system
        (e.g. drive or directory names) are ignored.  (XXX They should
        probably be diagnosed.)

        """
        # abandon query parameters
        path = path.split('?',1)[0]
        path = path.split('#',1)[0]
        path = posixpath.normpath(urllib.unquote(path))
        words = path.split('/')
        words = filter(None, words)
        path = self.__basepath
        for word in words:
            if word in (os.curdir, os.pardir): continue
            path = os.path.join(path, word)
        return path 
Example 15
Project: openhatch   Author: campbe13   File: url.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def canonicalize_url(url, keep_blank_values=True, keep_fragments=False, \
        encoding=None):
    """Canonicalize the given url by applying the following procedures:

    - sort query arguments, first by key, then by value
    - percent encode paths and query arguments. non-ASCII characters are
      percent-encoded using UTF-8 (RFC-3986)
    - normalize all spaces (in query arguments) '+' (plus symbol)
    - normalize percent encodings case (%2f -> %2F)
    - remove query arguments with blank values (unless keep_blank_values is True)
    - remove fragments (unless keep_fragments is True)

    The url passed can be a str or unicode, while the url returned is always a
    str.

    For examples see the tests in scrapy.tests.test_utils_url
    """

    scheme, netloc, path, params, query, fragment = parse_url(url)
    keyvals = cgi.parse_qsl(query, keep_blank_values)
    keyvals.sort()
    query = urllib.urlencode(keyvals)
    path = safe_url_string(urllib.unquote(path))
    fragment = '' if not keep_fragments else fragment
    return urlparse.urlunparse((scheme, netloc.lower(), path, params, query, fragment)) 
Example 16
Project: taxonwiki   Author: philiptzou   File: manage.py    MIT License 6 votes vote down vote up
def list_routes():
    import urllib
    output = []
    for rule in app.url_map.iter_rules():

        options = {}
        for arg in rule.arguments:
            options[arg] = "[{0}]".format(arg)

        methods = ','.join(rule.methods)
        url = url_for(rule.endpoint, **options)
        line = urllib.unquote("{0:40s} {1:30s} {2}"
                              .format(rule.endpoint, methods, url))
        output.append(line)

    for line in sorted(output):
        print line 
Example 17
Project: the-S3-amongos   Author: chembl   File: the-S3-amongos.py    Apache License 2.0 6 votes vote down vote up
def delete(self, *args):
        
        if self.bucket_name and self.object_name:
            bucket_name = self.bucket_name
            object_name = self.object_name
        else:
            bucket_name,object_name = args
            
        original_name = urllib.unquote(object_name)
        
        bucket_object = self._get_bucket_object(bucket_name=bucket_name,object_name=object_name)
        
        if bucket_object:
            self.set_status(204)
            self.application.S3[bucket_name].remove({"_id":bucket_object['_id']})
            self.finish()
        else:
            self._error(code=404,s3code='NSK')
            return 
Example 18
Project: the-S3-amongos   Author: chembl   File: the-S3-amongos.py    Apache License 2.0 6 votes vote down vote up
def head(self, *args):
        
        if hasattr(self,'bucket_name') and hasattr(self,'object_name'):
            bucket_name = self.bucket_name  
            object_name = self.object_name    
        else:
            bucket_name,object_name = args
        
        object_name = urllib.unquote(object_name)
        
        bucket_object = self._get_bucket_object(bucket_name=bucket_name,object_name=object_name)
        
        if bucket_object:
            self.set_header('etag', '"%s"' % bucket_object['md5'])
            self.set_header('last-modified', '"%s"' % bucket_object['updated'])
            self.finish()        
        else:
            self._error(code=404,s3code='NSK')
            return 
Example 19
Project: Pancas   Author: Sup3Roque   File: genesisresolvers.py    GNU General Public License v2.0 6 votes vote down vote up
def resolve(self, url):
        try:
            result = getUrl(url, close=False).result
            result = result.replace('\n','')

            url = re.compile('function\s*load_download.+?src\s*:\s*"(.+?)"').findall(result)[0]
            url = urlparse.urljoin('http://veehd.com', url)

            result = getUrl(url, close=False).result

            i = common.parseDOM(result, "iframe", ret="src")
            if len(i) > 0:
                i = urlparse.urljoin('http://veehd.com', i[0])
                getUrl(i, close=False).result
                result = getUrl(url).result

            url = re.compile('href *= *"([^"]+(?:mkv|mp4|avi))"').findall(result)
            url += re.compile('src *= *"([^"]+(?:divx|avi))"').findall(result)
            url += re.compile('"url" *: *"(.+?)"').findall(result)
            url = urllib.unquote(url[0])
            return url
        except:
            return 
Example 20
Project: Pancas   Author: Sup3Roque   File: veehd.py    GNU General Public License v2.0 6 votes vote down vote up
def resolve(url):
    try:
        result = client.request(url, close=False)
        result = result.replace('\n','')

        url = re.compile('function\s*load_download.+?src\s*:\s*"(.+?)"').findall(result)[0]
        url = urlparse.urljoin('http://veehd.com', url)

        result = client.request(url, close=False)

        i = client.parseDOM(result, 'iframe', ret='src')
        if len(i) > 0:
            i = urlparse.urljoin('http://veehd.com', i[0])
            client.request(i, close=False)
            result = client.request(url)

        url = re.compile('href *= *"([^"]+(?:mkv|mp4|avi))"').findall(result)
        url += re.compile('src *= *"([^"]+(?:divx|avi))"').findall(result)
        url += re.compile('"url" *: *"(.+?)"').findall(result)
        url = urllib.unquote(url[0])
        return url
    except:
        return 
Example 21
Project: Pancas   Author: Sup3Roque   File: youtube.py    GNU General Public License v2.0 6 votes vote down vote up
def live(result, id):
    try:
        hls = re.compile('"hlsvp" *: *"(.+?)"').findall(result)
        if len(hls) == 0:
            url = 'https://www.youtube.com/watch?v=%s' % id
            url = 'http://translate.googleusercontent.com/translate_c?anno=2&hl=en&sl=mt&tl=en&u=%s' % url
            hls = client.request(url)
            hls = re.compile('"hlsvp" *: *"(.+?)"').findall(hls)

        url = urllib.unquote(hls[0]).replace('\\/', '/')

        result = client.request(url)
        result = result.replace('\n','')
        url = re.compile('RESOLUTION *= *(\d*)x\d{1}.+?(http.+?\.m3u8)').findall(result)
        url = [(int(i[0]), i[1]) for i in url]
        url.sort()
        url = url[-1][1]
        return url
    except:
        return 
Example 22
Project: kicker-module   Author: EvanTheB   File: ladder_web.py    GNU General Public License v3.0 5 votes vote down vote up
def GET(self):
        user_input = web.input(command='')
        user_command = urllib.unquote(user_input.command)
        print user_command
        ret = k.ladder_command(user_command.split())
        web.header('Content-Type', 'application/json')
        return json.dumps(ret) 
Example 23
Project: pyblish-win   Author: pyblish   File: utils.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def parseaddr(addr):
    addrs = _AddressList(addr).addresslist
    if not addrs:
        return '', ''
    return addrs[0]


# rfc822.unquote() doesn't properly de-backslash-ify in Python pre-2.3. 
Example 24
Project: pyblish-win   Author: pyblish   File: utils.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def unquote(str):
    """Remove quotes from a string."""
    if len(str) > 1:
        if str.startswith('"') and str.endswith('"'):
            return str[1:-1].replace('\\\\', '\\').replace('\\"', '"')
        if str.startswith('<') and str.endswith('>'):
            return str[1:-1]
    return str



# RFC2231-related functions - parameter encoding and decoding 
Example 25
Project: pyblish-win   Author: pyblish   File: utils.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def collapse_rfc2231_value(value, errors='replace',
                           fallback_charset='us-ascii'):
    if isinstance(value, tuple):
        rawval = unquote(value[2])
        charset = value[0] or 'us-ascii'
        try:
            return unicode(rawval, charset, errors)
        except LookupError:
            # XXX charset is unknown to Python.
            return unicode(rawval, fallback_charset, errors)
    else:
        return unquote(value) 
Example 26
Project: pyblish-win   Author: pyblish   File: macurl2path.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def url2pathname(pathname):
    """OS-specific conversion from a relative URL of the 'file' scheme
    to a file system path; not recommended for general use."""
    #
    # XXXX The .. handling should be fixed...
    #
    tp = urllib.splittype(pathname)[0]
    if tp and tp != 'file':
        raise RuntimeError, 'Cannot convert non-local URL to pathname'
    # Turn starting /// into /, an empty hostname means current host
    if pathname[:3] == '///':
        pathname = pathname[2:]
    elif pathname[:2] == '//':
        raise RuntimeError, 'Cannot convert non-local URL to pathname'
    components = pathname.split('/')
    # Remove . and embedded ..
    i = 0
    while i < len(components):
        if components[i] == '.':
            del components[i]
        elif components[i] == '..' and i > 0 and \
                                  components[i-1] not in ('', '..'):
            del components[i-1:i+1]
            i = i-1
        elif components[i] == '' and i > 0 and components[i-1] != '':
            del components[i]
        else:
            i = i+1
    if not components[0]:
        # Absolute unix path, don't start with colon
        rv = ':'.join(components[1:])
    else:
        # relative unix path, start with colon. First replace
        # leading .. by empty strings (giving ::file)
        i = 0
        while i < len(components) and components[i] == '..':
            components[i] = ''
            i = i + 1
        rv = ':' + ':'.join(components)
    # and finally unquote slashes and other funny characters
    return urllib.unquote(rv) 
Example 27
Project: pyblish-win   Author: pyblish   File: simple_server.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def get_environ(self):
        env = self.server.base_environ.copy()
        env['SERVER_PROTOCOL'] = self.request_version
        env['REQUEST_METHOD'] = self.command
        if '?' in self.path:
            path,query = self.path.split('?',1)
        else:
            path,query = self.path,''

        env['PATH_INFO'] = urllib.unquote(path)
        env['QUERY_STRING'] = query

        host = self.address_string()
        if host != self.client_address[0]:
            env['REMOTE_HOST'] = host
        env['REMOTE_ADDR'] = self.client_address[0]

        if self.headers.typeheader is None:
            env['CONTENT_TYPE'] = self.headers.type
        else:
            env['CONTENT_TYPE'] = self.headers.typeheader

        length = self.headers.getheader('content-length')
        if length:
            env['CONTENT_LENGTH'] = length

        for h in self.headers.headers:
            k,v = h.split(':',1)
            k=k.replace('-','_').upper(); v=v.strip()
            if k in env:
                continue                    # skip content length, type,etc.
            if 'HTTP_'+k in env:
                env['HTTP_'+k] += ','+v     # comma-separate multiple headers
            else:
                env['HTTP_'+k] = v
        return env 
Example 28
Project: pyblish-win   Author: pyblish   File: robotparser.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def can_fetch(self, useragent, url):
        """using the parsed robots.txt decide if useragent can fetch url"""
        if self.disallow_all:
            return False
        if self.allow_all:
            return True

        # Until the robots.txt file has been read or found not
        # to exist, we must assume that no url is allowable.
        # This prevents false positives when a user erronenously
        # calls can_fetch() before calling read().
        if not self.last_checked:
            return False

        # search for given user agent matches
        # the first match counts
        parsed_url = urlparse.urlparse(urllib.unquote(url))
        url = urlparse.urlunparse(('', '', parsed_url.path,
            parsed_url.params, parsed_url.query, parsed_url.fragment))
        url = urllib.quote(url)
        if not url:
            url = "/"
        for entry in self.entries:
            if entry.applies_to(useragent):
                return entry.allowance(url)
        # try the default entry last
        if self.default_entry:
            return self.default_entry.allowance(url)
        # agent not found ==> access granted
        return True 
Example 29
Project: pyblish-win   Author: pyblish   File: test_urllib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_unquoting_mixed_case(self):
        # Test unquoting on mixed-case hex digits in the percent-escapes
        given = '%Ab%eA'
        expect = '\xab\xea'
        result = urllib.unquote(given)
        self.assertEqual(expect, result, "using unquote(): %r != %r"
                         % (expect, result)) 
Example 30
Project: pyblish-win   Author: pyblish   File: test_urllib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_unquoting_parts(self):
        # Make sure unquoting works when have non-quoted characters
        # interspersed
        given = 'ab%sd' % hexescape('c')
        expect = "abcd"
        result = urllib.unquote(given)
        self.assertEqual(expect, result,
                         "using quote(): %s != %s" % (expect, result))
        result = urllib.unquote_plus(given)
        self.assertEqual(expect, result,
                         "using unquote_plus(): %s != %s" % (expect, result)) 
Example 31
Project: pyblish-win   Author: pyblish   File: test_urllib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_unquoting_plus(self):
        # Test difference between unquote() and unquote_plus()
        given = "are+there+spaces..."
        expect = given
        result = urllib.unquote(given)
        self.assertEqual(expect, result,
                         "using unquote(): %s != %s" % (expect, result))
        expect = given.replace('+', ' ')
        result = urllib.unquote_plus(given)
        self.assertEqual(expect, result,
                         "using unquote_plus(): %s != %s" % (expect, result)) 
Example 32
Project: pyblish-win   Author: pyblish   File: xmlrpclib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def get_host_info(self, host):

        x509 = {}
        if isinstance(host, TupleType):
            host, x509 = host

        import urllib
        auth, host = urllib.splituser(host)

        if auth:
            import base64
            auth = base64.encodestring(urllib.unquote(auth))
            auth = string.join(string.split(auth), "") # get rid of whitespace
            extra_headers = [
                ("Authorization", "Basic " + auth)
                ]
        else:
            extra_headers = None

        return host, extra_headers, x509

    ##
    # Connect to server.
    #
    # @param host Target host.
    # @return A connection handle. 
Example 33
Project: xadmin_bugfix   Author: vip68   File: themes.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _get_theme(self):
        if self.user:
            try:
                return UserSettings.objects.get(user=self.user, key="site-theme").value
            except Exception:
                pass
        if '_theme' in self.request.COOKIES:
            if six.PY2:
                func = urllib.unquote
            else:
                func = urllib.parse.unquote
            return func(self.request.COOKIES['_theme'])
        return self.default_theme 
Example 34
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: fixers.py    Apache License 2.0 5 votes vote down vote up
def __call__(self, environ, start_response):
        for key in 'REQUEST_URL', 'REQUEST_URI', 'UNENCODED_URL':
            if key not in environ:
                continue
            request_uri = unquote(environ[key])
            script_name = unquote(environ.get('SCRIPT_NAME', ''))
            if request_uri.startswith(script_name):
                environ['PATH_INFO'] = request_uri[len(script_name):] \
                    .split('?', 1)[0]
                break
        return self.app(environ, start_response) 
Example 35
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: fixers.py    Apache License 2.0 5 votes vote down vote up
def __call__(self, environ, start_response):
        for key in 'REQUEST_URL', 'REQUEST_URI', 'UNENCODED_URL':
            if key not in environ:
                continue
            request_uri = unquote(environ[key])
            script_name = unquote(environ.get('SCRIPT_NAME', ''))
            if request_uri.startswith(script_name):
                environ['PATH_INFO'] = request_uri[len(script_name):] \
                    .split('?', 1)[0]
                break
        return self.app(environ, start_response) 
Example 36
Project: django-xadmin   Author: MarkHoo   File: themes.py    MIT License 5 votes vote down vote up
def _get_theme(self):
        if self.user:
            try:
                return UserSettings.objects.get(user=self.user, key="site-theme").value
            except Exception:
                pass
        if '_theme' in self.request.COOKIES:
            if six.PY2:
                func = urllib.unquote
            else:
                func = urllib.parse.unquote
            return func(self.request.COOKIES['_theme'])
        return self.default_theme 
Example 37
Project: django-xadmin   Author: MarkHoo   File: themes.py    MIT License 5 votes vote down vote up
def _get_theme(self):
        if self.user:
            try:
                return UserSettings.objects.get(user=self.user, key="site-theme").value
            except Exception:
                pass
        if '_theme' in self.request.COOKIES:
            if six.PY2:
                func = urllib.unquote
            else:
                func = urllib.parse.unquote
            return func(self.request.COOKIES['_theme'])
        return self.default_theme 
Example 38
Project: django-xadmin   Author: MarkHoo   File: themes.py    MIT License 5 votes vote down vote up
def _get_theme(self):
        if self.user:
            try:
                return UserSettings.objects.get(user=self.user, key="site-theme").value
            except Exception:
                pass
        if '_theme' in self.request.COOKIES:
            if six.PY2:
                func = urllib.unquote
            else:
                func = urllib.parse.unquote
            return func(self.request.COOKIES['_theme'])
        return self.default_theme 
Example 39
Project: slidoc   Author: mitotic   File: sdserver.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_id_from_cookie(self, role=False, for_site='', sites=False, name=False, email=False, altid=False, data=False):
        # If for_site and site name does not appear in cookie.sites, None will be returned for role
        # Null string will be returned for role, if site name is present
        cookieStr = self.get_user_cookie()
        if not cookieStr:
            return None
        try:
            comps = [urllib.unquote(x) for x in cookieStr.split(':')]
            ##if Options['debug']:
                ##print >> sys.stderr, "DEBUG: sdserver.UserIdMixin.get_id_from_cookie", comps
            userId, userRole, userSites, token, data_json = comps[:5]
            userData = json.loads(base64.b64decode(data_json))

            if role:
                if not userRole and for_site:
                    if not userSites:
                        return None
                    return sdproxy.getSiteRole(for_site, userSites)
                return userRole

            if sites:
                return userSites
            if name:
                return userData.get('name', '')
            if email:
                return userData.get('email', '')
            if altid:
                return userData.get('altid', '')
            if data:
                return userData
            return userId
        except Exception, err:
            print >> sys.stderr, 'sdserver: COOKIE ERROR - '+str(err)
            self.clear_user_cookie()
            return None 
Example 40
Project: slidoc   Author: mitotic   File: sdserver.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def batch_login(self, site_name=''):
        # Batch auto login for localhost through query parameter: ...?auth=userId:token
        # To print exams using headless browser: ...?auth=userId:token&print=1
        site_key = sliauth.gen_site_key(Options['root_auth_key'], site_name) if site_name else Options['root_auth_key']
        upload_key = sliauth.gen_hmac_token(site_key, 'upload:')

        userId, batchMode, siteRole, cookieData = None, None, None, None
        query = self.request.query
        if query.startswith('auth='):
            query = query.split('&')[0]
            qauth = urllib.unquote(query[len('auth='):])
            temUserId, token = qauth.split(':')
            nonce = self.use_nonce(site_name, temUserId)
            if nonce:
                batchToken = sliauth.gen_hmac_token(upload_key, 'batch:'+temUserId+':'+nonce)
                if token == upload_key or token == batchToken:
                    batchMode = site_name or '_root'
                    cookieData = {'batch': batchMode}
                elif token == gen_proxy_auth_token(temUserId, root=True):
                    cookieData = {}
                else:
                    print >> sys.stderr, 'sdserver: Invalid auth= for temUserId '+temUserId
                    temUserId = None

                if temUserId is not None:
                    userId = temUserId
                    print >> sys.stderr, "UserIdMixin.batch_login: BATCH ACCESS", self.request.path, site_name, userId
                    self.set_id(userId, displayName=userId, data=cookieData)
        return userId, batchMode, siteRole, cookieData 
Example 41
Project: slidoc   Author: mitotic   File: sliauth.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def safe_unquote(value):
    return urllib.unquote(value) 
Example 42
Project: trade   Author: alexbeam   File: Robinhood.py    MIT License 5 votes vote down vote up
def place_order(self, instrument, quantity=1, bid_price = None, transaction=None):
        if bid_price == None:
            bid_price = self.quote_data(instrument['symbol'])[0]['bid_price']
        data = 'account=%s&instrument=%s&price=%f&quantity=%d&side=buy&symbol=%s&time_in_force=gfd&trigger=immediate&type=market' % (urllib.quote('https://api.robinhood.com/accounts/5PY93481/'), urllib.unquote(instrument['url']), float(bid_price), quantity, instrument['symbol']) 
        res = self.session.post(self.endpoints['orders'], data=data)
        return res 
Example 43
Project: flasky   Author: RoseOu   File: fixers.py    MIT License 5 votes vote down vote up
def __call__(self, environ, start_response):
        for key in 'REQUEST_URL', 'REQUEST_URI', 'UNENCODED_URL':
            if key not in environ:
                continue
            request_uri = unquote(environ[key])
            script_name = unquote(environ.get('SCRIPT_NAME', ''))
            if request_uri.startswith(script_name):
                environ['PATH_INFO'] = request_uri[len(script_name):] \
                    .split('?', 1)[0]
                break
        return self.app(environ, start_response) 
Example 44
Project: Splunk_CBER_App   Author: MHaggis   File: internals.py    MIT License 5 votes vote down vote up
def unquote(cls, string):
        """ Removes quotes from a quoted string.

        Splunk search command quote rules are applied. The enclosing double-quotes, if present, are removed. Escaped
        double-quotes ('\"' or '""') are replaced by a single double-quote ('"').

        **NOTE**

        We are not using a json.JSONDecoder because Splunk quote rules are different than JSON quote rules. A
        json.JSONDecoder does not recognize a pair of double-quotes ('""') as an escaped quote ('"') and will
        decode single-quoted strings ("'") in addition to double-quoted ('"') strings.

        """
        if len(string) == 0:
            return ''

        if string[0] == '"':
            if len(string) == 1 or string[-1] != '"':
                raise SyntaxError('Poorly formed string literal: ' + string)
            string = string[1:-1]

        if len(string) == 0:
            return ''

        def replace(match):
            value = match.group(0)
            if value == '""':
                return '"'
            if len(value) < 2:
                raise SyntaxError('Poorly formed string literal: ' + string)
            return value[1]

        result = re.sub(cls._escaped_character_re, replace, string)
        return result

    # region Class variables 
Example 45
Project: Splunk_CBER_App   Author: MHaggis   File: client.py    MIT License 5 votes vote down vote up
def create(self, name, **params):
        """Creates a new role.

        This function makes two roundtrips to the server, plus at most
        two more if
        the ``autologin`` field of :func:`connect` is set to ``True``.

        :param name: Name for the role.
        :type name: ``string``
        :param params: Additional arguments (optional). For a list of available
            parameters, see `Roles parameters
            <http://dev.splunk.com/view/SP-CAAAEJ6#rolesparams>`_
            on Splunk Developer Portal.
        :type params: ``dict``

        :return: The new role.
        :rtype: :class:`Role`

        **Example**::

            import splunklib.client as client
            c = client.connect(...)
            roles = c.roles
            paltry = roles.create("paltry", imported_roles="user", defaultApp="search")
        """
        if not isinstance(name, basestring):
            raise ValueError("Invalid role name: %s" % str(name))
        name = name.lower()
        self.post(name=name, **params)
        # splunkd doesn't return the user in the POST response body,
        # so we have to make a second round trip to fetch it.
        response = self.get(name)
        entry = _load_atom(response, XNAME_ENTRY).entry
        state = _parse_atom_entry(entry)
        entity = self.item(
            self.service,
            urllib.unquote(state.links.alternate),
            state=state)
        return entity 
Example 46
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: security_group_base.py    Apache License 2.0 5 votes vote down vote up
def parse_cidr(self, cidr):
        if cidr:
            try:
                cidr = urllib.unquote(cidr).decode()
            except Exception as e:
                self.raise_invalid_cidr(cidr, e)

            if not utils.is_valid_cidr(cidr):
                self.raise_invalid_cidr(cidr)

            return cidr
        else:
            return '0.0.0.0/0' 
Example 47
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: floating_ip_dns.py    Apache License 2.0 5 votes vote down vote up
def _unquote_domain(domain):
    """Unquoting function for receiving a domain name in a URL.

    Domain names tend to have .'s in them.  Urllib doesn't quote dots,
    but Routes tends to choke on them, so we need an extra level of
    by-hand quoting here.
    """
    return urllib.unquote(domain).replace('%2E', '.') 
Example 48
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: floating_ip_dns.py    Apache License 2.0 5 votes vote down vote up
def _unquote_domain(domain):
    """Unquoting function for receiving a domain name in a URL.

    Domain names tend to have .'s in them.  Urllib doesn't quote dots,
    but Routes tends to choke on them, so we need an extra level of
    by-hand quoting here.
    """
    return urllib.unquote(domain).replace('%2E', '.') 
Example 49
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: rbd_utils.py    Apache License 2.0 5 votes vote down vote up
def parse_url(self, url):
        prefix = 'rbd://'
        if not url.startswith(prefix):
            reason = _('Not stored in rbd')
            raise exception.ImageUnacceptable(image_id=url, reason=reason)
        pieces = map(urllib.unquote, url[len(prefix):].split('/'))
        if '' in pieces:
            reason = _('Blank components')
            raise exception.ImageUnacceptable(image_id=url, reason=reason)
        if len(pieces) != 4:
            reason = _('Not an rbd snapshot')
            raise exception.ImageUnacceptable(image_id=url, reason=reason)
        return pieces 
Example 50
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: manage.py    Apache License 2.0 5 votes vote down vote up
def create(self, name, cell_type='child', username=None, broker_hosts=None,
               password=None, hostname=None, port=None, virtual_host=None,
               woffset=None, wscale=None):

        if cell_type not in ['parent', 'child', 'api', 'compute']:
            print("Error: cell type must be 'parent'/'api' or "
                "'child'/'compute'")
            return(2)

        # Set up the transport URL
        transport_hosts = self._create_transport_hosts(
                                                 username, password,
                                                 broker_hosts, hostname,
                                                 port)
        transport_url = rpc.get_transport_url()
        transport_url.hosts.extend(transport_hosts)
        transport_url.virtual_host = virtual_host

        is_parent = False
        if cell_type in ['api', 'parent']:
            is_parent = True
        values = {'name': name,
                  'is_parent': is_parent,
                  'transport_url': urllib.unquote(str(transport_url)),
                  'weight_offset': float(woffset),
                  'weight_scale': float(wscale)}
        ctxt = context.get_admin_context()
        db.cell_create(ctxt, values)