hi all...

how to find the localhost ip address in python

import socket
socket.gethostbyname(socket.gethostname())

gives me 127.0.0.1 instead of my ip address

any help...........

It's an ill-posed problem, as mathematicians say. Why do you need your ip address ? Why doesn't 127.0.0.1 suffice ? Do you want the ip address on the local network (you should know it), or your public ip address ? There is an old trick

import urllib2
ip = urllib2.urlopen("http://whatismyip.com/automation/n09230945.asp").read()
print(ip)

but it depends on the server at whatsmyip.com to be up and running...
Other problems, what is your os ? In linux you can get your local ip with sudo /sbin/ifconfig, in windows you could use ipconfig, etc.

thanks Grib....

I need to maintain a list of servers that host the software release folders. I need my script to be generic, i.e., it can be run on any of the servers.
I need to check the ip address of the host on which am running the script, and for the servers in list other than my host, i need to do a telnet to connect to it and get the folder,if present do an FTP upload.

I got this Crude method of creating a dummy socket and getting my IP address from ASPN Python cookbook

import socket

s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('google.com',80))
return s.getsocketname()[0]

and it helped me....

commented: Nice trick ! +4

thanks Grib....

I need to maintain a list of servers that host the software release folders. I need my script to be generic, i.e., it can be run on any of the servers.
I need to check the ip address of the host on which am running the script, and for the servers in list other than my host, i need to do a telnet to connect to it and get the folder,if present do an FTP upload.

I got this Crude method of creating a dummy socket and getting my IP address from ASPN Python cookbook

import socket

s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('google.com',80))
return s.getsocketname()[0]

and it helped me....

It's getsockname(). Nice trick, I'll add it to my notes.

Did you try to run ifconfig or ipconfig with subprocess.Popen ?

oh sorry....yes it's getsockname() :)

oh sorry....yes it's getsockname() :)

yes..have tried it...but ifconfig gives a lot of info and we need to parse the output to get the ip address

yes..have tried it...but ifconfig gives a lot of info and we need to parse the output to get the ip address

You only need to match the regex r"adr[:](\d+(?:\.\d+){3})" . The benefit is that you don't need to connect to a remote address. But if you want a cross platform code, we should also examine the output of ipconfig in windows.

You only need to match the regex r"adr[:](\d+(?:\.\d+){3})" . The benefit is that you don't need to connect to a remote address. But if you want a cross platform code, we should also examine the output of ipconfig in windows.

yes..... it is enlightening..

Thanx a lot Grib......... :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.