Python cgi.parse_qsl() Examples

The following are 30 code examples of cgi.parse_qsl(). 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 cgi , or try the search function .
Example #1
Source File: util.py    From sndlatr with Apache License 2.0 6 votes vote down vote up
def _add_query_parameter(url, name, value):
  """Adds a query parameter to a url.

  Replaces the current value if it already exists in the URL.

  Args:
    url: string, url to add the query parameter to.
    name: string, query parameter name.
    value: string, query parameter value.

  Returns:
    Updated query parameter. Does not update the url if value is None.
  """
  if value is None:
    return url
  else:
    parsed = list(urlparse.urlparse(url))
    q = dict(parse_qsl(parsed[4]))
    q[name] = value
    parsed[4] = urllib.urlencode(q)
    return urlparse.urlunparse(parsed) 
Example #2
Source File: plugin.py    From limnoria-plugins with MIT License 6 votes vote down vote up
def get_video_id_from_url(self, url, info):
        """
        Get YouTube video ID from URL
        """
        try:
            path = info.path
            domain = info.netloc
            video_id = ""

            if domain == "youtu.be":
                video_id = path.split("/")[1]
            else:
                parsed = cgi.parse_qsl(info.query)
                params = dict(parsed)

                if "v" in params:
                    video_id = params["v"]

            if video_id:
                return video_id
            else:
                log.error("SpiffyTitles: error getting video id from %s" % (url))

        except IndexError as e:
            log.error("SpiffyTitles: error getting video id from %s (%s)" % (url, str(e))) 
Example #3
Source File: util.py    From data with GNU General Public License v3.0 6 votes vote down vote up
def _add_query_parameter(url, name, value):
  """Adds a query parameter to a url.

  Replaces the current value if it already exists in the URL.

  Args:
    url: string, url to add the query parameter to.
    name: string, query parameter name.
    value: string, query parameter value.

  Returns:
    Updated query parameter. Does not update the url if value is None.
  """
  if value is None:
    return url
  else:
    parsed = list(urlparse.urlparse(url))
    q = dict(parse_qsl(parsed[4]))
    q[name] = value
    parsed[4] = urllib.urlencode(q)
    return urlparse.urlunparse(parsed) 
Example #4
Source File: tools.py    From data with GNU General Public License v3.0 6 votes vote down vote up
def do_GET(s):
    """Handle a GET request.

    Parses the query parameters and prints a message
    if the flow has completed. Note that we can't detect
    if an error occurred.
    """
    s.send_response(200)
    s.send_header("Content-type", "text/html")
    s.end_headers()
    query = s.path.split('?', 1)[-1]
    query = dict(parse_qsl(query))
    s.server.query_params = query
    s.wfile.write("<html><head><title>Authentication Status</title></head>")
    s.wfile.write("<body><p>The authentication flow has completed.</p>")
    s.wfile.write("</body></html>") 
Example #5
Source File: tools.py    From billing-export-python with Apache License 2.0 6 votes vote down vote up
def do_GET(s):
    """Handle a GET request.

    Parses the query parameters and prints a message
    if the flow has completed. Note that we can't detect
    if an error occurred.
    """
    s.send_response(200)
    s.send_header("Content-type", "text/html")
    s.end_headers()
    query = s.path.split('?', 1)[-1]
    query = dict(parse_qsl(query))
    s.server.query_params = query
    s.wfile.write("<html><head><title>Authentication Status</title></head>")
    s.wfile.write("<body><p>The authentication flow has completed.</p>")
    s.wfile.write("</body></html>") 
Example #6
Source File: util.py    From billing-export-python with Apache License 2.0 6 votes vote down vote up
def _add_query_parameter(url, name, value):
  """Adds a query parameter to a url.

  Replaces the current value if it already exists in the URL.

  Args:
    url: string, url to add the query parameter to.
    name: string, query parameter name.
    value: string, query parameter value.

  Returns:
    Updated query parameter. Does not update the url if value is None.
  """
  if value is None:
    return url
  else:
    parsed = list(urlparse.urlparse(url))
    q = dict(parse_qsl(parsed[4]))
    q[name] = value
    parsed[4] = urllib.urlencode(q)
    return urlparse.urlunparse(parsed) 
