How To Get A Client IP Address In DJANGO Using Python?
I have a django website developed with python programming. I want to store the viewers unique ip address when someone access my site. For that I included a code like below. def get
Solution 1:
you are getting 127.0.0.1
because you are visiting the page with a loopback adress in your local machine
when you deploy your app and open it in a browser, you will get your public IP.
Solution 2:
127.0.0.1
is a special IP address used for "loopback" connections. This means that your local machine is both the client AND the host. You have a few options if this isn't acceptable:
- Add a middlewear plugin to modify the HTTP_X_FORWARDED_FOR header (testing purposes only)
- Issue your client requests from a another host (a separate box or a virtual machine on the local host)
Instead of using a browser, use curl and spoof the appropriate header:
curl --header "X-Forwarded-For: 192.168.1.1" "http://127.0.0.1"
Solution 3:
<script type="text/javascript" src="http://l2.io/ip.js?var=myip"></script>
<script>
function systemip(){`enter code here`
document.getElementById("ip").value = myip
console.log(document.getElementById("ip").value)
}
</script>
Post a Comment for "How To Get A Client IP Address In DJANGO Using Python?"