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 | ||||
| # | Time | Memory | Function | Location |
| 1 | 0.0001 | 53236 | {main}( ) | ../index.php:0 |
| 2 | 0.0002 | 56676 | require( '/home/bbbart/www/htdocs/blog/wp-blog-header.php' ) | ../index.php:17 |
| 3 | 0.1781 | 16860744 | require_once( '/home/bbbart/www/htdocs/blog/wp-includes/template-loader.php' ) | ../wp-blog-header.php:16 |
| 4 | 0.1798 | 16923324 | include( '/home/bbbart/www/htdocs/blog/wp-content/themes/svelt/single.php' ) | ../template-loader.php:43 |
| 5 | 0.2156 | 17042536 | the_content( ) | ../single.php:16 |
| 6 | 0.2157 | 17046392 | apply_filters( ) | ../post-template.php:169 |
| 7 | 0.2217 | 17058056 | call_user_func_array ( ) | ../plugin.php:166 |
| 8 | 0.2217 | 17058240 | vim_color( ) | ../plugin.php:0 |
| 9 | 0.2217 | 17058576 | preg_replace ( ) | ../wp-vimcolor.php:143 |
| 10 | 0.2219 | 17078824 | preg_replace ( ) | ../wp-vimcolor.php:143 |
| 11 | 0.2219 | 17081084 | vimcolor_process_color( ) | ../wp-vimcolor.php(143) : regexp code:53 |
| 12 | 0.2315 | 17099240 | fread ( ) | ../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 | ||||
| # | Time | Memory | Function | Location |
| 1 | 0.0001 | 53236 | {main}( ) | ../index.php:0 |
| 2 | 0.0002 | 56676 | require( '/home/bbbart/www/htdocs/blog/wp-blog-header.php' ) | ../index.php:17 |
| 3 | 0.1781 | 16860744 | require_once( '/home/bbbart/www/htdocs/blog/wp-includes/template-loader.php' ) | ../wp-blog-header.php:16 |
| 4 | 0.1798 | 16923324 | include( '/home/bbbart/www/htdocs/blog/wp-content/themes/svelt/single.php' ) | ../template-loader.php:43 |
| 5 | 0.2156 | 17042536 | the_content( ) | ../single.php:16 |
| 6 | 0.2157 | 17046392 | apply_filters( ) | ../post-template.php:169 |
| 7 | 0.2217 | 17058056 | call_user_func_array ( ) | ../plugin.php:166 |
| 8 | 0.2217 | 17058240 | vim_color( ) | ../plugin.php:0 |
| 9 | 0.2217 | 17058576 | preg_replace ( ) | ../wp-vimcolor.php:143 |
| 10 | 0.2329 | 17087788 | preg_replace ( ) | ../wp-vimcolor.php:143 |
| 11 | 0.2329 | 17088020 | vimcolor_process_color( ) | ../wp-vimcolor.php(143) : regexp code:1 |
| 12 | 0.2412 | 17089540 | fread ( ) | ../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.