How to extract attached credentials from a url?

Viewed 152

In a URL like this: 'https://usertest:userpass@localhost:8080/mypath'. How do I extract the information such as username and password in python? For eg. the information I want to extract from the above URL would be usertest and userpass. I could extract these attached web credentials such as username and password using regex, but that seems to be a bit complicated and I am hoping if there is already a built-in solution for this. Any suggestion?

1 Answers
from urllib.parse import urlparse

url = 'https://usertest:userpass@localhost:8080/mypath'
parsed = urlparse(url)
print(parsed.username)
print(parsed.password)

Check the standard library

Related