How to get current URL in python web page?

前端 未结 2 1699
粉色の甜心
粉色の甜心 2021-01-02 03:36

I am a noob in Python. Just installed it, and spent 2 hours googleing how to get to a simple parameter sent in the URL to a Python script

Found this

Very hel

2条回答
  •  误落风尘
    2021-01-02 04:32

    If you don't have any libraries to do this for you, you can construct your current URL from the HTTP request that gets sent to your script via the browser.

    The headers that interest you are Host and whatever's after the HTTP method (probably GET, in your case). Here are some more explanations (first link that seemed ok, you're free to Google some more :).

    This answer shows you how to get the headers in your CGI script:

    If you are running as a CGI, you can't read the HTTP header directly, but the web server put much of that information into environment variables for you. You can just pick it out of os.environ[].

    If you're doing this as an exercise, then it's fine because you'll get to understand what's behind the scenes. If you're building anything reusable, I recommend you use libraries or a framework so you don't reinvent the wheel every time you need something.

提交回复
热议问题