Search code examples
python-3.xrequestsanic

How to get the referral website url in Sanic


I am trying to get the referral website URL/server name (not the IP address) from the incoming request in Sanic. However, so far, I have managed to get the IP and port of the connection from the request header. Is there any python package or built-in functionality in Sanic?


Solution

  • Try this:

    referrer = request.headers.get("Referer")  # url
    u = urlparse(referrer)
    print(u.netloc)  # server name
    

    Docs for Referer header - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer
    Docs for urlparse - https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlparse