Add a function to get information about all network interfaces

This commit is contained in:
Kovid Goyal 2012-08-26 22:39:35 +05:30
parent a43ca92e02
commit 26618a2a9d
3 changed files with 27 additions and 1 deletions

View File

@ -15,7 +15,7 @@ from setup import Command, modules, basenames, functions, __version__, \
SITE_PACKAGES = ['PIL', 'dateutil', 'dns', 'PyQt4', 'mechanize', SITE_PACKAGES = ['PIL', 'dateutil', 'dns', 'PyQt4', 'mechanize',
'sip.so', 'BeautifulSoup.py', 'cssutils', 'encutils', 'lxml', 'sip.so', 'BeautifulSoup.py', 'cssutils', 'encutils', 'lxml',
'sipconfig.py', 'xdg', 'dbus', '_dbus_bindings.so', 'dbus_bindings.py', 'sipconfig.py', 'xdg', 'dbus', '_dbus_bindings.so', 'dbus_bindings.py',
'_dbus_glib_bindings.so'] '_dbus_glib_bindings.so', 'netifaces.so']
QTDIR = '/usr/lib/qt4' QTDIR = '/usr/lib/qt4'
QTDLLS = ('QtCore', 'QtGui', 'QtNetwork', 'QtSvg', 'QtXml', 'QtWebKit', 'QtDBus') QTDLLS = ('QtCore', 'QtGui', 'QtNetwork', 'QtSvg', 'QtXml', 'QtWebKit', 'QtDBus')

View File

@ -348,6 +348,19 @@ Remove the CORE_xlib, UTIL_Imdisplay and CORE_Magick++ projects.
F7 for build project, you will get one error due to the removal of xlib, ignore F7 for build project, you will get one error due to the removal of xlib, ignore
it. it.
netifaces
------------
Download the source tarball from http://alastairs-place.net/projects/netifaces/
Rename netifaces.c to netifaces.cpp and make the same change in setup.py
Run
python setup.py build
cp build/lib.win32-2.7/netifaces.pyd /cygdrive/c/Python27/Lib/site-packages/
calibre calibre
--------- ---------

View File

@ -4,9 +4,22 @@ __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
import socket, time, atexit import socket, time, atexit
from collections import defaultdict
_server = None _server = None
def get_all_ips():
''' Return a mapping of interface names to the configuration of the
interface, which includes the ip address, netmask and broadcast addresses
'''
import netifaces
all_ips = defaultdict(list)
if hasattr(netifaces, 'AF_INET'):
for x in netifaces.interfaces():
for c in netifaces.ifaddresses(x).get(netifaces.AF_INET, []):
all_ips[x].append(c)
return dict(all_ips)
def _get_external_ip(): def _get_external_ip():
'Get IP address of interface used to connect to the outside world' 'Get IP address of interface used to connect to the outside world'
try: try: