I have a PC behind a firewall, and I'm trying to programmaticall y
determine the IP address visible from outside the firewall.
If I do this:
'127.0.0.1'
('localhost.loc aldomain', ['localhost'], ['127.0.0.1'])
I get my internal IP address, which is not what I want.
Other tricks, like parsing the output of os.system('/sbin/ifconfig eth0')
also give me my internal IP address.
I found this post on comp.lang.pytho n a few years ago:
which seems like it _should_ do what I want, but it doesn't: I get an
exception.
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.4/UserList.py", line 28, in __getitem__
def __getitem__(sel f, i): return self.data[i]
IndexError: list index out of range
I don't know how to fix it so that it works.
Can anyone help me fix that code snippet, or suggest another (better) way
to get the externally visible IP address?
--
Steven D'Aprano
determine the IP address visible from outside the firewall.
If I do this:
>>import socket
>>socket.gethos tbyname(socket. gethostname())
>>socket.gethos tbyname(socket. gethostname())
>>socket.gethos tbyname_ex(sock et.gethostname( ))
I get my internal IP address, which is not what I want.
Other tricks, like parsing the output of os.system('/sbin/ifconfig eth0')
also give me my internal IP address.
I found this post on comp.lang.pytho n a few years ago:
which seems like it _should_ do what I want, but it doesn't: I get an
exception.
>>from httplib import HTTPConnection
>>from xml.dom.ext.rea der.Sax import FromXmlStream
>>conn = HTTPConnection( 'xml.showmyip.c om')
>>conn.request( 'GET', '/')
>>doc = FromXmlStream(c onn.getresponse ())
>>print doc.getElements ByTagName('ip')[0].firstChild.dat a
>>from xml.dom.ext.rea der.Sax import FromXmlStream
>>conn = HTTPConnection( 'xml.showmyip.c om')
>>conn.request( 'GET', '/')
>>doc = FromXmlStream(c onn.getresponse ())
>>print doc.getElements ByTagName('ip')[0].firstChild.dat a
File "<stdin>", line 1, in ?
File "/usr/lib/python2.4/UserList.py", line 28, in __getitem__
def __getitem__(sel f, i): return self.data[i]
IndexError: list index out of range
>>conn.close( )
Can anyone help me fix that code snippet, or suggest another (better) way
to get the externally visible IP address?
--
Steven D'Aprano
Comment