Python urllib.quote() Examples

The following are code examples for showing how to use urllib.quote(). 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: webchecker.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def open_file(self, url):
        path = urllib.url2pathname(urllib.unquote(url))
        if os.path.isdir(path):
            if path[-1] != os.sep:
                url = url + '/'
            indexpath = os.path.join(path, "index.html")
            if os.path.exists(indexpath):
                return self.open_file(url + "index.html")
            try:
                names = os.listdir(path)
            except os.error, msg:
                exc_type, exc_value, exc_tb = sys.exc_info()
                raise IOError, msg, exc_tb
            names.sort()
            s = MyStringIO("file:"+url, {'content-type': 'text/html'})
            s.write('<BASE HREF="file:%s">\n' %
                    urllib.quote(os.path.join(path, "")))
            for name in names:
                q = urllib.quote(name)
                s.write('<A HREF="%s">%s</A>\n' % (q, q))
            s.seek(0)
            return s 
Example 2
Project: pyblish-win   Author: pyblish   File: util.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def application_uri(environ):
    """Return the application's base URI (no PATH_INFO or QUERY_STRING)"""
    url = environ['wsgi.url_scheme']+'://'
    from urllib import quote

    if environ.get('HTTP_HOST'):
        url += environ['HTTP_HOST']
    else:
        url += environ['SERVER_NAME']

        if environ['wsgi.url_scheme'] == 'https':
            if environ['SERVER_PORT'] != '443':
                url += ':' + environ['SERVER_PORT']
        else:
            if environ['SERVER_PORT'] != '80':
                url += ':' + environ['SERVER_PORT']

    url += quote(environ.get('SCRIPT_NAME') or '/')
    return url 
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_quoting_space(self):
        # Make sure quote() and quote_plus() handle spaces as specified in
        # their unique way
        result = urllib.quote(' ')
        self.assertEqual(result, hexescape(' '),
                         "using quote(): %s != %s" % (result, hexescape(' ')))
        result = urllib.quote_plus(' ')
        self.assertEqual(result, '+',
                         "using quote_plus(): %s != +" % result)
        given = "a b cd e f"
        expect = given.replace(' ', hexescape(' '))
        result = urllib.quote(given)
        self.assertEqual(expect, result,
                         "using quote(): %s != %s" % (expect, result))
        expect = given.replace(' ', '+')
        result = urllib.quote_plus(given)
        self.assertEqual(expect, result,
                         "using quote_plus(): %s != %s" % (expect, result)) 
Example 4
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_servers.py    Apache License 2.0 6 votes vote down vote up
def test_get_servers_system_metadata_filter(self, get_all_mock):
        server_uuid0 = str(uuid.uuid4())
        server_uuid1 = str(uuid.uuid4())
        expected_system_metadata = u'{"some_value": "some_key"}'
        db_list = [fakes.stub_instance(100, uuid=server_uuid0),
                        fakes.stub_instance(101, uuid=server_uuid1)]
        get_all_mock.return_value = instance_obj._make_instance_list(
                        context, instance_obj.InstanceList(), db_list, FIELDS)

        req = fakes.HTTPRequest.blank(
            '/fake/servers?status=active&status=error&system_metadata=' +
            urllib.quote(expected_system_metadata),
            use_admin_context=True)
        servers = self.controller.index(req)['servers']
        self.assertEqual(2, len(servers))
        self.assertEqual(server_uuid0, servers[0]['id'])
        self.assertEqual(server_uuid1, servers[1]['id'])
        expected_search_opts = dict(
            deleted=False, vm_state=[vm_states.ACTIVE, vm_states.ERROR],
            system_metadata=expected_system_metadata, project_id='fake')
        get_all_mock.assert_called_once_with(mock.ANY,
                        search_opts=expected_search_opts, limit=mock.ANY,
                        marker=mock.ANY, want_objects=mock.ANY,
                        expected_attrs=None,
                        sort_keys=mock.ANY, sort_dirs=mock.ANY) 
Example 5
Project: Paradrop   Author: ParadropLabs   File: pdutils.py    Apache License 2.0 6 votes vote down vote up
def urlEncodeMe(elem, safe=' '):
    """
        Converts any values that would cause JSON parsing to fail into URL percent encoding equivalents.
        This function can be used for any valid JSON type including str, dict, list.
        Returns:
            Same element properly encoded.
    """
    # What type am I?
    if isinstance(elem, dict):
        return {urlEncodeMe(key, safe): urlEncodeMe(value, safe) for key, value in six.iteritems(elem)}
    elif isinstance(elem, list):
        return [urlEncodeMe(element, safe) for element in elem]
    elif isinstance(elem, str):
        # Leave spaces alone, they are save to travel for JSON parsing
        return urllib.quote(elem, safe)
    else:
        return elem 
Example 6
Project: openhatch   Author: campbe13   File: views.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def display_person_edit_name_do(request):
    '''Take the new first name and last name out of the POST.

    Jam them into the Django user model.'''
    # {{{
    user = request.user

    new_first = request.POST['first_name']
    new_last = request.POST['last_name']

    user.first_name = new_first
    user.last_name = new_last
    user.save()

    return HttpResponseRedirect('/people/%s' % urllib.quote(user.username))
    # }}} 
Example 7
Project: openhatch   Author: campbe13   File: views.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def invite_someone_do(request):
    remaining_invitations = (InvitationKey.objects.
                             remaining_invitations_for_user(request.user))

    form = InvitationKeyForm(data=request.POST)
    if form.is_valid():
        if remaining_invitations > 0:
            invitation = InvitationKey.objects.create_invitation(request.user)
            invitation.send_to(form.cleaned_data["email"])
            # Yay! Redirect back to invite page, with message saying who
            # was just invited.
            return HttpResponseRedirect(
                reverse(invite_someone) + '?invited=' +
                urllib.quote(form.cleaned_data['email']))
        else:  # yes, there's an email; no, the guy can't invite
            return invite_someone(request, form=form,
                                  error_message='No more invites.')
    else:
        return invite_someone(request, form=form)