Example #7
Source File: authtools.py    From googleapps-message-recall with Apache License 2.0 6 votes vote down vote up
def do_GET(s):
    """Handle a GET request

    Parses the query parameters and prints a message
    if the flow has completed. Note that we can't detect
    if an error occurred.
    """
    s.send_response(200)
    s.send_header("Content-type", "text/html")
    s.end_headers()
    query = s.path.split('?', 1)[-1]
    query = dict(parse_qsl(query))
    s.server.query_params = query
    s.wfile.write("<html><head><title>Authentication Status</title></head>")
    s.wfile.write("<body><p>The authentication flow has completed.</p>")
    s.wfile.write("</body></html>") 
Example #8
Source File: util.py    From googleapps-message-recall with Apache License 2.0 6 votes vote down vote up
def _add_query_parameter(url, name, value):
  """Adds a query parameter to a url.

  Replaces the current value if it already exists in the URL.

  Args:
    url: string, url to add the query parameter to.
    name: string, query parameter name.
    value: string, query parameter value.

  Returns:
    Updated query parameter. Does not update the url if value is None.
  """
  if value is None:
    return url
  else:
    parsed = list(urlparse.urlparse(url))
    q = dict(parse_qsl(parsed[4]))
    q[name] = value
    parsed[4] = urllib.urlencode(q)
    return urlparse.urlunparse(parsed) 
Example #9
Source File: tools.py    From data with GNU General Public License v3.0 6 votes vote down vote up
def do_GET(s):
    """Handle a GET request.

    Parses the query parameters and prints a message
    if the flow has completed. Note that we can't detect
    if an error occurred.
    """
    s.send_response(200)
    s.send_header("Content-type", "text/html")
    s.end_headers()
    query = s.path.split('?', 1)[-1]
    query = dict(parse_qsl(query))
    s.server.query_params = query
    s.wfile.write("<html><head><title>Authentication Status</title></head>")
    s.wfile.write("<body><p>The authentication flow has completed.</p>")
    s.wfile.write("</body></html>") 
Example #10
Source File: util.py    From twitter-for-bigquery with Apache License 2.0 6 votes vote down vote up
def _add_query_parameter(url, name, value):
  """Adds a query parameter to a url.

  Replaces the current value if it already exists in the URL.

  Args:
    url: string, url to add the query parameter to.
    name: string, query parameter name.
    value: string, query parameter value.

  Returns:
    Updated query parameter. Does not update the url if value is None.
  """
  if value is None:
    return url
  else:
    parsed = list(urlparse.urlparse(url))
    q = dict(parse_qsl(parsed[4]))
    q[name] = value
    parsed[4] = urllib.urlencode(q)
    return urlparse.urlunparse(parsed) 
Example #11
Source File: tools.py    From twitter-for-bigquery with Apache License 2.0 6 votes vote down vote up
def do_GET(s):
    """Handle a GET request.

    Parses the query parameters and prints a message
    if the flow has completed. Note that we can't detect
    if an error occurred.
    """
    s.send_response(200)
    s.send_header("Content-type", "text/html")
    s.end_headers()
    query = s.path.split('?', 1)[-1]
    query = dict(parse_qsl(query))
    s.server.query_params = query
    s.wfile.write("<html><head><title>Authentication Status</title></head>")
    s.wfile.write("<body><p>The authentication flow has completed.</p>")
    s.wfile.write("</body></html>") 
Example #12
Source File: tools.py    From sndlatr with Apache License 2.0 6 votes vote down vote up
def do_GET(s):
    """Handle a GET request.

    Parses the query parameters and prints a message
    if the flow has completed. Note that we can't detect
    if an error occurred.
    """
    s.send_response(200)
    s.send_header("Content-type", "text/html")
    s.end_headers()
    query = s.path.split('?', 1)[-1]
    query = dict(parse_qsl(query))
    s.server.query_params = query
    s.wfile.write("<html><head><title>Authentication Status</title></head>")
    s.wfile.write("<body><p>The authentication flow has completed.</p>")
    s.wfile.write("</body></html>") 
Example #13
Source File: util.py    From data with GNU General Public License v3.0 6 votes vote down vote up
def _add_query_parameter(url, name, value):
  """Adds a query parameter to a url.

  Replaces the current value if it already exists in the URL.

  Args:
    url: string, url to add the query parameter to.
    name: string, query parameter name.
    value: string, query parameter value.

  Returns:
    Updated query parameter. Does not update the url if value is None.
  """
  if value is None:
    return url
  else:
    parsed = list(urlparse.urlparse(url))
    q = dict(parse_qsl(parsed[4]))
    q[name] = value
    parsed[4] = urllib.urlencode(q)
    return urlparse.urlunparse(parsed) 
