I am trying to take the username and password from a url and put it back together to use it again in its complete form but without the basic auth part.
I am trying something like the below but the result is not what I expect.
from urllib.parse import urlsplit, urlunsplit
parts = urlsplit("http://user:pwd@host:8080/path?query=foo", allow_fragments=True)
auth = (parts.username, parts.password)
print(urlunsplit((parts.scheme, parts.hostname, str(parts.port), parts.path, parts.query)))
# http://host/8080?/path#query=foo
It would be even better if I could set username and password to None on the parts and just call geturl
but I get errors that I can't change attributes.
from urllib.parse import urlsplit
parts = urlsplit("http://user:pwd@host:8080/path?query=foo", allow_fragments=True)
auth = (parts.username, parts.password)
parts.password = None
parts.username = None
print(parts.geturl())
# AttributeError: can't set attribute
Note, that I want to preserve the username and password in a separate tuple.