# The following is copied here from django_authopenid, and then
# modified trivially in the POST handler. 
Example 8
Project: gist-alfred   Author: danielecook   File: Repository.py    MIT License 6 votes vote down vote up
def get_contents(self, path, ref=github.GithubObject.NotSet):
        """
        :calls: `GET /repos/:owner/:repo/contents/:path <http://developer.github.com/v3/repos/contents>`_
        :param path: string
        :param ref: string
        :rtype: :class:`github.ContentFile.ContentFile`
        """
        assert isinstance(path, (str, unicode)), path
        assert ref is github.GithubObject.NotSet or isinstance(ref, (str, unicode)), ref
        url_parameters = dict()
        if ref is not github.GithubObject.NotSet:
            url_parameters["ref"] = ref
        headers, data = self._requester.requestJsonAndCheck(
            "GET",
            self.url + "/contents/" + urllib.quote(path),
            parameters=url_parameters
        )
        if isinstance(data, list):
            return [
                github.ContentFile.ContentFile(self._requester, headers, item, completed=False)
                for item in data
            ]
        return github.ContentFile.ContentFile(self._requester, headers, data, completed=True) 
Example 9
Project: gist-alfred   Author: danielecook   File: Repository.py    MIT License 6 votes vote down vote up
def legacy_search_issues(self, state, keyword):
        """
        :calls: `GET /legacy/issues/search/:owner/:repository/:state/:keyword <http://developer.github.com/v3/search/legacy>`_
        :param state: "open" or "closed"
        :param keyword: string
        :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Issue.Issue`
        """
        assert state in ["open", "closed"], state
        assert isinstance(keyword, (str, unicode)), keyword
        headers, data = self._requester.requestJsonAndCheck(
            "GET",
            "/legacy/issues/search/" + self.owner.login + "/" + self.name + "/" + state + "/" + urllib.quote(keyword)
        )
        return [
            github.Issue.Issue(self._requester, headers, github.Legacy.convertIssue(element), completed=False)
            for element in data["issues"]
        ] 
Example 10
Project: Pancas   Author: Sup3Roque   File: genesisresolvers.py    GNU General Public License v2.0 6 votes vote down vote up
def resolve(self, url):
        try:
            usr = re.compile('/mail/(.+?)/').findall(url)[0]
            vid = re.compile('(\d*)[.]html').findall(url)[0]
            url = 'http://videoapi.my.mail.ru/videos/mail/%s/_myvideo/%s.json?ver=0.2.60' % (usr, vid)

            import requests
            result = requests.get(url).content
            cookie = requests.get(url).headers['Set-Cookie']

            u = json.loads(result)['videos']
            h = "|Cookie=%s" % urllib.quote(cookie)

            url = []
            try: url += [[{'quality': '1080p', 'url': i['url'] + h} for i in u if i['key'] == '1080p'][0]]
            except: pass
            try: url += [[{'quality': 'HD', 'url': i['url'] + h} for i in u if i['key'] == '720p'][0]]
            except: pass
            try: url += [[{'quality': 'SD', 'url': i['url'] + h} for i in u if not (i['key'] == '1080p' or i ['key'] == '720p')][0]]
            except: pass

            if url == []: return
            return url
        except:
            return 
Example 11
Project: tom-bot   Author: maartenberg   File: wolframalpha_plugin.py    MIT License 5 votes vote down vote up
def wolfram_cb(bot, message, *args, **kwargs):
    '''
    (Attempt to) answer query using the WolframAlpha API.

    Results may not be interpreted as you'd expect, open link for explanation.
    '''
    if not CLIENT:
        return _('Not connected to WolframAlpha!')
    query = extract_query(message)
    LOGGER.debug('Query to WolframAlpha: %s', query)
    entity = OutgoingChatstateProtocolEntity(
        ChatstateProtocolEntity.STATE_TYPING, message.getFrom())
    bot.toLower(entity)
    result = CLIENT.query(query)
    entity = OutgoingChatstateProtocolEntity(
        ChatstateProtocolEntity.STATE_PAUSED, message.getFrom())
    bot.toLower(entity)
    restext = _('Result from WolframAlpha:\n')
    results = [p.text.encode('utf-8') for p in result.pods
               if p.title in ('Result', 'Value', 'Decimal approximation', 'Exact result')]
    if not results:
        return _('No result.')
    restext += '\n'.join(results) + '\n'
    restext += 'Link: https://wolframalpha.com/input/?i={}'.format(
        urllib.quote(query).replace('%20', '+'))
    return restext 
Example 12
Project: pyblish-win   Author: pyblish   File: utils.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def encode_rfc2231(s, charset=None, language=None):
    """Encode string according to RFC 2231.

    If neither charset nor language is given, then s is returned as-is.  If
    charset is given but not language, the string is encoded using the empty
    string for language.
    """
    import urllib
    s = urllib.quote(s, safe='')
    if charset is None and language is None:
        return s
    if language is None:
        language = ''
    return "%s'%s'%s" % (charset, language, s) 
