Python urllib.parse.urlencode() Examples

The following are 30 code examples of urllib.parse.urlencode(). 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: qq_spider.py    From QQ_zone with Apache License 2.0 7 votes vote down vote up
def get_mood_url(self):
        url='https://h5.qzone.qq.com/proxy/domain/taotao.qq.com/cgi-bin/emotion_cgi_msglist_v6?'
        params = {
              "sort":0,
                  "start":0,
              "num":20,
            "cgi_host": "http://taotao.qq.com/cgi-bin/emotion_cgi_msglist_v6",
              "replynum":100,
              "callback":"_preloadCallback",
              "code_version":1,
            "inCharset": "utf-8",
            "outCharset": "utf-8",
            "notice": 0,
              "format":"jsonp",
              "need_private_comment":1,
              "g_tk": self.g_tk
              }
        url = url + parse.urlencode(params)
        return url 
Example #2
Source File: message.py    From wechatpy with MIT License 7 votes vote down vote up
def get_subscribe_authorize_url(self, scene, template_id, redirect_url, reserved=None):
        """
        构造请求用户授权的url
        详情请参阅:
        https://mp.weixin.qq.com/wiki?id=mp1500374289_66bvB

        :param scene: 订阅场景值,开发者可以填0-10000的整形值,用来标识订阅场景值
        :type scene: int
        :param template_id: 订阅消息模板ID,登录公众平台后台,在接口权限列表处可查看订阅模板ID
        :param redirect_url: 授权后重定向的回调地址
        :param reserved: 用于保持请求和回调的状态,授权请后原样带回给第三方。该参数可用于防止csrf攻击。若不指定则随机生成。
        """
        if reserved is None:
            reserved = random_string()
        base_url = "https://mp.weixin.qq.com/mp/subscribemsg"
        params = [
            ("action", "get_confirm"),
            ("appid", self.appid),
            ("scene", scene),
            ("template_id", template_id),
            ("redirect_url", redirect_url),
            ("reserved", reserved),
        ]
        encoded_params = urlencode(params)
        return f"{base_url}?{encoded_params}#wechat_redirect" 
Example #3
Source File: fun.py    From cyberdisc-bot with MIT License 7 votes vote down vote up
def lmgtfy(self, ctx: Context, *args: str):
        """
        Returns a LMGTFY URL for a given user argument.
        """
        # Creates a lmgtfy.com url for the given query.
        request_data = {
            "q": " ".join(arg for arg in args if not arg.startswith("-")),
            "ie": int("-ie" in args),
        }
        url = "https://lmgtfy.com/?" + urlencode(request_data)

        await ctx.send(url)

        if "-d" in args:
            await ctx.message.delete()

    # Ratelimit to one use per user every minute and 4 usages per minute per channel 
Example #4
Source File: request.py    From recruit with Apache License 2.0 6 votes vote down vote up
def request_encode_url(self, method, url, fields=None, headers=None,
                           **urlopen_kw):
        """
        Make a request using :meth:`urlopen` with the ``fields`` encoded in
        the url. This is useful for request methods like GET, HEAD, DELETE, etc.
        """
        if headers is None:
            headers = self.headers

        extra_kw = {'headers': headers}
        extra_kw.update(urlopen_kw)

        if fields:
            url += '?' + urlencode(fields)

        return self.urlopen(method, url, **extra_kw) 
Example #5
Source File: wfapi.py    From terraform-templates with Apache License 2.0 6 votes vote down vote up
def verdict(self,
                hash=None):
        self.__clear_response()

        request_uri = '/publicapi/get/verdict'

        query = {}
        query['apikey'] = self.api_key
        if hash is not None:
            query['hash'] = hash

        response = self.__api_request(request_uri=request_uri,
                                      body=urlencode(query))
        if not response:
            raise PanWFapiError(self._msg)

        if not self.__set_response(response):
            raise PanWFapiError(self._msg) 
Example #6
Source File: wfapi.py    From terraform-templates with Apache License 2.0 6 votes vote down vote up
def sample(self,
               hash=None):
        self.__clear_response()

        request_uri = '/publicapi/get/sample'

        query = {}
        query['apikey'] = self.api_key
        if hash is not None:
            query['hash'] = hash

        response = self.__api_request(request_uri=request_uri,
                                      body=urlencode(query))
        if not response:
            raise PanWFapiError(self._msg)

        if not self.__set_response(response):
            raise PanWFapiError(self._msg) 
