[CentralOH] SheevaPlug - Python app right out of the box!

Eric Floehr eric at intellovations.com
Thu Apr 1 16:48:37 CEST 2010


A little more on how the results are returned from
netifaces.ifaddresses('eth0')...

The result is a dictionary keyed by the address family type.  The
types are defined by the constants in the netifaces module.  On my
machine example below, there are three address families: AF_INET,
AF_INET6, and AF_LINK.  AF_INET is your IPv4 addresses, AF_INET6 is
IPv6, and AF_LINK is the physical link (i.e. MAC address).

Each address family value is an array, with one dictionary entry per
address.  On my machine there is only one NIC, so there is just one
address in each address family.  Then within each dictionary are the
particulars of the address.

So in the example:
netifaces.ifaddresses('eth0')[netifaces.AF_INET][0]['addr']

It is saying, for interface 'eth0', get me the first IPv4 address.  If
I wanted the MAC address of that interface, I could replace AF_INET
with AF_LINK.  If I wanted the loopback address, I could just:

In [11]: netifaces.ifaddresses('lo')[netifaces.AF_INET][0]['addr']
Out[11]: '127.0.0.1'

-Eric



On Thu, Apr 1, 2010 at 10:31 AM, Eric Floehr <eric at intellovations.com> wrote:
> There is also a third-party module called netifaces (available on the
> cheeseshop (so just "easy_install netifaces") which wraps it all up
> and makes it cross-platform:
>
> http://pypi.python.org/pypi/netifaces
>
> Example from my machine:
>
> In [1]: import netifaces
>
> In [2]: netifaces.interfaces()
> Out[2]: ['lo', 'eth0', 'vmnet1', 'vmnet8']
>
> In [3]: netifaces.ifaddresses('eth0')
> Out[3]:
> {2: [{'addr': '192.168.13.13',
>      'broadcast': '192.168.13.255',
>      'netmask': '255.255.255.0'}],
>  10: [{'addr': 'fe80::224:e8ff:fe45:87a4%eth0',
>       'netmask': 'ffff:ffff:ffff:ffff::'}],
>  17: [{'addr': '00:24:e8:45:87:a4', 'broadcast': 'ff:ff:ff:ff:ff:ff'}]}
>
> In [4]: netifaces.ifaddresses('eth0')[netifaces.AF_INET][0]['addr']
> Out[4]: '192.168.13.13'
>
> -Eric
>
>
>
>
> On Thu, Apr 1, 2010 at 10:12 AM, Jon Miller <jonebird at gmail.com> wrote:
>> I felt bad for supplying a shell version... another trick you can do
>> is to use the 'socket' module. If you happen to have a host which you
>> can connect to on the network, then this trick might be good for you.
>> In my example, I'll connect to a  fictious server named 'jumpbox' on
>> port 22:
>>
>> import socket
>> s1 = socket.socket()
>> s.connect(('jumpbox', 22))
>> print 'My IP is %s' % s.getsockname()[0]
>>
>> -- Jon Miller
>>
>> On Thu, Apr 1, 2010 at 9:36 AM, Jon Miller <jonebird at gmail.com> wrote:
>>> This is what I do to determine my frontend IP:
>>>  INTERFACE=$(/sbin/ip route list default | sed -n '/^default /s/.*
>>> dev \([a-z0-9]*\) $/\1/p' | head -1)
>>>  /sbin/ip addr show dev $INTERFACE primary | sed -n '/inet /s/.*inet
>>> \([^ ]*\) .*$/\1/p' | sed 's|/24||g'
>>>
>>> -- Jon
>>>
>>> On Thu, Apr 1, 2010 at 9:03 AM, Mark Erbaugh <mark at microenh.com> wrote:
>>>> Has anyone in the COhPy messed with a SheevaPlug? This is a "plug computer" that comes preloaded with a version of Ubuntu 9.04 and, more importantly, Python 2.5 is installed. I have a small (but useful) web-based app written using webpy. I simply copied the files to a USB memory stick. Once I mounted the memory stick on the SheevaPlug, I issued the command 'python main.py' and my app was off an running. Pretty cool!
>>>>
>>>> I have a question related to my webpy app.  Is there some python code for determining the IP address on which it is listening?  When the webpy app runs, it always displays http://0.0.0.0:8080/.  I end up having to use ifconfig to get the IP address.
>>>>
>>>> Mark
>>>> _______________________________________________
>>>> CentralOH mailing list
>>>> CentralOH at python.org
>>>> http://mail.python.org/mailman/listinfo/centraloh
>>>>
>>>
>> _______________________________________________
>> CentralOH mailing list
>> CentralOH at python.org
>> http://mail.python.org/mailman/listinfo/centraloh
>>
>


More information about the CentralOH mailing list