Example 13
Project: pyblish-win   Author: pyblish   File: cookielib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def escape_path(path):
    """Escape any invalid characters in HTTP URL, and uppercase all escapes."""
    # There's no knowing what character encoding was used to create URLs
    # containing %-escapes, but since we have to pick one to escape invalid
    # path characters, we pick UTF-8, as recommended in the HTML 4.0
    # specification:
    # http://www.w3.org/TR/REC-html40/appendix/notes.html#h-B.2.1
    # And here, kind of: draft-fielding-uri-rfc2396bis-03
    # (And in draft IRI specification: draft-duerst-iri-05)
    # (And here, for new URI schemes: RFC 2718)
    if isinstance(path, unicode):
        path = path.encode("utf-8")
    path = urllib.quote(path, HTTP_PATH_SAFE)
    path = ESCAPED_CHAR_RE.sub(uppercase_escaped_char, path)
    return path 
Example 14
Project: pyblish-win   Author: pyblish   File: SimpleHTTPServer.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def list_directory(self, path):
        """Helper to produce a directory listing (absent index.html).

        Return value is either a file object, or None (indicating an
        error).  In either case, the headers are sent, making the
        interface the same as for send_head().

        """
        try:
            list = os.listdir(path)
        except os.error:
            self.send_error(404, "No permission to list directory")
            return None
        list.sort(key=lambda a: a.lower())
        f = StringIO()
        displaypath = cgi.escape(urllib.unquote(self.path))
        f.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">')
        f.write("<html>\n<title>Directory listing for %s</title>\n" % displaypath)
        f.write("<body>\n<h2>Directory listing for %s</h2>\n" % displaypath)
        f.write("<hr>\n<ul>\n")
        for name in list:
            fullname = os.path.join(path, name)
            displayname = linkname = name
            # Append / for directories or @ for symbolic links
            if os.path.isdir(fullname):
                displayname = name + "/"
                linkname = name + "/"
            if os.path.islink(fullname):
                displayname = name + "@"
                # Note: a link to a directory displays with @ and links with /
            f.write('<li><a href="%s">%s</a>\n'
                    % (urllib.quote(linkname), cgi.escape(displayname)))
        f.write("</ul>\n<hr>\n</body>\n</html>\n")
        length = f.tell()
        f.seek(0)
        self.send_response(200)
        encoding = sys.getfilesystemencoding()
        self.send_header("Content-type", "text/html; charset=%s" % encoding)
        self.send_header("Content-Length", str(length))
        self.end_headers()
        return f 
Example 15
Project: pyblish-win   Author: pyblish   File: util.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def request_uri(environ, include_query=1):
    """Return the full request URI, optionally including the query string"""
    url = application_uri(environ)
    from urllib import quote
    path_info = quote(environ.get('PATH_INFO',''),safe='/;=,')
    if not environ.get('SCRIPT_NAME'):
        url += path_info[1:]
    else:
        url += path_info
    if include_query and environ.get('QUERY_STRING'):
        url += '?' + environ['QUERY_STRING']
    return url 
Example 16
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 17
Project: pyblish-win   Author: pyblish   File: robotparser.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def __init__(self, path, allowance):
        if path == '' and not allowance:
            # an empty value means allow all
            allowance = True
        path = urlparse.urlunparse(urlparse.urlparse(path))
        self.path = urllib.quote(path)
        self.allowance = allowance 
Example 18
Project: pyblish-win   Author: pyblish   File: test_urllib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_never_quote(self):
        # Make sure quote() does not quote letters, digits, and "_,.-"
        do_not_quote = '' .join(["ABCDEFGHIJKLMNOPQRSTUVWXYZ",
                                 "abcdefghijklmnopqrstuvwxyz",
                                 "0123456789",
                                 "_.-"])
        result = urllib.quote(do_not_quote)
        self.assertEqual(do_not_quote, result,
                         "using quote(): %s != %s" % (do_not_quote, result))
        result = urllib.quote_plus(do_not_quote)
        self.assertEqual(do_not_quote, result,
                        "using quote_plus(): %s != %s" % (do_not_quote, result)) 
Example 19
Project: pyblish-win   Author: pyblish   File: test_urllib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_default_safe(self):
        # Test '/' is default value for 'safe' parameter
        self.assertEqual(urllib.quote.func_defaults[0], '/') 
Example 20
Project: pyblish-win   Author: pyblish   File: test_urllib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_safe(self):
        # Test setting 'safe' parameter does what it should do
        quote_by_default = "<>"
        result = urllib.quote(quote_by_default, safe=quote_by_default)
        self.assertEqual(quote_by_default, result,
                         "using quote(): %s != %s" % (quote_by_default, result))
        result = urllib.quote_plus(quote_by_default, safe=quote_by_default)
        self.assertEqual(quote_by_default, result,
                         "using quote_plus(): %s != %s" %
                         (quote_by_default, result)) 
Example 21
Project: pyblish-win   Author: pyblish   File: test_urllib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_default_quoting(self):
        # Make sure all characters that should be quoted are by default sans
        # space (separate test for that).
        should_quote = [chr(num) for num in range(32)] # For 0x00 - 0x1F
        should_quote.append('<>#%"{}|\^[]`')
        should_quote.append(chr(127)) # For 0x7F
        should_quote = ''.join(should_quote)
        for char in should_quote:
            result = urllib.quote(char)
            self.assertEqual(hexescape(char), result,
                             "using quote(): %s should be escaped to %s, not %s" %
                             (char, hexescape(char), result))
            result = urllib.quote_plus(char)
            self.assertEqual(hexescape(char), result,
                             "using quote_plus(): "
                             "%s should be escapes to %s, not %s" %
                             (char, hexescape(char), result))
        del should_quote
        partial_quote = "ab[]cd"
        expected = "ab%5B%5Dcd"
        result = urllib.quote(partial_quote)
        self.assertEqual(expected, result,
                         "using quote(): %s != %s" % (expected, result))
        result = urllib.quote_plus(partial_quote)
        self.assertEqual(expected, result,
                         "using quote_plus(): %s != %s" % (expected, result))
        self.assertRaises(TypeError, urllib.quote, None) 
