使用 os.environ 在 Python 中获取当前 URL

编程入门 行业动态 更新时间:2024-10-10 21:29:50
本文介绍了使用 os.environ 在 Python 中获取当前 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我没有使用os.environ,但这可能是最简单的方法.

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

我可以使用以下方法成功获取上一个 URL:

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']

但我无法获取当前 URL.

but I cannot get the current URL.

这里有其他答案建议:

sURL = self.request.url

(或 self.request.host) 但是当我这样做时,我得到:

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

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

如何轻松获取当前 URL?

How can I easily get the current URL?

完整代码:

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 />'

请参阅 os.path 说,返回规范指定文件名的路径,消除路径中遇到的任何符号链接(如果操作系统支持它们)."

所以这不会返回 URL 而是文件路径,但我也可以使用它.

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

这篇关于使用 os.environ 在 Python 中获取当前 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-28 14:22:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1173711.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:environ   os   URL   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!