Example #14
Source File: __init__.py    From python-compat-runtime with Apache License 2.0 6 votes vote down vote up
def parse_backend_uri(backend_uri):
    """
    Converts the "backend_uri" into a cache scheme ('db', 'memcached', etc), a
    host and any extra params that are required for the backend. Returns a
    (scheme, host, params) tuple.
    """
    if backend_uri.find(':') == -1:
        raise InvalidCacheBackendError("Backend URI must start with scheme://")
    scheme, rest = backend_uri.split(':', 1)
    if not rest.startswith('//'):
        raise InvalidCacheBackendError("Backend URI must start with scheme://")

    host = rest[2:]
    qpos = rest.find('?')
    if qpos != -1:
        params = dict(parse_qsl(rest[qpos+1:]))
        host = rest[2:qpos]
    else:
        params = {}
    if host.endswith('/'):
        host = host[:-1]

    return scheme, host, params 
Example #15
Source File: tools.py    From googleapps-message-recall with Apache License 2.0 6 votes vote down vote up
def do_GET(s):
    """Handle a GET request.

    Parses the query parameters and prints a message
    if the flow has completed. Note that we can't detect
    if an error occurred.
    """
    s.send_response(200)
    s.send_header("Content-type", "text/html")
    s.end_headers()
    query = s.path.split('?', 1)[-1]
    query = dict(parse_qsl(query))
    s.server.query_params = query
    s.wfile.write("<html><head><title>Authentication Status</title></head>")
    s.wfile.write("<body><p>The authentication flow has completed.</p>")
    s.wfile.write("</body></html>") 
Example #16
Source File: tools.py    From splunk-ref-pas-code with Apache License 2.0 6 votes vote down vote up
def do_GET(s):
    """Handle a GET request.

    Parses the query parameters and prints a message
    if the flow has completed. Note that we can't detect
    if an error occurred.
    """
    s.send_response(200)
    s.send_header("Content-type", "text/html")
    s.end_headers()
    query = s.path.split('?', 1)[-1]
    query = dict(parse_qsl(query))
    s.server.query_params = query
    s.wfile.write("<html><head><title>Authentication Status</title></head>")
    s.wfile.write("<body><p>The authentication flow has completed.</p>")
    s.wfile.write("</body></html>") 
Example #17
Source File: util.py    From splunk-ref-pas-code with Apache License 2.0 6 votes vote down vote up
def _add_query_parameter(url, name, value):
  """Adds a query parameter to a url.

  Replaces the current value if it already exists in the URL.

  Args:
    url: string, url to add the query parameter to.
    name: string, query parameter name.
    value: string, query parameter value.

  Returns:
    Updated query parameter. Does not update the url if value is None.
  """
  if value is None:
    return url
  else:
    parsed = list(urlparse.urlparse(url))
    q = dict(parse_qsl(parsed[4]))
    q[name] = value
    parsed[4] = urllib.urlencode(q)
    return urlparse.urlunparse(parsed) 
Example #18
Source File: util.py    From data with GNU General Public License v3.0 6 votes vote down vote up
def _add_query_parameter(url, name, value):
  """Adds a query parameter to a url.

  Replaces the current value if it already exists in the URL.

  Args:
    url: string, url to add the query parameter to.
    name: string, query parameter name.
    value: string, query parameter value.

  Returns:
    Updated query parameter. Does not update the url if value is None.
  """
  if value is None:
    return url
  else:
    parsed = list(urlparse.urlparse(url))
    q = dict(parse_qsl(parsed[4]))
    q[name] = value
    parsed[4] = urllib.urlencode(q)
    return urlparse.urlunparse(parsed) 
Example #19
Source File: url.py    From mishkal with GNU General Public License v3.0 6 votes vote down vote up
def from_environ(cls, environ, with_query_string=True,
                     with_path_info=True, script_name=None,
                     path_info=None, querystring=None):
        url = request.construct_url(
            environ, with_query_string=False,
            with_path_info=with_path_info, script_name=script_name,
            path_info=path_info)
        if with_query_string:
            if querystring is None:
                vars = request.parse_querystring(environ)
            else:
                vars = cgi.parse_qsl(
                    querystring,
                    keep_blank_values=True,
                    strict_parsing=False)
        else:
            vars = None
        v = cls(url, vars=vars)
        return v 
