简体   繁体   中英

Get current URL in Python using os.environ

I don't have to use os.environ but that might be the easiest method.

I can successfully get the previous URL using:

sURL = os.getenv('HTTP_REFERER')

and the arguments of the current string using:

sURL = os.environ['QUERY_STRING']

but I cannot get the current URL.

Other answers here advise:

sURL = self.request.url

(or self.request.host ) but when I do that I get:

 sURL = self.request.url NameError: global name 'self' is not defined

How can I easily get the current URL?

Full code:

import os

def GetTestMode ():

    sURL = self.request.url
    print '<br />sURL = ' + sURL + '<br />'

Thanks to a colleague, this can be solved with:

    import os.path
    sURL = os.path.realpath('.')
    print 'sURL = ' + sURL + '<br />'

See os.path which says, "Return the canonical path of the specified filename, eliminating any symbolic links encountered in the path (if they are supported by the operating system)."

So this does not return the URL but the file path, but I can also use this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM