Search code examples
pythonurllib

urllib.parse.urlparse not reading params


I'm trying to read parameters in python

 urlparse('jdbc:mysql:@localhost:3306/mysql;user:pass?jdbc_driver_name=com.mysql.jdbc.Driver')

According to documentation params are placed after semicolon, but I'm getting empty string for params in my example url.


Solution

  • From the source code:

    uses_params = ['', 'ftp', 'hdl', 'prospero', 'http', 'imap',
                   'https', 'shttp', 'rtsp', 'rtspu', 'sip', 'sips',
                   'mms', 'sftp', 'tel']
    

    It only parses params if thee scheme in this list.

    def urlparse(url, scheme='', allow_fragments=True):
        url, scheme, _coerce_result = _coerce_args(url, scheme)
        splitresult = urlsplit(url, scheme, allow_fragments)
        scheme, netloc, url, query, fragment = splitresult
        if scheme in uses_params and ';' in url: # <------------------------------ Right here
            url, params = _splitparams(url)
        else:
            params = ''
        result = ParseResult(scheme, netloc, url, params, query, fragment)
        return _coerce_result(result)