ifconfig in python

Mark Wooding mdw at distorted.org.uk
Tue Jan 20 07:33:16 EST 2009


Дамјан Георгиевски <gdamjan at gmail.com> writes:

> Something *like*  this could work:
>
> 	myip = urllib2.urlopen('http://whatismyip.org/').read()

This is going to cause all manner of problems.

Firstly, many users are stuck behind NAT routers.  In this case, the
external service will report the address of the router, which is
probably useless -- certainly it will be for programs attempting to
communicate over a LAN.

Secondly, imagine the joy when overzealous ISPs decide that
whatismyip.org is peddling kiddiepr0n (as happened to Wikipedia last
month): then the service will report the address of ISP's censoring
proxy to thousands of otherwise unrelated users.

And that's before we get onto onion routers like Tor...

Here's an idea which might do pretty well.

In [1]: import socket as S
In [2]: s = S.socket(S.AF_INET, S.SOCK_DGRAM)
In [4]: s.connect(('192.0.2.1', 666))
In [5]: s.getsockname()
Out[5]: ('172.29.198.11', 46300)

(No packets were sent during this process: UDP `connections' don't need
explicit establishment.  The network 192.0.2.0/24 is reserved for use in
examples; selecting a local address should therefore exercise the
default route almost everywhere.  If there's a specific peer address or
network you want to communicate with, use that address explicitly.)

I have to wonder what the purpose of this is.  It's much better to have
the recipient of a packet work out the sender's address from the packet
(using recvfrom or similar) because that actually copes with NAT and so
on properly.

-- [mdw]



More information about the Python-list mailing list