Re: ip address ( python )
I'm not sure exactly what you're trying to do here. If you're just trying to get your own machine's IP using python commands, maybe this little tweak (from http://mgltools.scripps.edu/document...p-using-python):
Code:
import socket
socket.gethostbyname(socket.gethostname())
socket.gethostbyname_ex(socket.gethostname())
I'm like hacks too, so I would probably try something like
Code:
os.system("ifconfig > file.txt")
and then read the text file, but that's obviously a crappy way to go about writing clean code.
Re: ip address ( python )
Nevermind, the gethostbyname_ex() doesn't work either. I'll keep poking around on the internet, but that hack of mine doesn't seem quite so terrible now...
Re: ip address ( python )
Ok, I think I got it this time (courtesy http://kryptoz.wordpress.com/2008/01...nd_ip_address/):
Code:
from socket import socket, SOCK_DGRAM, AF_INET
s = socket(AF_INET, SOCK_DGRAM)
s.connect(('google.com', 0))
s.getsockname()
I tried it out and it worked for me. Not sure why connecting to an external site (google here) is so important, but I ran across several people saying that it's necessary, so I suppose it is.
Re: ip address ( python )
Hey, thanks for answering. My actual problem was making socket.bind() to work with my ip address instead of localhost. I didn't really knew what was the problem, why socket.bind("",port) would not use my ip address so i asked why my ip retrieving method failed. Well, i tried your method and it worked. I can now see my ip and bind it so i can accept incoming connections.
Thanks a lot man, i really appreciate your help :D
Re: ip address ( python )
No problem! I'm glad that solution worked so well for you.