Example 22
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 23
Project: pyblish-win   Author: pyblish   File: test_urllib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_quoting(self):
        # Test automatic quoting and unquoting works for pathnam2url() and
        # url2pathname() respectively
        given = os.path.join("needs", "quot=ing", "here")
        expect = "needs/%s/here" % urllib.quote("quot=ing")
        result = urllib.pathname2url(given)
        self.assertEqual(expect, result,
                         "pathname2url() failed; %s != %s" %
                         (expect, result))
        expect = given
        result = urllib.url2pathname(result)
        self.assertEqual(expect, result,
                         "url2pathname() failed; %s != %s" %
                         (expect, result))
        given = os.path.join("make sure", "using_quote")
        expect = "%s/using_quote" % urllib.quote("make sure")
        result = urllib.pathname2url(given)
        self.assertEqual(expect, result,
                         "pathname2url() failed; %s != %s" %
                         (expect, result))
        given = "make+sure/using_unquote"
        expect = os.path.join("make+sure", "using_unquote")
        result = urllib.url2pathname(given)
        self.assertEqual(expect, result,
                         "url2pathname() failed; %s != %s" %
                         (expect, result)) 
Example 24
Project: wikilinks   Author: trovdimi   File: crawler.py    MIT License 5 votes vote down vote up
def req():
    # Get URLs from a text file, remove white space.
    db = MySQLDatabase(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME)
    db_worker_view = db.get_work_view()
    articles = db_worker_view.retrieve_all_articles()
    #articles = db_worker_view.retrieve_all_articles_questionmark()
    # measure time
    start = time.clock()
    start_time_iteration = start
    iteration_number = 483
    for i, article in enumerate(articles):
        # print some progress
        if i % 10000 == 0:
            #print time for the iteration
            seconds = time.clock() - start_time_iteration
            m, s = divmod(seconds, 60)
            h, m = divmod(m, 60)
            print "Number of crawled articles: %d. Total time for last iteration of 10000 articles: %d:%02d:%02d" % (i, h, m, s)
            start_time_iteration = time.clock()
            iteration_number += 1

        # Thread pool.
        # Blocks other threads (more than the set limit).
        pool.acquire(blocking=True)
        # Create a new thread.
        # Pass each URL (i.e. u parameter) to the worker function.
        t = threading.Thread(target=worker, args=(MEDIAWIKI_API_ENDPOINT+urllib.quote(article['title'])+'/'+str(article['rev_id']), article, iteration_number))

        # Start the newly create thread.
        t.start()
    seconds = time.clock() - start
    m, s = divmod(seconds, 60)
    h, m = divmod(m, 60)
    print "Total time: %d:%02d:%02d" % (h, m, s) 
Example 25
Project: azure-iot-rest   Author: jongio   File: module-twin.py    MIT License 5 votes vote down vote up
def get_iot_hub_sas_token(uri, key, policy_name, expiry=3600):
    ttl = time() + expiry
    sign_key = "%s\n%d" % ((quote(uri)), int(ttl))
    signature = b64encode(HMAC(b64decode(key), sign_key.encode('utf-8'), sha256).digest())

    rawtoken = {
        'sr' :  uri,
        'sig': signature,
        'se' : str(int(ttl))
    }

    if policy_name is not None:
        rawtoken['skn'] = policy_name

    return 'SharedAccessSignature ' + urlencode(rawtoken) 
Example 26
Project: azure-iot-rest   Author: jongio   File: modules.py    MIT License 5 votes vote down vote up
def get_iot_hub_sas_token(uri, key, policy_name, expiry=3600):
    ttl = time() + expiry
    sign_key = "%s\n%d" % ((quote(uri)), int(ttl))
    signature = b64encode(HMAC(b64decode(key), sign_key.encode('utf-8'), sha256).digest())

    rawtoken = {
        'sr' :  uri,
        'sig': signature,
        'se' : str(int(ttl))
    }

    if policy_name is not None:
        rawtoken['skn'] = policy_name

    return 'SharedAccessSignature ' + urlencode(rawtoken) 
Example 27
Project: azure-iot-rest   Author: jongio   File: device-conf.py    MIT License 5 votes vote down vote up
def get_iot_hub_sas_token(uri, key, policy_name, expiry=3600):
    ttl = time() + expiry
    sign_key = "%s\n%d" % ((quote(uri)), int(ttl))
    signature = b64encode(HMAC(b64decode(key), sign_key.encode('utf-8'), sha256).digest())

    rawtoken = {
        'sr' :  uri,
        'sig': signature,
        'se' : str(int(ttl))
    }

    if policy_name is not None:
        rawtoken['skn'] = policy_name

    return 'SharedAccessSignature ' + urlencode(rawtoken) 
Example 28
Project: slidoc   Author: mitotic   File: sdserver.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def viewer_link(self, relpath, site_admin=False):
        # Returns (linkpath, viewpath) quoted URL links to restricted file with key appended as query parameter, given relative path
        fext = os.path.splitext(relpath)[1]
        toppath = '/' + relpath
        if Options['site_name']:
            toppath = '/' + Options['site_name'] + toppath

        linkpath = urllib.quote(relpath)

        files_path = relpath.startswith(FILES_PATH+'/')

        if not (files_path or site_admin):
            return linkpath, linkpath

        # files_path or site_admin: generate viewable links
        viewpath = urllib.quote(toppath)
        if ('/'+RESTRICTED_PATH) in relpath:
            # Restricted _files; viewable by anyone with file key in link
            fileKey = '?' + PluginManager.getFileKey(toppath)

            viewpath += fileKey
            linkpath += fileKey

            if fext.lower() == '.ipynb':
                # Render link for notebooks
                if Options['server_url'].startswith('https://'):
                    nbprefix = 'https://nbviewer.jupyter.org/urls/' + Options['server_url'][len('https://'):]
                elif Options['server_url'].startswith('http://'):
                    nbprefix = 'http://nbviewer.jupyter.org/url/' + Options['server_url'][len('http://'):]
                else:
                    raise Exception('Invalid server URL :' + Options['server_url'])

                # Append "query" using "%3F" as nbviewer chomps up normal queries
                viewpath = nbprefix + viewpath.replace('?', '%3F')

        return linkpath, viewpath 
