pyfconfig — how to get ifconfig data without regular expressions


( ! ) Warning: fread() [function.fread]: Length parameter must be greater than 0 in /home/bbbart/www/htdocs/blog/wp-content/plugins/vimcolor/wp-vimcolor.php on line 105
Call Stack
#TimeMemoryFunctionLocation
10.000153204{main}( )../index.php:0
20.000256644require( '/home/bbbart/www/htdocs/blog/wp-blog-header.php' )../index.php:17
30.143213848252require_once( '/home/bbbart/www/htdocs/blog/wp-includes/template-loader.php' )../wp-blog-header.php:16
40.144113910844include( '/home/bbbart/www/htdocs/blog/wp-content/themes/svelt/single.php' )../template-loader.php:34
50.181314024424the_content( )../single.php:16
60.181614028484apply_filters( )../post-template.php:167
70.187514037648call_user_func_array ( )../plugin.php:166
80.187514037832vim_color( )../plugin.php:0
90.187514038168preg_replace ( )../wp-vimcolor.php:143
100.187714056804preg_replace ( )../wp-vimcolor.php:143
110.187714058836vimcolor_process_color( )../wp-vimcolor.php(143) : regexp code:53
120.195714076952fread ( )../wp-vimcolor.php:105

( ! ) Warning: fread() [function.fread]: Length parameter must be greater than 0 in /home/bbbart/www/htdocs/blog/wp-content/plugins/vimcolor/wp-vimcolor.php on line 105
Call Stack
#TimeMemoryFunctionLocation
10.000153204{main}( )../index.php:0
20.000256644require( '/home/bbbart/www/htdocs/blog/wp-blog-header.php' )../index.php:17
30.143213848252require_once( '/home/bbbart/www/htdocs/blog/wp-includes/template-loader.php' )../wp-blog-header.php:16
40.144113910844include( '/home/bbbart/www/htdocs/blog/wp-content/themes/svelt/single.php' )../template-loader.php:34
50.181314024424the_content( )../single.php:16
60.181614028484apply_filters( )../post-template.php:167
70.187514037648call_user_func_array ( )../plugin.php:166
80.187514037832vim_color( )../plugin.php:0
90.187514038168preg_replace ( )../wp-vimcolor.php:143
100.196714066412preg_replace ( )../wp-vimcolor.php:143
110.196714066644vimcolor_process_color( )../wp-vimcolor.php(143) : regexp code:1
120.201414068144fread ( )../wp-vimcolor.php:105

After reading this post about ifconfig output parsing by Kris, I remembered I once needed a cross-platform way to get the IP address of an interface in Python.

Of course I could just parse the output of `ifconfig`, but I really don’t like such ugly hacks. I guess I’ve got too many (bad) experiences with libwww-perl scripts I wrote for web harvesting various stuff. Basically, this is output parsing too, as HTML is generally the result of some server side script. Each time the webpage changed the way it looked (non-CSS changes) or worked, my scripts started to fail.

That’s when I decided I’ll always try to avoid such clumsy dependencies on third party software.

So, back to the Python question. I set out for a short adventure on comp.lang.python and came up with a solution after some fiddling: pyfconfig, a cross platform Python module to query for the IP address of an interface. Tested on FreeBSD x86, GNU/Linux x86 and GNU/Linux x86_64, with Python 2.4 and Python 2.5. Works just fine.

Compile with (for Python 2.5) and after an import pyfconfig, pyfconfig.ipaddr(’lo’) should return ‘127.0.0.1′ (YMMV).

No dependency on the output formatting of ifconfig. Less bugs.

Add a Comment   Trackback  

Add a Comment