Get current URL in Python

前端 未结 7 1984
独厮守ぢ
独厮守ぢ 2020-12-05 18:49

How would i get the current URL with Python,

I need to grab the current URL so i can check it for query strings e.g

requested_url = \"URL_HERE\"

ur         


        
相关标签:
7条回答
  • 2020-12-05 18:49

    Try this

    import os
    url = os.environ['HTTP_HOST']
    
    0 讨论(0)
  • 2020-12-05 18:52

    I couldn't get the other answers to work, but here is what worked for me:

        url = os.environ['HTTP_HOST']
        uri = os.environ['REQUEST_URI']
        return url + uri
    
    0 讨论(0)
  • 2020-12-05 18:53

    This is how I capture in Python 3 from CGI (A) URL, (B) GET parameters and (C) POST data:

    =======================================================

    import sys, os, io

    CAPTURE URL

    myDomainSelf = os.environ.get('SERVER_NAME')

    myPathSelf = os.environ.get('PATH_INFO')

    myURLSelf = myDomainSelf + myPathSelf

    CAPTURE GET DATA

    myQuerySelf = os.environ.get('QUERY_STRING')

    CAPTURE POST DATA

    myTotalBytesStr=(os.environ.get('HTTP_CONTENT_LENGTH'))

    if (myTotalBytesStr == None):

    myJSONStr = '{"error": {"value": true, "message": "No (post) data received"}}'
    

    else:

    myTotalBytes=int(os.environ.get('HTTP_CONTENT_LENGTH'))
    
    myPostDataRaw = io.open(sys.stdin.fileno(),"rb").read(myTotalBytes)
    
    myPostData = myPostDataRaw.decode("utf-8")
    

    Write RAW to FILE

    mySpy = "myURLSelf: [" + str(myURLSelf) + "]\n"

    mySpy = mySpy + "myQuerySelf: [" + str(myQuerySelf) + "]\n"

    mySpy = mySpy + "myPostData: [" + str(myPostData) + "]\n"

    You need to define your own myPath here

    myFilename = "spy.txt"

    myFilePath = myPath + "\" + myFilename

    myFile = open(myFilePath, "w")

    myFile.write(mySpy)

    myFile.close()

    =======================================================

    Here are some other useful CGI environment vars:

    AUTH_TYPE

    CONTENT_LENGTH

    CONTENT_TYPE

    GATEWAY_INTERFACE

    PATH_INFO

    PATH_TRANSLATED

    QUERY_STRING

    REMOTE_ADDR

    REMOTE_HOST

    REMOTE_IDENT

    REMOTE_USER

    REQUEST_METHOD

    SCRIPT_NAME

    SERVER_NAME

    SERVER_PORT

    SERVER_PROTOCOL

    SERVER_SOFTWARE

    ============================================

    I am using these methods running Python 3 on Windows Server with CGI via MIIS.

    Hope this can help you.

    0 讨论(0)
  • 2020-12-05 18:55

    Try this:

    self.request.url
    

    Also, if you just need the querystring, this will work:

    self.request.query_string
    

    And, lastly, if you know the querystring variable that you're looking for, you can do this:

    self.request.get("name-of-querystring-variable")
    
    0 讨论(0)
  • 2020-12-05 18:56

    requests module has 'url' attribute, that is changed url.
    just try this:

    import requests
    current_url=requests.get("some url").url
    print(current_url)
    
    0 讨论(0)
  • 2020-12-05 19:00

    For anybody finding this via google,

    i figured it out,

    you can get the query strings on your current request using:

    url_get = self.request.GET
    

    which is a UnicodeMultiDict of your query strings!

    0 讨论(0)
提交回复
热议问题