Example 29
Project: slidoc   Author: mitotic   File: md2md.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def make_id_from_text(text):
    """Make safe ID string from string"""
    return urllib.quote(re.sub(r'[^-\w\.]+', '-', text.lower().strip()).strip('-').strip('.'), safe='') 
Example 30
Project: slidoc   Author: mitotic   File: sliauth.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def safe_quote(value):
    return urllib.quote(str_encode(value), safe='') 
Example 31
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 32
Project: AirPCrawler   Author: penetest   File: Spider_NanH_demo_win.py    MIT License 5 votes vote down vote up
def data_Crawling(from_h,to_h,date_h):
    s = requests.Session()
    start_url = 'http://b2c.csair.com/B2C40/modules/bookingnew/main/flightSelectDirect.html?t=S&c1='+from_h+'&c2='+to_h+'&d1='+date_h+'&at=1&ct=0&it=0&preUrl=360BUY'
    h2 = {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
          'Accept-Encoding':'gzip, deflate, sdch',
          'Accept-Language':'zh-CN,zh;q=0.8',
          'X-Requested-With':'XMLHttpRequest',
          #Cookie:Webtrends=111.160.254.58.1464136223732083; BIGipServerpool_sc_122.119.122.51=830109562.20480.0000; JSESSIONID=0000M2suWXhIhzAP_5BzKDkE78S:1a5jgnqlj; OZ_1U_671=vid=v744f3c10a3ea8.0&ctime=1464140663&ltime=1464140662; OZ_1Y_671=erefer=-&eurl=http%3A//sc.travelsky.com/scet/queryAv.do%3Flan%3Dcn%26countrytype%3D0%26travelType%3D0%26cityNameOrg%3D%25E5%258C%2597%25E4%25BA%25AC%26cityCodeOrg%3DPEK%26cityNameDes%3D%25E5%258E%25A6%25E9%2597%25A8%26cityCodeDes%3DXMN%26takeoffDate%3D2016-05-28%26returnDate%3D2016-05-28%26cabinStage%3D0%26adultNum%3D1%26childNum%3D0&etime=1464139715&ctime=1464140663&ltime=1464140662&compid=671
          'Referer':start_url,
          'Origin':'http://b2c.csair.com',
          'Host':'b2c.csair.com',
          'Proxy-Connection':'keep-alive',
          'Upgrade-Insecure-Requests':1,
          'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36'
          }
    date_h = date_h.replace('-','')
    data = '{"depcity":"'+from_h+'", "arrcity":"'+to_h+'", "flightdate":"'+date_h+'", "adultnum":"1", "childnum":"0", "infantnum":"0", "cabinorder":"0","airline":"1", "flytype":"0", "international":"0", "action":"0", "segtype":"1", "cache":"0", "preUrl":"360BUY", "isMember":""}'
    data = urllib.quote(data)
    res_url ='http://b2c.csair.com/B2C40/query/jaxb/direct/query.ao?json='+data
    try:
        re =s.get(start_url,headers = h1,timeout = 15)
        resp1 = s.post(res_url,headers = h2,timeout = 15)
        return resp1.text,resp1.status_code
    except requests.exceptions.ReadTimeout:
        return -1,-1
    except requests.exceptions.ConnectionError:
        return -2,-2
    except AttributeError:
        #print resp.text
        return -1,-1
# 解析数据 
Example 33
Project: AirPCrawler   Author: penetest   File: Spider_NanH_demo.py    MIT License 5 votes vote down vote up
def data_Crawling(from_h,to_h,date_h):
    s = requests.Session()
    start_url = 'http://b2c.csair.com/B2C40/modules/bookingnew/main/flightSelectDirect.html?t=S&c1='+from_h+'&c2='+to_h+'&d1='+date_h+'&at=1&ct=0&it=0&preUrl=360BUY'
    h2 = {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
          'Accept-Encoding':'gzip, deflate, sdch',
          'Accept-Language':'zh-CN,zh;q=0.8',
          'X-Requested-With':'XMLHttpRequest',
          #Cookie:Webtrends=111.160.254.58.1464136223732083; BIGipServerpool_sc_122.119.122.51=830109562.20480.0000; JSESSIONID=0000M2suWXhIhzAP_5BzKDkE78S:1a5jgnqlj; OZ_1U_671=vid=v744f3c10a3ea8.0&ctime=1464140663&ltime=1464140662; OZ_1Y_671=erefer=-&eurl=http%3A//sc.travelsky.com/scet/queryAv.do%3Flan%3Dcn%26countrytype%3D0%26travelType%3D0%26cityNameOrg%3D%25E5%258C%2597%25E4%25BA%25AC%26cityCodeOrg%3DPEK%26cityNameDes%3D%25E5%258E%25A6%25E9%2597%25A8%26cityCodeDes%3DXMN%26takeoffDate%3D2016-05-28%26returnDate%3D2016-05-28%26cabinStage%3D0%26adultNum%3D1%26childNum%3D0&etime=1464139715&ctime=1464140663&ltime=1464140662&compid=671
          'Referer':start_url,
          'Origin':'http://b2c.csair.com',
          'Host':'b2c.csair.com',
          'Proxy-Connection':'keep-alive',
          'Upgrade-Insecure-Requests':1,
          'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36'
          }
    date_h = date_h.replace('-','')
    data = '{"depcity":"'+from_h+'", "arrcity":"'+to_h+'", "flightdate":"'+date_h+'", "adultnum":"1", "childnum":"0", "infantnum":"0", "cabinorder":"0","airline":"1", "flytype":"0", "international":"0", "action":"0", "segtype":"1", "cache":"0", "preUrl":"360BUY", "isMember":""}'
    data = urllib.quote(data)
    res_url ='http://b2c.csair.com/B2C40/query/jaxb/direct/query.ao?json='+data
    try:
        re =s.get(start_url,headers = h1,timeout = 15)
        resp1 = s.post(res_url,headers = h2,timeout = 15)
        return resp1.text,resp1.status_code
    except requests.exceptions.ReadTimeout:
        return -1,-1
    except requests.exceptions.ConnectionError:
        return -2,-2
    except AttributeError:
        #print resp.text
        return -1,-1