Example #7
Source File: api.py    From google_streetview with MIT License 6 votes vote down vote up
def __init__(
    self,
    params,
    site_api='https://maps.googleapis.com/maps/api/streetview',
    site_metadata='https://maps.googleapis.com/maps/api/streetview/metadata'):
    
    # (params) Set default params
    defaults = {
      'size': '640x640'
    }
    for i in range(len(params)):
      for k in defaults:
        if k not in params[i]:
          params[i][k] = defaults[k]
    self.params = params
    
    # (image) Create image api links from parameters
    self.links = [site_api + '?' + urlencode(p) for p in params]
    
    # (metadata) Create metadata api links and data from parameters
    self.metadata_links = [site_metadata + '?' + urlencode(p) for p in params]
    self.metadata = [requests.get(url, stream=True).json() for url in self.metadata_links] 
Example #8
Source File: wfapi.py    From terraform-templates with Apache License 2.0 6 votes vote down vote up
def pcap(self,
             hash=None,
             platform=None):
        self.__clear_response()

        request_uri = '/publicapi/get/pcap'

        query = {}
        query['apikey'] = self.api_key
        if hash is not None:
            query['hash'] = hash
        if platform is not None:
            query['platform'] = platform

        response = self.__api_request(request_uri=request_uri,
                                      body=urlencode(query))
        if not response:
            raise PanWFapiError(self._msg)

        if not self.__set_response(response):
            raise PanWFapiError(self._msg) 
Example #9
Source File: wfapi.py    From terraform-templates with Apache License 2.0 6 votes vote down vote up
def verdicts_changed(self,
                         date=None):
        self.__clear_response()

        request_uri = '/publicapi/get/verdicts/changed'

        query = {}
        query['apikey'] = self.api_key
        if date is not None:
            query['date'] = date

        response = self.__api_request(request_uri=request_uri,
                                      body=urlencode(query))
        if not response:
            raise PanWFapiError(self._msg)

        if not self.__set_response(response):
            raise PanWFapiError(self._msg) 
Example #10
Source File: wfapi.py    From terraform-templates with Apache License 2.0 6 votes vote down vote up
def report(self,
               hash=None,
               format=None):
        self.__clear_response()

        request_uri = '/publicapi/get/report'

        query = {}
        query['apikey'] = self.api_key
        if hash is not None:
            query['hash'] = hash
        if format is not None:
            query['format'] = format

        response = self.__api_request(request_uri=request_uri,
                                      body=urlencode(query))
        if not response:
            raise PanWFapiError(self._msg)

        if not self.__set_response(response):
            raise PanWFapiError(self._msg) 
Example #11
Source File: request.py    From plugin.video.emby with GNU General Public License v3.0 6 votes vote down vote up
def request_encode_url(self, method, url, fields=None, headers=None,
                           **urlopen_kw):
        """
        Make a request using :meth:`urlopen` with the ``fields`` encoded in
        the url. This is useful for request methods like GET, HEAD, DELETE, etc.
        """
        if headers is None:
            headers = self.headers

        extra_kw = {'headers': headers}
        extra_kw.update(urlopen_kw)

        if fields:
            url += '?' + urlencode(fields)

        return self.urlopen(method, url, **extra_kw) 
Example #12
Source File: noaa.py    From noaa with MIT License 6 votes vote down vote up
def stations(self, **params):
        """Get list of US weather stations and their metadata.

        Response in this method should not be modified.
        In this way, we can keep track of changes made by NOAA through
        functional tests @todo(paulokuong) later on.

        Args:
            station_id (str[optional]): station id.
            state (str[optional]): 2 letters state code.
            limit (int[optional]): limit of results.
        Returns:
            json: json response from api.
        """
        if len(params) > 0:
            if 'station_id' in params:
                params['id'] = params['station_id']
                del params['station_id']
            return self.make_get_request(
                "/stations?{query_string}".format(
                    query_string=urlencode(params)),
                end_point=self.DEFAULT_END_POINT)
        return self.make_get_request(
            "/stations", end_point=self.DEFAULT_END_POINT) 
Example #13
Source File: test_utils.py    From Jike-Metro with MIT License 6 votes vote down vote up
def test_get_uptoken(self):
        params = {'bucket': 'jike'}
        success_reponse = {'uptoken': 'token'}
        responses.add(responses.GET, constants.ENDPOINTS['picture_uptoken'],
                      json=success_reponse, status=200)
        responses.add(responses.GET, constants.ENDPOINTS['picture_uptoken'],
                      status=404)
        # success call
        result = utils.get_uptoken()
        self.assertEqual(result, 'token')
        self.assertEqual(len(responses.calls), 1)
        self.assertEqual(responses.calls[0].request.url,
                         constants.ENDPOINTS['picture_uptoken'] + '?' + urlencode(params))
        self.assertEqual(responses.calls[0].response.json(), success_reponse)
        # failed call
        with self.assertRaises(requests.HTTPError) as cm:
            utils.get_uptoken()
        self.assertEqual(len(responses.calls), 2)
        self.assertEqual(cm.exception.response.status_code, 404) 
Example #14
Source File: extending.py    From tekore with MIT License 6 votes vote down vote up
def _load(self, request: Request) -> tuple:
        params = ('&' + urlencode(request.params)) if request.params else ''
        url = request.url + params
        item = self._cache.get(url, None)

        if item is None:
            return None, None

        vary_key = self._vary_key(request, item[0])
        cached = item[1].get(vary_key, None)

        if cached is not None:
            response = cached['response']
            deque_item = (url, vary_key)
            if self._cc_fresh(cached):
                self._update_usage(deque_item)
                return response, None
            elif self._has_etag(cached):
                self._update_usage(deque_item)
                return response, cached['etag']
            elif self.max_size is not None:
                self._deque.remove(deque_item)

        return None, None 
Example #15
Source File: client.py    From rets with MIT License 6 votes vote down vote up
def _http_request(self, url: str, headers: dict = None, payload: dict = None) -> Response:
        if not self._session:
            raise RetsClientError('Session not instantiated. Call .login() first')

        request_headers = {
            **(headers or {}),
            'User-Agent': self.user_agent,
            'RETS-Version': self.rets_version,
            'RETS-UA-Authorization': self._rets_ua_authorization()
        }

        if self._use_get_method:
            if payload:
                url = '%s?%s' % (url, urlencode(payload))
            response = self._session.get(url, auth=self._http_auth, headers=request_headers)
        else:
            response = self._session.post(url, auth=self._http_auth, headers=request_headers, data=payload)

        response.raise_for_status()
        self._rets_session_id = self._session.cookies.get('RETS-Session-ID', '')
        return response 
Example #16
Source File: connection.py    From dot2moon with MIT License 6 votes vote down vote up
def post(self, payload, request_par):
        data = parse.urlencode(request_par).encode()

        if self.UserAgent != None:
            req = Request(
                    self.target_url,
                    data=data, 
                    headers={"User-Agent":self.UserAgent})
            conn = urlopen(req, timeout=self.TimeOut)
        else:
            conn = urlopen(self.target_url,
                    data=data,
                    timeout=self.TimeOut)

        html = conn.read().decode("utf-8")
        page_size = len(html)

        if self.verbose == True:
            print(" [+] Source code got from %s" % payload)
            print("----START" + "-"*71)
            print(html)
            print("----END" + "-"*73)
        
        return(self.target_url, page_size, html, payload) 
Example #17
Source File: test_cassettes.py    From schemathesis with MIT License 5 votes vote down vote up
def test_replay(openapi_version, cli, schema_url, app, reset_app, cassette_path):
    # Record a cassette
    result = cli.run(
        schema_url,
        f"--store-network-log={cassette_path}",
        "--hypothesis-max-examples=1",
        "--hypothesis-seed=1",
        "--validate-schema=false",
    )
    assert result.exit_code == ExitCode.TESTS_FAILED, result.stdout
    # these requests are not needed
    reset_app(openapi_version)
    assert not app["incoming_requests"]
    # When a valid cassette is replayed
    result = cli.replay(str(cassette_path))
    assert result.exit_code == ExitCode.OK, result.stdout
    cassette = load_cassette(cassette_path)
    interactions = cassette["http_interactions"]
    # Then there should be the same number of requests made to the app as there are in the cassette
    assert len(app["incoming_requests"]) == len(interactions)
    for interaction, request in zip(interactions, app["incoming_requests"]):
        # And these requests should be equal
        serialized = interaction["request"]
        assert request.method == serialized["method"]
        parsed = urlparse(str(request.url))
        encoded_query = urlencode(parse_qsl(parsed.query, keep_blank_values=True))
        url = urlunparse((parsed.scheme, parsed.netloc, parsed.path, parsed.params, encoded_query, parsed.fragment))
        assert url == serialized["uri"]
        content = await request.read()
        assert content == base64.b64decode(serialized["body"]["base64_string"])
        compare_headers(request, serialized["headers"]) 
