pythonurllibenviron

python: getting the complete url with os.environ


I would like to get the COMPLETE URL using os.environ. I am rendering notebooks with voila and I would like to open url from a dashboard using parameters in the URL.

So far I have:

URL_BASE = os.environ.get('SCRIPT_NAME')
PARAMETERS = os.environ.get("QUERY_STRING")
print(f'{URL_BASE=}')
print(f'{PARAMETERS=}')

assuming this is the url:

https://flyingcar.org/john/voila/render/shared/users/j/john/learn_url.ipynb?redirects=2&name=john&dossier=SA123445#{whatever=no/f=5}

URL_BASE="flyingcar.org/john/voila/render/shared/users/j/john/learn_url.ipynb"
&
PARAMETERS="redirects=2&name=john&dossier=SA123445"

Having a look at the whole collection of vars in os.environ I dont see any that would include the whole url (including what is there after #) in order to parse that part as well as with parameters.

captured_values = parse_qs(PARAMETERS)
print('here parse_qs of query:',captured_values)
>>> here parse_qs of query: {'d': ['34'], 'f': ['56']}

Some ideas?

I tried to print all the os.environ variables with:

for k,v in os.environ.items():
    print(k,v)

but it nothing seems to contain what is beyond the # symbol in the URL Any ideas? Thanks

RELATED: Get current URL in Python


Solution

  • The fragment (#) part of the URI never leaves the user agent, so it can't be picked up by the python web server. See: https://datatracker.ietf.org/doc/html/rfc3986#page-24

    ... instead, the fragment identifier is separated from the rest of the URI prior to a dereference, and thus the identifying information within the fragment itself is dereferenced solely by the user agent, regardless of the URI scheme.