# 解析数据 
Example 34
Project: AirPCrawler   Author: penetest   File: Spider_ChuanH_demo.py    MIT License 5 votes vote down vote up
def data_Crawling(from_h,to_h,date_h,i_orgcity, i_dstCity):
    s = requests.Session()
    data = '{"AirlineType":"Single","IsFixedCabin":false,"RouteList":[{"RouteIndex":1,"RouteName":"单    程","OrgCity":"'+from_h+'","DesCity":"'+to_h+'","OrgCityName":"'+i_orgcity+'","DesCityName":"'+i_dstCity+'","FlightDate":"'+date_h+'"}],"AVType":0}'
    #data = urllib.urlencode(data)
    data = urllib.quote(data)
    data = 'http://www.scal.com.cn/Web/ETicket/AirlineList?AirlineParamJSON='+data
    try:
        resp = s.post(data,timeout=15)
        url = re.search(r'arrPageValue.AirlineParamJSON = (.*?);',resp.text)
        #print url.group(1)
        data = json.loads( url.group(1))
        #print(data)
        data1 = re.search(r'arrPageValue.AirlineParamJSON = .*\[(.*?)\].*;',resp.text).group(1).replace("}","")
        #data1 = str(data['RouteList'][0]).replace("}","")
        data11 = data['AirlineType']
        data1 += ',\"AirlineType\":\"'+data11+'\"'
        data11 = data['AVType']
        data1 += ',\"AVType\":'+str(data11)
        data1 += ',\"CardFlag\":null'
        data11 = data['Flag']
        data1 += ',\"Flag\":null'
        data11 = data['BuyerType']
        data1 += ',\"BuyerType\":'+str(data11)
        data11 = data['IsFixedCabin']
        data1 += ',\"IsFixedCabin\":'+str(data11).lower()
        data11 = data['PassKey']
        data1 += ',\"PassKey\":\"'+data11+'\"}'
        data1 = json.loads(data1)
        resp1 = s.post('http://www.scal.com.cn/Web/ETicket/GetSingleChina',json=data1)
        return resp1.text,resp1.status_code
    except requests.exceptions.ReadTimeout:
        return -1,-1
    except requests.exceptions.ConnectionError:
        return -2,-2
    except AttributeError:
        #print resp.text
        return -1,-1
# 解析数据 
Example 35
Project: AirPCrawler   Author: penetest   File: Spider_ChuanH_demo_win.py    MIT License 5 votes vote down vote up
def data_Crawling(from_h,to_h,date_h,i_orgcity, i_dstCity):
    s = requests.Session()
    data = '{"AirlineType":"Single","IsFixedCabin":false,"RouteList":[{"RouteIndex":1,"RouteName":"单    程","OrgCity":"'+from_h+'","DesCity":"'+to_h+'","OrgCityName":"'+i_orgcity+'","DesCityName":"'+i_dstCity+'","FlightDate":"'+date_h+'"}],"AVType":0}'
    #data = urllib.urlencode(data)
    data = urllib.quote(data)
    data = 'http://www.scal.com.cn/Web/ETicket/AirlineList?AirlineParamJSON='+data
    try:
        resp = s.post(data,timeout=15)
        url = re.search(r'arrPageValue.AirlineParamJSON = (.*?);',resp.text)
        #print url.group(1)
        data = json.loads( url.group(1))
        #print(data)
        data1 = re.search(r'arrPageValue.AirlineParamJSON = .*\[(.*?)\].*;',resp.text).group(1).replace("}","")
        #data1 = str(data['RouteList'][0]).replace("}","")
        data11 = data['AirlineType']
        data1 += ',\"AirlineType\":\"'+data11+'\"'
        data11 = data['AVType']
        data1 += ',\"AVType\":'+str(data11)
        data1 += ',\"CardFlag\":null'
        data11 = data['Flag']
        data1 += ',\"Flag\":null'
        data11 = data['BuyerType']
        data1 += ',\"BuyerType\":'+str(data11)
        data11 = data['IsFixedCabin']
        data1 += ',\"IsFixedCabin\":'+str(data11).lower()
        data11 = data['PassKey']
        data1 += ',\"PassKey\":\"'+data11+'\"}'
        data1 = json.loads(data1)
        resp1 = s.post('http://www.scal.com.cn/Web/ETicket/GetSingleChina',json=data1)
        return resp1.text,resp1.status_code
    except requests.exceptions.ReadTimeout:
        return -1,-1
    except requests.exceptions.ConnectionError:
        return -2,-2
    except AttributeError:
        #print resp.text
        return -1,-1
# 解析数据 
Example 36
Project: Splunk_CBER_App   Author: MHaggis   File: binding.py    MIT License 5 votes vote down vote up
def __new__(self, val='', skip_encode=False, encode_slash=False):
        if isinstance(val, UrlEncoded):
            # Don't urllib.quote something already URL encoded.
            return val
        elif skip_encode:
            return str.__new__(self, val)
        elif encode_slash:
            return str.__new__(self, urllib.quote_plus(val))
        else:
            # When subclassing str, just call str's __new__ method
            # with your class and the value you want to have in the
            # new string.
            return str.__new__(self, urllib.quote(val)) 
Example 37
Project: Splunk_CBER_App   Author: MHaggis   File: binding.py    MIT License 5 votes vote down vote up
def __add__(self, other):
        """self + other

        If *other* is not a ``UrlEncoded``, URL encode it before
        adding it.
        """
        if isinstance(other, UrlEncoded):
            return UrlEncoded(str.__add__(self, other), skip_encode=True)
        else:
            return UrlEncoded(str.__add__(self, urllib.quote(other)), skip_encode=True) 
Example 38
Project: Splunk_CBER_App   Author: MHaggis   File: binding.py    MIT License 5 votes vote down vote up
def __radd__(self, other):
        """other + self

        If *other* is not a ``UrlEncoded``, URL _encode it before
        adding it.
        """
        if isinstance(other, UrlEncoded):
            return UrlEncoded(str.__radd__(self, other), skip_encode=True)
        else:
            return UrlEncoded(str.__add__(urllib.quote(other), self), skip_encode=True) 
Example 39
Project: Splunk_CBER_App   Author: MHaggis   File: cbapi.py    MIT License 5 votes vote down vote up
def watchlist_modify(self, id, watchlist):
        """
        updates a watchlist
        """
        url = "%s/api/v1/watchlist/%s" % (self.server, id)
        watchlist['search_query'] = urllib.quote(watchlist['search_query'])
        # ensure that it starts with the proper url parameters
        if not watchlist['search_query'].startswith("cb.urlver=1&q="):
            watchlist['search_query'] = "cb.urlver=1&q=" + watchlist['search_query']
        r = self.cbapi_put(url, data=json.dumps(watchlist))
        r.raise_for_status()

        return r.json() 
Example 40
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_instance_usage_audit_log.py    Apache License 2.0 5 votes vote down vote up
def test_show_instance_usage_audit_log(self):
        response = self._do_get('os-instance_usage_audit_log/%s' %
                                urllib.quote('2012-07-05 10:00:00'))
        self._verify_response('inst-usage-audit-log-show-get-resp',
                              {}, response, 200) 
Example 41
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_floating_ip_dns.py    Apache License 2.0 5 votes vote down vote up
def _quote_domain(domain):
    """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.  This function needs to duplicate the one in
    python-novaclient/novaclient/v1_1/floating_ip_dns.py
    """
    return urllib.quote(domain.replace('.', '%2E')) 
Example 42
Project: AshsSDK   Author: thehappydinoa   File: sigV4Core.py    MIT License 5 votes vote down vote up
def createWebsocketEndpoint(self, host, port, region, method, awsServiceName, path):
        # Return the endpoint as unicode string in 3.x
        # Gather all the facts
        amazonDate = self._createAmazonDate()
        amazonDateSimple = amazonDate[0]  # Unicode in 3.x
        amazonDateComplex = amazonDate[1]  # Unicode in 3.x
        allKeys = self._checkIAMCredentials()  # Unicode in 3.x
        hasCredentialsNecessaryForWebsocket = "aws_access_key_id" in allKeys.keys() and "aws_secret_access_key" in allKeys.keys()
        if not hasCredentialsNecessaryForWebsocket:
            return ""
        else:
            keyID = allKeys["aws_access_key_id"]
            secretKey = allKeys["aws_secret_access_key"]
            queryParameters = "X-Amz-Algorithm=AWS4-HMAC-SHA256" + \
                "&X-Amz-Credential=" + keyID + "%2F" + amazonDateSimple + "%2F" + region + "%2F" + awsServiceName + "%2Faws4_request" + \
                "&X-Amz-Date=" + amazonDateComplex + \
                "&X-Amz-Expires=86400" + \
                "&X-Amz-SignedHeaders=host"  # Unicode in 3.x
            hashedPayload = hashlib.sha256(str("").encode('utf-8')).hexdigest()  # Unicode in 3.x
            # Create the string to sign
            signedHeaders = "host"
            canonicalHeaders = "host:" + host + "\n"
            canonicalRequest = method + "\n" + path + "\n" + queryParameters + "\n" + canonicalHeaders + "\n" + signedHeaders + "\n" + hashedPayload  # Unicode in 3.x
            hashedCanonicalRequest = hashlib.sha256(str(canonicalRequest).encode('utf-8')).hexdigest()  # Unicoede in 3.x
            stringToSign = "AWS4-HMAC-SHA256\n" + amazonDateComplex + "\n" + amazonDateSimple + "/" + region + "/" + awsServiceName + "/aws4_request\n" + hashedCanonicalRequest  # Unicode in 3.x
            # Sign it
            signingKey = self._getSignatureKey(secretKey, amazonDateSimple, region, awsServiceName)
            signature = hmac.new(signingKey, (stringToSign).encode("utf-8"), hashlib.sha256).hexdigest()
            # generate url
            url = "wss://" + host + ":" + str(port) + path + '?' + queryParameters + "&X-Amz-Signature=" + signature
            # See if we have STS token, if we do, add it
            if "aws_session_token" in allKeys.keys():
                aws_session_token = allKeys["aws_session_token"]
                url += "&X-Amz-Security-Token=" + quote(aws_session_token.encode("utf-8"))  # Unicode in 3.x
            self._logger.debug("createWebsocketEndpoint: Websocket URL: " + url)
            return url 
Example 43
Project: zufang   Author: facert   File: app.py    MIT License 5 votes vote down vote up
def reply_all(**kwargs):
    username = kwargs.get('sender')
    sender = kwargs.get('receiver')
    message_type = kwargs.get('type')
    content = kwargs.get('content', message_type)
    results = zufang_query(' OR '.join(content.strip().split(' ')), limit=6)
    result_list = []
    if results:
        for result in results:
            result_list.append('<a href="%s">%s</a>\n%s\n====================\n' % (result['url'], result['title'].strip(), result['create_time']))
        result_list.append('<a href="http://zufang.ngrok.cc/?keywords=%s">%s</a>' % (quote(content.encode('utf-8')), u'更多【%s】租房请点击这里' % content))
    else:
        result_list = [u'暂时没有【%s】房源哦' % content]
    return weixin.reply(username, sender=sender, content=''.join(result_list)) 
Example 44
Project: openhatch   Author: campbe13   File: unicode_sanity.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def quote(str):
    return urllib.quote(str.encode('utf-8')) 
Example 45
Project: openhatch   Author: campbe13   File: ohloh.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def project_id2projectdata(self, project_id=None, project_name=None):
        if project_name is None:
            project_query = str(int(project_id))
        else:
            project_query = str(project_name)
        url = 'http://www.ohloh.net/projects/%s.xml?' % urllib.quote(
            project_query)
        data, web_response = ohloh_url2data(url=url, selector='result/project')
        return data 
Example 46
Project: openhatch   Author: campbe13   File: views.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def signup_do(request):
    post = {}
    post.update(dict(request.POST.items()))
    post['password2'] = post.get('password1', '')
    signup_form = mysite.account.forms.UserCreationFormWithEmail(post)
    if signup_form.is_valid():

        user = signup_form.save()

        username = request.POST['username']
        password = request.POST['password1']

        # authenticate and login
        user = django.contrib.auth.authenticate(
            username=username,
            password=password)
        django.contrib.auth.login(request, user)

        # redirect to profile or the page that brought the user to signup page
        next = '/people/%s/' % urllib.quote(username)
        if request.POST.get('next'):
            next = request.POST['next']
        return HttpResponseRedirect(next)

    else:
        return mysite.account.views.signup(request, signup_form=signup_form) 
Example 47
Project: openhatch   Author: campbe13   File: feedparser.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def _urljoin(base, uri):
    uri = _urifixer.sub(r'\1\3', uri)
    try:
        return urlparse.urljoin(base, uri)
    except:
        uri = urlparse.urlunparse([urllib.quote(part) for part in urlparse.urlparse(uri)])
        return urlparse.urljoin(base, uri) 
Example 48
Project: bitpay-brick   Author: javgh   File: frontend.py    MIT License 5 votes vote down vote up
def show_invoice(self, bitcoin_uri):
        (_, size, img) = qrencode.encode_scaled(bitcoin_uri, QR_CODE_SIZE)
        buf = StringIO()
        img.save(buf, format='PNG')
        image_data = "data:image/png,%s" % urllib.quote(buf.getvalue())
        self._evaluate_java_script('show_invoice("%s")' % image_data) 
Example 49
Project: domain_discovery_API   Author: VIDA-NYU   File: runSeedFinder.py    GNU General Public License v3.0 5 votes vote down vote up
def execSeedFinder(self, terms, data_path, updateStatusCB, shouldTerminateCB, es_info):
        print "\n\n\n EXEC SEED FINDER", terms, " ", data_path, " \n\n\n"
        domain_name = es_info['activeDomainIndex']
  
        data_dir = data_path + "/data/"
        data_crawler  = data_dir + domain_name
        data_training = data_crawler + "/training_data/"
    
        crawlermodel_dir = data_crawler + "/models/"

        if (not isdir(crawlermodel_dir)):
            return

        seed_dir = data_crawler + "/seedFinder/"
    
        if (not isdir(seed_dir)):
            # Create dir if it does not exist
            makedirs(seed_dir)

        if (not isdir(seed_dir+"log")):
            makedirs(seed_dir+"log")
    
        csv_file = seed_dir + terms.replace(" ","_") + "_results.csv"
        
        if isfile(csv_file):
            remove(csv_file)

        ache_home = environ['ACHE_HOME']
        
        comm = ache_home + "/bin/ache seedFinder --csvPath " + csv_file + " --initialQuery \"" +terms + "\" --modelPath " + crawlermodel_dir + " --seedsPath " + seed_dir + " --maxPages 2 --maxQueries 25"

        encoded_query = urllib.quote(terms).replace("%5C","")

        f_sf_log = open(seed_dir+"log/seeds_"+"+".join(encoded_query.split("%20"))+".log", 'w')
        f_sf_err_log = open(seed_dir+"log/seeds_"+"+".join(encoded_query.split("%20"))+"_error.log", 'w')

        if not shouldTerminateCB(terms):
            p = Popen(comm, shell=True, stderr=f_sf_err_log, stdout=f_sf_log)
            updateStatusCB(terms, "terminate_process", p)
            # Upload seeds generated by the SeedFinder
            CollectSeeds(terms, csv_file, es_info).collect_seed_urls(updateStatusCB, shouldTerminateCB, p) 
Example 50
Project: cottontail   Author: QKaiser   File: rabbitmq_management.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_definitions(self, vhost=None):
        """
        The server definitions - exchanges, queues, bindings, users,
        virtual hosts, permissions and parameters.
        Everything apart from messages.
        """
        if vhost is not None:
            return self.get_request("definitions/{}".format(
                quote(vhost, safe='')))
        return self.get_request("definitions")