Example #18
Source File: test_verification.py    From django-rest-registration with MIT License 5 votes vote down vote up
def test_get_url(self):
        signer = self.create_signer({
            'email': self.test_email,
        })
        url = signer.get_url()
        self.assertIn(self.cls.BASE_URL, url)
        self.assertIn(urlencode({'email': self.test_email}), url) 
Example #19
Source File: conftest.py    From aioredis with MIT License 5 votes vote down vote up
def server_tcp_url(server, request):

    def make(**kwargs):
        netloc = '{0.host}:{0.port}'.format(server.tcp_address)
        path = ''
        if request.param == 'path':
            if 'password' in kwargs:
                netloc = ':{0}@{1.host}:{1.port}'.format(
                    kwargs.pop('password'), server.tcp_address)
            if 'db' in kwargs:
                path = '/{}'.format(kwargs.pop('db'))
        query = urlencode(kwargs)
        return urlunparse(('redis', netloc, path, '', query, ''))
    return make 
Example #20
Source File: EasyLogin.py    From cc98 with MIT License 5 votes vote down vote up
def post_dict(self, url, dict, result=True, save=False, headers=None,cache=None):
        """
        like post but using dict to post
        :param url: post target url
        :param dict: the data to be post, in dict form, example: {"age":"19","name":"chenyuan"}
        :param result: the page returned save to a.b
        :param save: save cookie to file
        :param headers: override headers to be sent
        :return: the requests object
        """
        if cache is True:
            dict = OrderedDict(sorted(dict.items(), key=lambda t: t[0]))
        data = urlencode(dict)
        return self.post(url, data, result=result, save=save, headers=headers,cache=cache) 
Example #21
Source File: wolfram.py    From bot with MIT License 5 votes vote down vote up
def wolfram_short_command(self, ctx: Context, *, query: str) -> None:
        """Requests an answer to a simple question."""
        url_str = parse.urlencode({
            "i": query,
            "appid": APPID,
        })
        query = QUERY.format(request="result", data=url_str)

        # Give feedback that the bot is working.
        async with ctx.channel.typing():
            async with self.bot.http_session.get(query) as response:
                status = response.status
                response_text = await response.text()

            if status == 501:
                message = "Failed to get response"
                color = Colours.soft_red
            elif status == 400:
                message = "No input found"
                color = Colours.soft_red
            elif response_text == "Error 1: Invalid appid":
                message = "Wolfram API key is invalid or missing."
                color = Colours.soft_red
            else:
                message = response_text
                color = Colours.soft_orange

            await send_embed(ctx, message, color) 
Example #22
Source File: wolfram.py    From bot with MIT License 5 votes vote down vote up
def wolfram_command(self, ctx: Context, *, query: str) -> None:
        """Requests all answers on a single image, sends an image of all related pods."""
        url_str = parse.urlencode({
            "i": query,
            "appid": APPID,
        })
        query = QUERY.format(request="simple", data=url_str)

        # Give feedback that the bot is working.
        async with ctx.channel.typing():
            async with self.bot.http_session.get(query) as response:
                status = response.status
                image_bytes = await response.read()

            f = discord.File(BytesIO(image_bytes), filename="image.png")
            image_url = "attachment://image.png"

            if status == 501:
                message = "Failed to get response"
                footer = ""
                color = Colours.soft_red
            elif status == 400:
                message = "No input found"
                footer = ""
                color = Colours.soft_red
            elif status == 403:
                message = "Wolfram API key is invalid or missing."
                footer = ""
                color = Colours.soft_red
            else:
                message = ""
                footer = "View original for a bigger picture."
                color = Colours.soft_orange

            # Sends a "blank" embed if no request is received, unsure how to fix
            await send_embed(ctx, message, color, footer=footer, img_url=image_url, f=f) 
Example #23
Source File: request.py    From vulscan with MIT License 5 votes vote down vote up
def request_encode_url(self, method, url, fields=None, **urlopen_kw):
        """
        Make a request using :meth:`urlopen` with the ``fields`` encoded in
        the url. This is useful for request methods like GET, HEAD, DELETE, etc.
        """
        if fields:
            url += '?' + urlencode(fields)
        return self.urlopen(method, url, **urlopen_kw) 
Example #24
Source File: client.py    From d6tpipe with MIT License 5 votes vote down vote up
def _build_url(self, query_params):
        """Build the final URL to be passed to urllib

        :param query_params: A dictionary of all the query parameters
        :type query_params: dictionary
        :return: string
        """
        url = ''
        count = 0
        while count < len(self._url_path):
            url += '/{0}'.format(self._url_path[count])
            count += 1

        # add slash
        if self.append_slash:
            url += '/'

        if query_params:
            url_values = urlencode(sorted(query_params.items()), True)
            url = '{0}?{1}'.format(url, url_values)

        if self._version:
            url = self._build_versioned_url(url)
        else:
            url = self.host + url
        return url 
Example #25
Source File: caching.py    From tekore with MIT License 5 votes vote down vote up
def response(code, url, params, cc=None, etag=None, vary=None) -> MagicMock:
    r = MagicMock()
    r.status_code = code
    r.url = url + ('&' + urlencode(params) if params else '')
    if isinstance(cc, int):
        cc = f'public, max-age={cc}'
    h = {
        'Cache-Control': cc,
        'ETag': etag,
        'Vary': vary
    }
    r.headers = {k: v for k, v in h.items() if v is not None}
    return r 
Example #26
Source File: oauth2.py    From accesslink-example-python with MIT License 5 votes vote down vote up
def get_authorization_url(self, response_type="code"):
        """Build authorization url for the client"""

        params = {
            "client_id": self.client_id,
            "response_type": response_type,
        }

        if self.redirect_url:
            params["redirect_uri"] = self.redirect_url

        return "{url}?{params}".format(url=self.authorization_url,
                                       params=urlencode(params)) 
Example #27
Source File: proxycrawl_api.py    From proxycrawl-python with Apache License 2.0 5 votes vote down vote up
def buildURL(self, url, options):
        options = urlencode(options or {})
        url = quote_plus(url)
        url = self.endPointUrl + '&url=' + url + '&' + options

        return url 
Example #28
Source File: proxycrawl_api.py    From proxycrawl-python with Apache License 2.0 5 votes vote down vote up
def post(self, url, data, options = None):
        if isinstance(data, dict):
            data = urlencode(data)
        data = data.encode('utf-8')
        return self.request(url, data, options) 
Example #29
Source File: __init__.py    From misp42splunk with GNU Lesser General Public License v3.0 5 votes vote down vote up
def __init__(
        self, credentials, host, request_uri, headers, response, content, http
    ):
        from urllib.parse import urlencode

        Authentication.__init__(
            self, credentials, host, request_uri, headers, response, content, http
        )
        challenge = _parse_www_authenticate(response, "www-authenticate")
        service = challenge["googlelogin"].get("service", "xapi")
        # Bloggger actually returns the service in the challenge
        # For the rest we guess based on the URI
        if service == "xapi" and request_uri.find("calendar") > 0:
            service = "cl"
        # No point in guessing Base or Spreadsheet
        # elif request_uri.find("spreadsheets") > 0:
        #    service = "wise"

        auth = dict(
            Email=credentials[0],
            Passwd=credentials[1],
            service=service,
            source=headers["user-agent"],
        )
        resp, content = self.http.request(
            "https://www.google.com/accounts/ClientLogin",
            method="POST",
            body=urlencode(auth),
            headers={"Content-Type": "application/x-www-form-urlencoded"},
        )
        lines = content.split("\n")
        d = dict([tuple(line.split("=", 1)) for line in lines if line])
        if resp.status == 403:
            self.Auth = ""
        else:
            self.Auth = d["Auth"] 
Example #30
Source File: conftest.py    From aioredis with MIT License 5 votes vote down vote up
def server_unix_url(server):

    def make(**kwargs):
        query = urlencode(kwargs)
        return urlunparse(('unix', '', server.unixsocket, '', query, ''))
    return make

# Internal stuff #