Subscribed unsubscribe Subscribe Subscribe

oinume journal

Scratchpad of what I learned

Parsing MySQL's URL in Python3

Just add urllib.parse.uses_netloc.append("mysql") if you want to parse URL such as mysql://root:pass@localhost/demo.

#!/usr/bin/env python

import urllib.parse
urllib.parse.uses_netloc.append("mysql")

if __name__ == "__main__":
    url_str = "mysql://root:pass@localhost/demo"
    url = urllib.parse.urlparse(url_str)
    print("URL={0}".format(url_str))
    print("host={0}, user={1}, password={2}, database={3}".format(
        url.hostname, url.username, url.password, url.path[1:]))
$ python3 url_parse.python
URL=mysql://root:pass@localhost/demo
host=localhost, user=root, password=pass, database=demo