Update/add username in url in python
Asked Answered
I

4

6

If I have a URL (ex: "ssh://[email protected]:553/random_uri", "https://test.blah.blah:993/random_uri2"), I want to set/update the username in the url. I know there is urllib.parse.urlparse (https://docs.python.org/3/library/urllib.parse.html) that would break them down but I am having trouble creating a new url (or updating) the parsed result with the username I intend to use.

Is there any python library that can help set/update username? Preferably using the parsed result of an urlparse.

Indolent answered 17/9, 2018 at 0:29 Comment(0)
I
1

Found a way to do this: https://sdqali.in/blog/2017/09/25/python-url-manipulation-revisited/ Can create a 'furl' object, set username, and get updated url string.

Indolent answered 17/9, 2018 at 0:36 Comment(3)
I would have gone the simple way: url_text.replace('https://', 'https://user@pass')Balladist
Yeah, good option as well, though sometimes i expect there to be a 'user' included in the url, though a good regex should take care of it.Indolent
The linked site is now offline and not found in the Internet Archive. It would have been more useful for posterity to record the actual steps here.Poison
M
2

Python urllib.parse does not have a nice method to set/edit or remove username/password from URLs. However, you can use the netloc attribute. See the example function below.

from urllib.parse import urlparse, quote

def set_url_username_password(url, username, password):
  _username = quote(username)
  _password = quote(password)
  _url = urlparse(url)
  _netloc = _url.netloc.split('@')[-1]
  _url = _url._replace(netloc=f'{_username}:{_password}@{_netloc}')
  return _url.geturl()

original_url = 'https://google.com'
username = 'username'
password = 'pa$$word'
new_url = set_url_username_password(original_url, username, password)

new_url will be set to https://username:pa%24%[email protected].

Note that this function replaces any existing credentials with the new ones.

Here is a bonus function to remove credentials from an URL:

from urllib.parse import urlparse

def unset_url_username_password(url):
  _url = urlparse(url)
  _netloc = _url.netloc.split('@')[-1]
  _url = _url._replace(netloc=_netloc)
  return _url.geturl()
Marlee answered 9/1, 2023 at 17:25 Comment(1)
You can use _url.hostname instead of _url.netloc.split('@')[-1]Anaxagoras
I
1

Found a way to do this: https://sdqali.in/blog/2017/09/25/python-url-manipulation-revisited/ Can create a 'furl' object, set username, and get updated url string.

Indolent answered 17/9, 2018 at 0:36 Comment(3)
I would have gone the simple way: url_text.replace('https://', 'https://user@pass')Balladist
Yeah, good option as well, though sometimes i expect there to be a 'user' included in the url, though a good regex should take care of it.Indolent
The linked site is now offline and not found in the Internet Archive. It would have been more useful for posterity to record the actual steps here.Poison
G
1

Let me to show a script without 3rd package.

from urllib.parse import urlparse

old_url = "https://user:[email protected]"
user="user-01"
password="my-secure-password"

_url = urlparse(old_url)
domain = _url.netloc.split("@")[-1]
new_url = "{}://{}:{}@{}".format(_url.scheme, user,password, domain)

Run the script, the variable new_url contains your url with new user and password.

Goaltender answered 21/11, 2020 at 6:50 Comment(0)
S
0

Here's an option that retains everything in the input URL (just adds or updates the auth), using stdlib only:

from urllib.parse import urlparse, urlunparse


def inject_auth_to_url(url: str, user: str, password: str) -> str:
    parsed = urlparse(url)
    
    # works no matter if the original url had a user/pass or not
    domain = parsed.netloc.split("@")[-1]
    domain = f"{user}:{password}@{domain}"

    unparsed = (parsed[0], domain, parsed[2], parsed[3], parsed[4], parsed[5])

    return urlunparse(unparsed)
Senegambia answered 18/1, 2023 at 12:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.