From 26618a2a9d8215f664e53ca92e35c9c443215467 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 26 Aug 2012 22:39:35 +0530 Subject: [PATCH] Add a function to get information about all network interfaces --- setup/installer/linux/freeze2.py | 2 +- setup/installer/windows/notes.rst | 13 +++++++++++++ src/calibre/utils/mdns.py | 13 +++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/setup/installer/linux/freeze2.py b/setup/installer/linux/freeze2.py index 3b801c190e..13c02cca12 100644 --- a/setup/installer/linux/freeze2.py +++ b/setup/installer/linux/freeze2.py @@ -15,7 +15,7 @@ from setup import Command, modules, basenames, functions, __version__, \ SITE_PACKAGES = ['PIL', 'dateutil', 'dns', 'PyQt4', 'mechanize', 'sip.so', 'BeautifulSoup.py', 'cssutils', 'encutils', 'lxml', 'sipconfig.py', 'xdg', 'dbus', '_dbus_bindings.so', 'dbus_bindings.py', - '_dbus_glib_bindings.so'] + '_dbus_glib_bindings.so', 'netifaces.so'] QTDIR = '/usr/lib/qt4' QTDLLS = ('QtCore', 'QtGui', 'QtNetwork', 'QtSvg', 'QtXml', 'QtWebKit', 'QtDBus') diff --git a/setup/installer/windows/notes.rst b/setup/installer/windows/notes.rst index e29b205de6..d0f6eb67ba 100644 --- a/setup/installer/windows/notes.rst +++ b/setup/installer/windows/notes.rst @@ -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 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 --------- diff --git a/src/calibre/utils/mdns.py b/src/calibre/utils/mdns.py index 9232aab994..6140435e46 100644 --- a/src/calibre/utils/mdns.py +++ b/src/calibre/utils/mdns.py @@ -4,9 +4,22 @@ __copyright__ = '2009, Kovid Goyal ' __docformat__ = 'restructuredtext en' import socket, time, atexit +from collections import defaultdict _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(): 'Get IP address of interface used to connect to the outside world' try: