Python urllib.parse.quote() Examples

The following are 30 code examples of urllib.parse.quote(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module urllib.parse , or try the search function .
Example #1
Source File: diff_match_patch.py    From tandem with Apache License 2.0 6 votes vote down vote up
def diff_toDelta(self, diffs):
        """Crush the diff into an encoded string which describes the operations
        required to transform text1 into text2.
        E.g. =3\t-2\t+ing  -> Keep 3 chars, delete 2 chars, insert 'ing'.
        Operations are tab-separated.  Inserted text is escaped using %xx notation.

        Args:
            diffs: Array of diff tuples.

        Returns:
            Delta text.
        """
        text = []
        for (op, data) in diffs:
            if op == self.DIFF_INSERT:
                # High ascii will raise UnicodeDecodeError.  Use Unicode instead.
                data = data.encode("utf-8")
                text.append("+" + parse.quote(data, "!~*'();/?:@&=+$,# "))
            elif op == self.DIFF_DELETE:
                text.append("-%d" % len(data))
            elif op == self.DIFF_EQUAL:
                text.append("=%d" % len(data))
        return "\t".join(text) 
Example #2
Source File: qrcode.py    From wechatpy with MIT License 6 votes vote down vote up
def get_url(cls, ticket):
        """
        通过ticket换取二维码地址
        详情请参考
        https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1443433542

        :param ticket: 二维码 ticket 。可以通过 :func:`create` 获取到
        :return: 返回的二维码地址

        使用示例::

            from wechatpy import WeChatClient

            client = WeChatClient('appid', 'secret')
            url = client.qrcode.get_url('ticket data')

        """
        if isinstance(ticket, dict):
            ticket = ticket["ticket"]
        ticket = quote(ticket)
        return f"https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={ticket}" 
Example #3
Source File: test_encoding.py    From cherrypy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_query_string_decoding(self):
        URI_TMPL = '/reqparams?q={q}'

        europoundUtf8_2_bytes = europoundUnicode.encode('utf-8')
        europoundUtf8_2nd_byte = europoundUtf8_2_bytes[1:2]

        # Encoded utf8 query strings MUST be parsed correctly.
        # Here, q is the POUND SIGN U+00A3 encoded in utf8 and then %HEX
        self.getPage(URI_TMPL.format(q=url_quote(europoundUtf8_2_bytes)))
        # The return value will be encoded as utf8.
        self.assertBody(b'q: ' + europoundUtf8_2_bytes)

        # Query strings that are incorrectly encoded MUST raise 404.
        # Here, q is the second byte of POUND SIGN U+A3 encoded in utf8
        # and then %HEX
        # TODO: check whether this shouldn't raise 400 Bad Request instead
        self.getPage(URI_TMPL.format(q=url_quote(europoundUtf8_2nd_byte)))
        self.assertStatus(404)
        self.assertErrorPage(
            404,
            'The given query string could not be processed. Query '
            "strings for this resource must be encoded with 'utf8'.") 
Example #4
Source File: oauth.py    From wechatpy with MIT License 6 votes vote down vote up
def authorize_url(self):
        """获取授权跳转地址

        :return: URL 地址
        """
        redirect_uri = quote(self.redirect_uri, safe=b"")
        url_list = [
            self.OAUTH_BASE_URL,
            "oauth2/authorize?appid=",
            self.app_id,
            "&redirect_uri=",
            redirect_uri,
            "&response_type=code&scope=",
            self.scope,
        ]
        if self.state:
            url_list.extend(["&state=", self.state])
        url_list.append("#wechat_redirect")
        return "".join(url_list) 
Example #5
Source File: component.py    From wechatpy with MIT License 6 votes vote down vote up
def get_authorize_url(self, redirect_uri, scope="snsapi_base", state=""):
        """

        :param redirect_uri: 重定向地址,需要urlencode,这里填写的应是服务开发方的回调地址
        :param scope: 可选,微信公众号 OAuth2 scope,默认为 ``snsapi_base``
        :param state: 可选,重定向后会带上state参数,开发者可以填写任意参数值,最多128字节
        """
        redirect_uri = quote(redirect_uri, safe=b"")
        url_list = [
            self.OAUTH_BASE_URL,
            "oauth2/authorize?appid=",
            self.app_id,
            "&redirect_uri=",
            redirect_uri,
            "&response_type=code&scope=",
            scope,
        ]
        if state:
            url_list.extend(["&state=", state])
        url_list.extend(
            ["&component_appid=", self.component.component_appid,]
        )
        url_list.append("#wechat_redirect")
        return "".join(url_list) 
Example #6
Source File: oauth.py    From wechatpy with MIT License 6 votes vote down vote up
def authorize_url(self, redirect_uri, state=None):
        """
        构造网页授权链接
        详情请参考
        https://work.weixin.qq.com/api/doc#90000/90135/91022

        :param redirect_uri: 授权后重定向的回调链接地址
        :param state: 重定向后会带上 state 参数
        :return: 返回的 JSON 数据包
        """
        redirect_uri = quote(redirect_uri, safe=b"")
        url_list = [
            self.OAUTH_BASE_URL,
            "?appid=",
            self._client.corp_id,
            "&redirect_uri=",
            redirect_uri,
            "&response_type=code&scope=snsapi_base",
        ]
        if state:
            url_list.extend(["&state=", state])
        url_list.append("#wechat_redirect")
        return "".join(url_list) 
Example #7
Source File: image_helper.py    From JJMumbleBot with GNU General Public License v3.0 6 votes vote down vote up
def format_image_html(img_ext, byte_arr):
    if img_ext == "jpg":
        img_ext = "JPEG"
    elif img_ext == "jpeg":
        img_ext = "JPEG"
    elif img_ext == "png":
        img_ext = "PNG"

    raw_base = encode_b64(byte_arr)
    encoded = []
    i = 0

    begin = i * 72
    end = i * 72
    mid_raw_base = mid(raw_base, begin, 72)
    encoded.append(quote(mid_raw_base, safe=''))
    i += 1
    while end < len(raw_base):
        begin = i * 72
        end = i * 72
        mid_raw_base = mid(raw_base, begin, 72)
        encoded.append(quote(mid_raw_base, safe=''))
        i += 1
    return f"<img src='data:image/{img_ext};base64,{''.join(encoded)}' />" 
Example #8
Source File: test_redirect.py    From sanic with MIT License 6 votes vote down vote up
def test_redirect_with_params(app, test_str):
    use_in_uri = quote(test_str)

    @app.route("/api/v1/test/<test>/")
    async def init_handler(request, test):
        return redirect(f"/api/v2/test/{use_in_uri}/")

    @app.route("/api/v2/test/<test>/")
    async def target_handler(request, test):
        assert test == test_str
        return text("OK")

    _, response = app.test_client.get(f"/api/v1/test/{use_in_uri}/")
    assert response.status == 200

    assert response.content == b"OK" 
Example #9
Source File: nzbmonkey.py    From nzb-monkey with MIT License 6 votes vote down vote up
def search_nzb_url(self):
        """Search for NZB Download URL and return the URL
        :return bool, str: """
        try:
            self.header = self.header.replace('_', ' ')
            res = requests.get(self.search_url.format(quote(self.header, encoding='utf-8')),
                               timeout=REQUESTS_TIMEOUT, headers={'Cookie': 'agreed=true'}, verify=False)
        except requests.exceptions.Timeout:
            print(Col.WARN + ' Timeout' + Col.OFF, flush=True)
            return False, None
        except requests.exceptions.ConnectionError:
            print(Col.WARN + ' Connection Error' + Col.OFF, flush=True)
            return False, None

        m = re.search(self.regex, res.text, re.DOTALL)
        if m is None:
            print(Col.WARN + ' NOT FOUND' + Col.OFF, flush=True)
            return False, None

        self.nzb_url = self.download_url.format(**m.groupdict())

        return True, self.nzb_url 
Example #10
Source File: net.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def validate_(self, value, context=None):
        url = self.valid_url(value)
        if not url:
            raise StopValidationError(self.messages['invalid_url'])
        if self.verify_exists:
            url_string = urlquote(urlunsplit((
                url['scheme'],
                (url['host6'] or url['host4'] or url['hostn_enc']) + ':' + (url['port'] or ''),
                url['path'],
                url['query'],
                url['frag'])
                ).encode('utf-8'), safe=VALID_CHAR_STRING)
            try:
                urlopen(url_string)
            except URLError:
                raise StopValidationError(self.messages['not_found']) 
Example #11
Source File: splunk_rest_client.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _get_proxy_info(context):
    if not context.get('proxy_hostname') or not context.get('proxy_port'):
        return None

    user_pass = ''
    if context.get('proxy_username') and context.get('proxy_password'):
        username = quote(context['proxy_username'], safe='')
        password = quote(context['proxy_password'], safe='')
        user_pass = '{user}:{password}@'.format(
            user=username, password=password)

    proxy = 'http://{user_pass}{host}:{port}'.format(
        user_pass=user_pass, host=context['proxy_hostname'],
        port=context['proxy_port'])
    proxies = {
        'http': proxy,
        'https': proxy,
    }
    return proxies 
Example #12
Source File: splunk_rest_client.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _get_proxy_info(context):
    if not context.get('proxy_hostname') or not context.get('proxy_port'):
        return None

    user_pass = ''
    if context.get('proxy_username') and context.get('proxy_password'):
        username = quote(context['proxy_username'], safe='')
        password = quote(context['proxy_password'], safe='')
        user_pass = '{user}:{password}@'.format(
            user=username, password=password)

    proxy = 'http://{user_pass}{host}:{port}'.format(
        user_pass=user_pass, host=context['proxy_hostname'],
        port=context['proxy_port'])
    proxies = {
        'http': proxy,
        'https': proxy,
    }
    return proxies 
Example #13
Source File: oauth.py    From wechatpy with MIT License 6 votes vote down vote up
def qrconnect_url(self):
        """生成扫码登录地址

        :return: URL 地址
        """
        redirect_uri = quote(self.redirect_uri, safe=b"")
        url_list = [
            self.OAUTH_BASE_URL,
            "qrconnect?appid=",
            self.app_id,
            "&redirect_uri=",
            redirect_uri,
            "&response_type=code&scope=",
            "snsapi_login",  # scope
        ]
        if self.state:
            url_list.extend(["&state=", self.state])
        url_list.append("#wechat_redirect")
        return "".join(url_list) 
Example #14
Source File: diff_match_patch.py    From tandem with Apache License 2.0 6 votes vote down vote up
def diff_toDelta(self, diffs):
        """Crush the diff into an encoded string which describes the operations
        required to transform text1 into text2.
        E.g. =3\t-2\t+ing  -> Keep 3 chars, delete 2 chars, insert 'ing'.
        Operations are tab-separated.  Inserted text is escaped using %xx notation.

        Args:
            diffs: Array of diff tuples.

        Returns:
            Delta text.
        """
        text = []
        for (op, data) in diffs:
            if op == self.DIFF_INSERT:
                # High ascii will raise UnicodeDecodeError.  Use Unicode instead.
                data = data.encode("utf-8")
                text.append("+" + parse.quote(data, "!~*'();/?:@&=+$,# "))
            elif op == self.DIFF_DELETE:
                text.append("-%d" % len(data))
            elif op == self.DIFF_EQUAL:
                text.append("=%d" % len(data))
        return "\t".join(text) 
Example #15
Source File: helper.py    From calibre-web with GNU General Public License v3.0 6 votes vote down vote up
def get_download_link(book_id, book_format, client):
    book_format = book_format.split(".")[0]
    book = calibre_db.get_filtered_book(book_id)
    if book:
        data1 = calibre_db.get_book_format(book.id, book_format.upper())
    else:
        abort(404)
    if data1:
        # collect downloaded books only for registered user and not for anonymous user
        if current_user.is_authenticated:
            ub.update_download(book_id, int(current_user.id))
        file_name = book.title
        if len(book.authors) > 0:
            file_name = book.authors[0].name + '_' + file_name
        file_name = get_valid_filename(file_name)
        headers = Headers()
        headers["Content-Type"] = mimetypes.types_map.get('.' + book_format, "application/octet-stream")
        headers["Content-Disposition"] = "attachment; filename=%s.%s; filename*=UTF-8''%s.%s" % (
            quote(file_name.encode('utf-8')), book_format, quote(file_name.encode('utf-8')), book_format)
        return do_download_file(book, book_format, client, data1, headers)
    else:
        abort(404) 
Example #16
Source File: upload.py    From Auto_Record_Matsuri with MIT License 6 votes vote down vote up
def upload_video(video_dict, user_config):
    upload_way_dict = {'bd': BDUpload,
                       's3': S3Upload}
    upload_way = upload_way_dict.get(config['upload_by'])
    uploader = upload_way()
    ddir = get_ddir(user_config)
    uploader.upload_item(f"{ddir}/{video_dict['Title']}", video_dict['Title'])
    if config['upload_by'] == 'bd':
        share_url = uploader.share_item(video_dict['Title'])
        if config['enable_mongodb']:
            db = Database(user_map(video_dict['User']))
            db.insert(video_dict['Title'], share_url, video_dict['Date'])
    elif config['upload_by'] == 's3':
        if config['enable_mongodb']:
            db = Database(user_map(video_dict['User']))
            db.insert(video_dict['Title'],
                      f"gets3/{quote(video_dict['Title'])}",
                      video_dict['Date'])
    else:
        raise RuntimeError(f'Upload {video_dict["Title"]} failed')
    bot(f"[下载提示] {video_dict['Title']} 已上传, 请查看https://matsuri.design/", user_config) 
Example #17
Source File: svg.py    From Jtyoui with MIT License 6 votes vote down vote up
def math_tex(tex, file_path=None):
    """根据Tex语言生成数学公式矢量图

    关于Tex语法参考:https://blog.csdn.net/qfire/article/details/81382048

    :param tex: Tex语言
    :param file_path: 保存矢量图的地址,后缀名一定是: xxx.svg
    :return: 默认返回SVG数据。有地址保存到地址,返回True
    """
    u = quote(tex)
    name = hash(tex)
    s = get(f'https://math.jianshu.com/math?formula={u}')
    data = s.text
    if not file_path:
        file_path = './' + str(name) + '.svg'
    w = open(file_path, 'w')
    w.write(data)
    w.flush()
    w.close()
    return True 
Example #18
Source File: __init__.py    From Jtyoui with MIT License 6 votes vote down vote up
def __init__(self, search):
        """初始化,也就是模糊搜索的第一步

        :param search: 关键字
        """
        self.header = {'User-Agent': random()}  # 设置UA
        # 将中文字转化为URL链接。注意搜狗将中文字进行的GBK编码。而不是UTF-8
        url_word = quote(search.encode('GBK'))
        url = 'https://pinyin.sogou.com/dict/search/search_list/%s/normal/' % url_word  # 搜索链接
        response = requests.get(url=url, headers=self.header)
        match = re.findall(url[24:] + '(.{1,3})">', response.text)  # 匹配下载页数
        max_page = max(map(lambda x: int(x), match)) if match else 1  # 选取最大的页数,如果没有页数返回1
        m = []  # 将匹配到下载链接
        for page in range(1, max_page + 1):
            response = requests.get(url=url + str(page), headers=self.header)
            match = re.findall(r'id=(.+)&name=(.+)"', response.text)  # 匹配下载链接
            m.extend(match)  # 将匹配到的下载链接装到链表中
        load_url = 'https://pinyin.sogou.com/d/dict/download_cell.php?id={0}&name={1}'  # 下载链接的格式
        # 将匹配到的,名字和ID映射到下载链接格式中
        self.load_url = map(lambda x: load_url.format(x[0], x[1]), m) 
Example #19
Source File: rseclient.py    From rucio with Apache License 2.0 6 votes vote down vote up
def list_rses(self, rse_expression=None):
        """
        Sends the request to list all rucio locations(RSEs).

        :rse_expression: RSE Expression to use as filter.
        :return:         a list containing the names of all rucio locations.
        """
        if rse_expression:
            path = ['rses', "?expression=" + quote(rse_expression)]
            path = '/'.join(path)
        else:
            path = 'rses/'
        url = build_url(choice(self.list_hosts), path=path)
        r = self._send_request(url, type='GET')
        if r.status_code == codes.ok:
            return self._load_json_data(r)
        else:
            exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
            raise exc_cls(exc_msg) 
Example #20
Source File: utils.py    From rucio with Apache License 2.0 6 votes vote down vote up
def build_url(url, path=None, params=None, doseq=False):
    """
    utitily function to build an url for requests to the rucio system.

    If the optional parameter doseq is evaluates to True, individual key=value pairs
    separated by '&' are generated for each element of the value sequence for the key.
    """
    complete_url = url
    if path is not None:
        complete_url += "/" + path
    if params is not None:
        complete_url += "?"
        if isinstance(params, str):
            complete_url += quote(params)
        else:
            complete_url += urlencode(params, doseq=doseq)
    return complete_url 
Example #21
Source File: net.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def validate_(self, value, context=None):
        url = self.valid_url(value)
        if not url:
            raise StopValidationError(self.messages['invalid_url'])
        if self.verify_exists:
            url_string = urlquote(urlunsplit((
                url['scheme'],
                (url['host6'] or url['host4'] or url['hostn_enc']) + ':' + (url['port'] or ''),
                url['path'],
                url['query'],
                url['frag'])
                ).encode('utf-8'), safe=VALID_CHAR_STRING)
            try:
                urlopen(url_string)
            except URLError:
                raise StopValidationError(self.messages['not_found']) 
Example #22
Source File: webhdfs.py    From filesystem_spec with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _call(self, op, method="get", path=None, data=None, redirect=True, **kwargs):
        url = self.url + quote(path or "")
        args = kwargs.copy()
        args.update(self.pars)
        args["op"] = op.upper()
        logger.debug(url, method, args)
        out = self.session.request(
            method=method.upper(),
            url=url,
            params=args,
            data=data,
            allow_redirects=redirect,
        )
        if out.status_code in [400, 401, 403, 404, 500]:
            try:
                err = out.json()
                msg = err["RemoteException"]["message"]
                exp = err["RemoteException"]["exception"]
            except (ValueError, KeyError):
                pass
            else:
                if exp in ["IllegalArgumentException", "UnsupportedOperationException"]:
                    raise ValueError(msg)
                elif exp in ["SecurityException", "AccessControlException"]:
                    raise PermissionError(msg)
                elif exp in ["FileNotFoundException"]:
                    raise FileNotFoundError(msg)
                else:
                    raise RuntimeError(msg)
        out.raise_for_status()
        return out 
Example #23
Source File: header.py    From PyOne with Mozilla Public License 2.0 5 votes vote down vote up
def ReFreshToken(refresh_token,user=GetConfig('default_pan')):
    client_id=get_value('client_id',user)
    client_secret=get_value('client_secret',user)
    od_type=get_value('od_type',user)
    headers={'Content-Type':'application/x-www-form-urlencoded'}
    headers.update(default_headers)
    data=ReFreshData.format(client_id=client_id,redirect_uri=urllib.quote(redirect_uri),client_secret=urllib.quote(client_secret),refresh_token=refresh_token)
    if od_type=='cn':
        data+='&resource={}'.format(GetAppUrl(user))
    url=GetOAuthUrl(od_type)
    r=browser.post(url,data=data,headers=headers,verify=False)
    return json.loads(r.text) 
Example #24
Source File: http.py    From discord.py with MIT License 5 votes vote down vote up
def ban(self, user_id, guild_id, delete_message_days=1, reason=None):
        r = Route('PUT', '/guilds/{guild_id}/bans/{user_id}', guild_id=guild_id, user_id=user_id)
        params = {
            'delete-message-days': delete_message_days,
        }

        if reason:
            # thanks aiohttp
            r.url = '{0.url}?reason={1}'.format(r, _uriquote(reason))

        return self.request(r, params=params) 
Example #25
Source File: http.py    From discord.py with MIT License 5 votes vote down vote up
def kick(self, user_id, guild_id, reason=None):
        r = Route('DELETE', '/guilds/{guild_id}/members/{user_id}', guild_id=guild_id, user_id=user_id)
        if reason:
            # thanks aiohttp
            r.url = '{0.url}?reason={1}'.format(r, _uriquote(reason))

        return self.request(r) 
Example #26
Source File: problem.py    From vj4 with GNU Affero General Public License v3.0 5 votes vote down vote up
def get(self, *, q: str):
    q = q.strip()
    if not q:
      self.json_or_redirect(self.referer_or_main)
      return
    try:
      pdoc = await problem.get(self.domain_id, document.convert_doc_id(q))
    except error.ProblemNotFoundError:
      pdoc = None
    if pdoc:
      self.redirect(self.reverse_url('problem_detail', pid=pdoc['doc_id']))
      return
    self.redirect('http://cn.bing.com/search?q={0}+site%3A{1}' \
                  .format(parse.quote(q), parse.quote(options.url_prefix))) 
Example #27
Source File: request_finder.py    From openSUSE-release-tools with GNU General Public License v2.0 5 votes vote down vote up
def find_request_package(self, package):
        """
        Look up the package by its name and return the SR#
        :param package: name of the package
        """

        query = 'types=submit,delete&states=new,review&project={}&view=collection&package={}'
        query = query.format(self.api.project, quote(package))
        url = makeurl(self.api.apiurl, ['request'], query)
        f = http_GET(url)

        root = ET.parse(f).getroot()

        requests = []
        for sr in root.findall('request'):
            # Check the target matches - OBS query is case insensitive, but OBS is not
            rq_target = sr.find('action').find('target')
            if package != rq_target.get('package') or self.api.project != rq_target.get('project'):
                continue

            request = sr.get('id')
            state = sr.find('state').get('name')

            self.srs[int(request)] = {'project': self.api.project, 'state': state}
            requests.append(request)

        if len(requests) > 1:
            msg = 'There are multiple requests for package "{}": {}'
            msg = msg.format(package, ', '.join(requests))
            raise oscerr.WrongArgs(msg)

        request = int(requests[0]) if requests else None
        return request 
Example #28
Source File: request_finder.py    From openSUSE-release-tools with GNU General Public License v2.0 5 votes vote down vote up
def find_request_package(self, package):
        """
        Look up the package by its name and return the SR#
        :param package: name of the package
        """

        query = 'types=submit,delete&states=new,review&project={}&view=collection&package={}'
        query = query.format(self.api.project, quote(package))
        url = makeurl(self.api.apiurl, ['request'], query)
        f = http_GET(url)

        root = ET.parse(f).getroot()

        requests = []
        for sr in root.findall('request'):
            # Check the target matches - OBS query is case insensitive, but OBS is not
            rq_target = sr.find('action').find('target')
            if package != rq_target.get('package') or self.api.project != rq_target.get('project'):
                continue

            request = sr.get('id')
            state = sr.find('state').get('name')

            self.srs[int(request)] = {'project': self.api.project, 'state': state}
            requests.append(request)

        if len(requests) > 1:
            msg = 'There are multiple requests for package "{}": {}'
            msg = msg.format(package, ', '.join(requests))
            raise oscerr.WrongArgs(msg)

        request = int(requests[0]) if requests else None
        return request 
Example #29
Source File: test_serializing.py    From schemathesis with MIT License 5 votes vote down vote up
def __init__(self, instance, prefix=""):
        self.instance = quote(instance)
        self.prefix = quote(prefix) 
Example #30
Source File: Struts2Scan.py    From Struts2-Scan with GNU General Public License v3.0 5 votes vote down vote up
def exec_cmd3(self, cmd):
        """执行命令"""
        html = get(self.url + "?" + self.exec_payload3.replace('CMD', quote(cmd)).replace('ENCODING', self.encoding),
                   self.headers, self.encoding)
        return html