0

I am trying to send 'ip' variable from home.html to my main app.py python file, I have tried many things and none of them worked, Here is what I'm working with:

    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
    </script>

    <script>
        $.getJSON("https://api.ipify.org?format=json",
                                          function(data) {
            var ip = data.ip
            console.log(ip)
        })
    </script>
@app.route("/ip", methods=["GET", "POST"])
def get_ip():
        return render_template('home.html')
JaniniRami
  • 493
  • 3
  • 11
  • If you just want the IP address of the user doing the request, you can just grab this from Flask: https://stackoverflow.com/questions/3759981/get-ip-address-of-visitors-using-flask-for-python – Nick Apr 20 '20 at 14:09
  • You can send data back to Flask with a POST request. (See how to do it with jQuery: https://api.jquery.com/jquery.post/.) You can see here how to use the data: https://stackoverflow.com/questions/10434599/get-the-data-received-in-a-flask-request – Nick Apr 20 '20 at 14:15
  • I have tried it, It prints outthe local IP when getting it from Flask, and i want the public IP, so when using this javascript code I was able to print out the public IP to the console but all is left is sending it back to the python code to run some scans on it and that the part im asking about! – JaniniRami Apr 20 '20 at 14:17
  • Are you doing a reverse-proxy through something like nginx? – Nick Apr 20 '20 at 14:19
  • No I'm not!‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎ – JaniniRami Apr 20 '20 at 14:27
  • I assume you're testing it locally then. Once you get your app out on the internet, you should be able to fetch the IP of people through the shown method. It shows `localhost` or something similar, because you are on the same machine/local network. However, you _should_ be able to get the public IP with your current API approach with help from the above linked questions, but I don't think it's the best approach. – Nick Apr 20 '20 at 14:46

2 Answers2

1

I got it to work!, Here is the answer:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>

        $(document).ready(function() {

          $.getJSON("https://api.ipify.org?format=json",
                                          function(data) {
                  ip = data.ip
                  dd = {'ip': ip}
              //    console.log(ip);
              //    console.log(dd);

                  $.ajax({
                    data: JSON.stringify(dd),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                        type : 'POST',
                        url : '/'
                    })
                });
                })

</script>
JaniniRami
  • 493
  • 3
  • 11
0

If you just want to send the IP to the backend, then do the following. In JS:

$.ajax({
   dataType: 'string',
   url: '/',
   data: {
       ip: '2.2.2.2'
   },
   type: 'GET',
   success: function (data) {
       ...
       ...
   }
});

And, to receive the IP in Flask,

ip = request.args.get("ip")