Example #20
Source File: tools.py    From data with GNU General Public License v3.0 6 votes vote down vote up
def do_GET(s):
    """Handle a GET request.

    Parses the query parameters and prints a message
    if the flow has completed. Note that we can't detect
    if an error occurred.
    """
    s.send_response(200)
    s.send_header("Content-type", "text/html")
    s.end_headers()
    query = s.path.split('?', 1)[-1]
    query = dict(parse_qsl(query))
    s.server.query_params = query
    s.wfile.write("<html><head><title>Authentication Status</title></head>")
    s.wfile.write("<body><p>The authentication flow has completed.</p>")
    s.wfile.write("</body></html>") 
Example #21
Source File: request.py    From mishkal with GNU General Public License v3.0 6 votes vote down vote up
def parse_querystring(environ):
    """
    Parses a query string into a list like ``[(name, value)]``.
    Caches this value in case parse_querystring is called again
    for the same request.

    You can pass the result to ``dict()``, but be aware that keys that
    appear multiple times will be lost (only the last value will be
    preserved).

    """
    source = environ.get('QUERY_STRING', '')
    if not source:
        return []
    if 'paste.parsed_querystring' in environ:
        parsed, check_source = environ['paste.parsed_querystring']
        if check_source == source:
            return parsed
    parsed = cgi.parse_qsl(source, keep_blank_values=True,
                           strict_parsing=False)
    environ['paste.parsed_querystring'] = (parsed, source)
    return parsed 
Example #22
Source File: client.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def _parse_exchange_token_response(content):
  """Parses response of an exchange token request.

  Most providers return JSON but some (e.g. Facebook) return a
  url-encoded string.

  Args:
    content: The body of a response

  Returns:
    Content as a dictionary object. Note that the dict could be empty,
    i.e. {}. That basically indicates a failure.
  """
  resp = {}
  try:
    resp = simplejson.loads(content)
  except StandardError:
    # different JSON libs raise different exceptions,
    # so we just do a catch-all here
    resp = dict(parse_qsl(content))

  # some providers respond with 'expires', others with 'expires_in'
  if resp and 'expires' in resp:
    resp['expires_in'] = resp.pop('expires')

  return resp 
Example #23
Source File: sqlbrute.py    From darkc0de-old-stuff with GNU General Public License v3.0 5 votes vote down vote up
def postReformat(self, postdata):
        return urllib.urlencode(cgi.parse_qsl(postdata)) 
Example #24
Source File: test_cgi.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_deprecated_parse_qsl(self):
        # this func is moved to urllib.parse, this is just a sanity check
        with check_warnings(('cgi.parse_qsl is deprecated, use urllib.parse.'
                             'parse_qsl instead', DeprecationWarning)):
            self.assertEqual([('a', 'A1'), ('b', 'B2'), ('B', 'B3')],
                             cgi.parse_qsl('a=A1&b=B2&B=B3')) 
Example #25
Source File: test_cgi.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_deprecated_parse_qsl(self):
        # this func is moved to urlparse, this is just a sanity check
        with check_warnings(('cgi.parse_qsl is deprecated, use urlparse.'
                             'parse_qsl instead', PendingDeprecationWarning)):
            self.assertEqual([('a', 'A1'), ('b', 'B2'), ('B', 'B3')],
                             cgi.parse_qsl('a=A1&b=B2&B=B3')) 
Example #26
Source File: gauth.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def generate_authorize_url(self, redirect_uri='oob', response_type='code',
                             access_type='offline', **kwargs):
    """Returns a URI to redirect to the provider.

    Args:
      redirect_uri: Either the string 'oob' for a non-web-based application, or
                    a URI that handles the callback from the authorization
                    server.
      response_type: Either the string 'code' for server-side or native
                     application, or the string 'token' for client-side
                     application.
      access_type: Either the string 'offline' to request a refresh token or
                   'online'.

    If redirect_uri is 'oob' then pass in the
    generated verification code to get_access_token,
    otherwise pass in the query parameters received
    at the callback uri to get_access_token.
    If the response_type is 'token', no need to call
    get_access_token as the API will return it within
    the query parameters received at the callback:
      oauth2_token.access_token = YOUR_ACCESS_TOKEN
    """
    self.redirect_uri = redirect_uri
    query = {
      'response_type': response_type,
      'client_id': self.client_id,
      'redirect_uri': redirect_uri,
      'scope': self.scope,
      'access_type': access_type
      }
    query.update(kwargs)
    parts = list(urllib.parse.urlparse(self.auth_uri))
    query.update(dict(parse_qsl(parts[4]))) # 4 is the index of the query part
    parts[4] = urllib.parse.urlencode(query)
    return urllib.parse.urlunparse(parts) 
