Getting Public Ip adrress

Im using socket to get client Ip address to save values to a database but socket returning wrong thing

this is what it is returning


this is what it should be returning
image

I made the same thing but with JavaScript and it worked perfectly

but i want to do it in python Because j Don’t know JavaScript
link to JavaScript Repl https://replit.com/@PrestonCurtis1/WhatsMyIp#index.html
link to python Repl https://replit.com/@PrestonCurtis1/Get-ip#main.py
If anyone know why this is not working please let me know i would greatly appreciate it

I risked myself to see what’s wrong with your program. Turns out your python program is returning the same ip. it might be returning you the server’s ip.

Hey @PrestonCurtis1,

maybe this code works :

import socket   

hostname=socket.gethostname()   
IPAddr=socket.gethostbyname(hostname)   
print("Your Computer Name is:"+hostname)   
print("Your Computer IP Address is:"+IPAddr) 

If you mean on replit:
When running code like this, the code gets the server’s ip, in this case the virtual machine running your repl’s id. If you want to get a user’s ip, you need to open an output, for example Flask. Here is some flask code that allows you to get the users ip and use it for something:

from replit import db #To save ips
from flask import Flask, request, render_template

app = Flask(__name__)

@app.route('/')
def index():
    ip = request.headers['X-Forwarded-For']
    db['ips'].append(ip)
app.run(host='0.0.0.0', port=81)

You need to render an output for this btw.
Example of this code working (not my code, but MNA4 did it well):
https://replit.com/@MNA5/Sussy-repl?v=1#main.py

2 Likes

it return the same ip But thanks

Try my code it should work

What is My_Token for

Edit: was removed because variable is not necessary.

Original Post

It is used to generate a random token to authenticate connections (that is what i think it is, not an expert).

oh oops i forgot to remove that xD
Normally the token was for an api key for a ip geolocations api

Thanks @MiloCat it worked

you’re welcome!
Feel free to mark my post as the solution if you think that it was correct

lol, thanks for featuring one of my repls :stuck_out_tongue:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.