How to display User's IP in the GE

Hello everyone. I am using agoose77’s networking add-on to make a multiplayer game, and was wondering if on my hosting scene, if there was a script i could use to display the server’s ip address? thank you

Interesting question!, Public IP or Local IP ?

You should take a look at my Networking / Sockets tutorial:

It’s long, but covers things in fair detail, and your question is also answered; You get the address from the recvfrom method on the socket (along with the data, which comes first).

@juancarlospaco: public IP

@Goran: would it work with the network add-on or do i have to completely re-write my scripts?

I’ve got a script that does just this. I’ll dig it out. it uses a page on my website:
http://hostilestudios.com/matter/server/ip.php
so you can just use some python to read it


import urllib.request

def get_ip():
    url = "http://hostilestudios.com/matter/server/ip.php"
    page = urllib.request.urlopen(url)
    page_bytes = page.read()
    page.close()
    page_contents = page_bytes.decode('utf-8')
    return eval(page_contents)
    
get_ip()

so do i just connect a text string to the python script? or how do i use it?

The last line:
get_ip()
returns the ip of the computer. So use some python to set the value of a text object

(assuming you already set a variable ‘text_object’ as the text gameobject reference:


text_object.text = get_ip()

my text object is named “text”, and this is my code,

import urllib.request

def get_ip():
url = “http://hostilestudios.com/matter/server/ip.php
page = urllib.request.urlopen(url)
page_bytes = page.read()
page.close()
page_contents = page_bytes.decode(‘utf-8’)
return eval(page_contents)

text_object.text = get_ip()

what am i doing wrong?

You’ve not told it what the text object is. Like i said, you need to set a variable called “text_object” that points to the game object. You should learn a little python before jumping into a game like this…

e.g


import urllib.request
import bge

def get_ip():
    url = "http://hostilestudios.com/matter/server/ip.php"
    page = urllib.request.urlopen(url)
    page_bytes = page.read()
    page.close()
    page_contents = page_bytes.decode('utf-8')
    return eval(page_contents)
    

scene = bge.logic.getCurrentScene()
objects = scene.objects

text_object = objects['Text']

text_object.text = get_ip()


once again, thank you for the help :slight_smile: you’re right! i probably should look more into python

it is the wrong IP address, it doesn’t connect when i put it in, but when i find my ip from the cmd prompt or from networking preferences, that seems to work

Try this (same as agoose but shorter and nicer ;D):

def getNETip():
         import urllib.request
    
         url = urllib.request.urlopen('http://automation.whatismyip.com/n09230945.asp')
    
         return (str(url.read(),'utf-8'))

You call the function in python. ( ex: text = getNETip() )

that’s because it returns a Web ip. you want to use the Web ip over the Internet, and the lan ip over lan