Example #27
Source File: pUtil.py    From pilot with Apache License 2.0 5 votes vote down vote up
def parseDispatcherResponse(response):
    """ Create the parameter list from the dispatcher response """

# use this when listFilesInDataset usage is not needed any more (v 51b)
#    # remove any _subNNN strings if necessary (from dataset names)
#    if "_sub" in response:
#        response = removeSubFromResponse(response)

    try:
        parList = json.loads(response)
    except:
        data = {}
        parList = cgi.parse_qsl(response, keep_blank_values=True)
        for p in parList:
            data[p[0]] = p[1]

        if 'userProxy' in str(parList) or 'privateKey' in str(parList):
            for i in range(len(parList)):
                if parList[i][0] == 'userProxy' or parList[i][0] == 'publicKey' or parList[i][0] == 'privateKey':
                    newList = list(parList[i])
                    newList[1] = 'hidden'
                    parList[i] = newList
    else:
        data = parList.copy()
        if 'jobs' in parList:
            for p in parList['jobs']:
                if 'userProxy' in p:
                     p['userProxy'] = 'hidden'
                if 'privateKey' in p:
                    p['privateKey'] = 'hidden'
                if 'publicKey' in p:
                    p['publicKey'] = 'hidden'

    tolog("Dispatcher response: %s" % str(parList))

    return data, response 
Example #28
Source File: taskqueue.py    From python-compat-runtime with Apache License 2.0 5 votes vote down vote up
def extract_params(self):
    """Returns the parameters for this task.

    If the same name parameter has several values, then the value is a list of
    strings. For `POST` requests and pull tasks, the parameters are extracted
    from the task payload; for all other methods, the parameters are extracted
    from the URL query string.

    Returns:
      A dictionary of strings that map parameter names to their values as
      strings. If the same name parameter has several values, the value
      will be a list of strings. For `POST` requests and pull tasks, the
      parameters are extracted from the task payload. For all other
      methods, the parameters are extracted from the URL query string. An
      empty dictionary is returned if the task contains an empty payload
      or query string.

    Raises:
      ValueError: If the payload does not contain valid
          `application/x-www-form-urlencoded` data (for `POST` requests and pull
          tasks) or the URL does not contain a valid query (all other requests).
    """
    if self.__method in ('PULL', 'POST'):

      query = self.__payload
    else:
      query = urlparse.urlparse(self.__relative_url).query

    p = {}
    if not query:
      return p

    for key, value in cgi.parse_qsl(
        query, keep_blank_values=True, strict_parsing=True):
      p.setdefault(key, []).append(value)

    for key, value in p.items():
      if len(value) == 1:
        p[key] = value[0]

    return p 
Example #29
Source File: scrapy_pagestorage.py    From scrapy-pagestorage with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def save_response(self, response, spider):
        if isinstance(response, TextResponse):
            fp = request_fingerprint(response.request)
            payload = {
                "_key": fp,
                "_jobid": self.hsref.job.key,
                "_type": "_pageitem",
                "_encoding": response.encoding,
                "url": response.url,
            }
            self._set_cookies(payload, response)

            if response.request.method == 'POST':
                payload["postdata"] = dict(parse_qsl(response.request.body.decode()))

            payload["body"] = response.body_as_unicode()
            if self.trim_html:
                payload['body'] = payload['body'].strip(' \r\n\0')

            if len(payload['body']) > self._writer.maxitemsize:
                spider.logger.warning("Page not saved, body too large: <%s>" %
                                      response.url)
                return

            try:
                self._writer.write(payload)
            except ValueTooLarge as exc:
                spider.logger.warning("Page not saved, %s: <%s>" %
                                      (exc, response.url)) 
Example #30
Source File: test_cgi.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_deprecated_parse_qsl(self):
        # this func is moved to urlparse, this is just a sanity check
        with check_warnings(('cgi.parse_qsl is deprecated, use urlparse.'
                             'parse_qsl instead', PendingDeprecationWarning)):
            self.assertEqual([('a', 'A1'), ('b', 'B2'), ('B', 'B3')],
                             cgi.parse_qsl('a=A1&b=B2&B=B3'))