portable Python ifconfig
Hi all, I'm looking for a portable (FreeBSD and Linux) way of getting typical ifconfig information into Python. Some research on the web brought me to Linux only solutions http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/439094 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/439093 which I didn't manage to port to FreeBSD (I'm not that experienced). Finally though, I found out about pyifconfig: http://mail.python.org/pipermail/python-list/1999-August/009274.html which compiles fine on Linux after changing to . On FreeBSD there is no SIOCGIFHWADDR, so I just commented out that part. I can now do In [1]: from pyifconfig import pyifconfig In [2]: pyifconfig('ath0') Out[2]: {'addr': '192.168.50.104', 'brdaddr': '192.168.50.255', 'hwaddr': '00:17:f2:4c:a5:0c', 'netmask': '255.255.255.0'} on Linux, and In [1]: from pyifconfig import pyifconfig In [2]: pyifconfig('vr1') Out[2]: {'addr': '192.168.50.1', 'brdaddr': '192.168.50.255', 'hwaddr': '\xff\xff', 'netmask': '255.255.255.0'} on FreeBSD. The problem now is that I get seemingly random information when I pass a non-existing interface, a down interface or an empty string to pyifconfig, which is very hard to figure out from inside a script: In [3]: pyifconfig('foobar') Out[3]: {'addr': '104.154.165.183', 'brdaddr': '104.154.165.183', 'hwaddr': '00:00:68:9a:a5:b7', 'netmask': '104.154.165.183'} so, any pointers here on how I can go on from this point? any help is appreciated -- regards, BBBart Wormwood : Calvin, how about you? Calvin : Hard to say ma'am. I think my cerebellum just fused. -- http://mail.python.org/mailman/listinfo/python-list
Re: portable Python ifconfig
It was 3 Mar 2007 18:43:57 -0800, when MonkeeSage wrote: > Bart, > > Can you try this and let us know if it works for FreeBSD? thanks for you suggestions! > import socket, fcntl, struct > > def _ifinfo(sock, addr, ifname): > iface = struct.pack('256s', ifname[:15]) > info = fcntl.ioctl(sock.fileno(), addr, iface) > if addr == 0x8927: > hwaddr = [] > for char in info[18:24]: > hwaddr.append(hex(ord(char))[2:]) > return ':'.join(hwaddr) > else: > return socket.inet_ntoa(info[20:24]) > > def ifconfig(ifname): > ifreq = {'ifname': ifname} > sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) > try: > ifreq['addr']= _ifinfo(sock, 0x8915, ifname) # SIOCGIFADDR > ifreq['brdaddr'] = _ifinfo(sock, 0x8919, ifname) # > SIOCGIFBRDADDR > ifreq['netmask'] = _ifinfo(sock, 0x891b, ifname) # > SIOCGIFNETMASK > ifreq['hwaddr'] = _ifinfo(sock, 0x8927, ifname) # > SIOCSIFHWADDR > except: > pass > sock.close() > return ifreq apparenlty, it doesn't. :-( I changes the except block into except Exception, e: print e and added if __name__ == '__main__': print ifconfig('ng0') print ifconfig('vr0') print ifconfig('vr2') print ifconfig('xl0') ng0 exists and has an IP (virtual interface created by mpd) vr0 exists but does not have an IP (phisical interface for mpd) vr2 exists and has an IP configured xl0 does not exist output: [Errno 25] Inappropriate ioctl for device {'ifname': 'ng0'} [Errno 25] Inappropriate ioctl for device {'ifname': 'vr0'} [Errno 25] Inappropriate ioctl for device {'ifname': 'vr2'} [Errno 25] Inappropriate ioctl for device {'ifname': 'xl0'} however, in Linux I get the following results: [Errno 99] Cannot assign requested address {'ifname': 'eth0'} {'hwaddr': '0:17:f2:4c:a5:c', 'ifname': 'ath0', 'netmask': '255.255.255.0', 'addr': '192.168.50.104', 'brdaddr': '192.168.50.255'} {'hwaddr': '0:0:0:0:0:0', 'ifname': 'tun0', 'netmask': '255.255.255.255', 'addr': '192.168.3.6', 'brdaddr': '0.0.0.0'} [Errno 19] No such device {'ifname': 'wielewoele'} which seems 100% correct. -- regards, BBBart Susie: You'd get a good grade without doing any work. Calvin: So? Susie: It's wrong to get rewards you haven't earned. Calvin: I've never heard of anyone who couldn't live with that. -- http://mail.python.org/mailman/listinfo/python-list
Re: portable Python ifconfig
It was Sun, 4 Mar 2007 02:38:58 +0500, when Bart Van Loon wrote: > Hi all, > > I'm looking for a portable (FreeBSD and Linux) way of getting typical > ifconfig information into Python. After lots of trial and error (I'm proficient in C at all), I puzzled togehter the following. It works (at least on FreeBSD and Linux), but is still fairly rough, as it returns an empty string when given a non existing or down interface. I'll clean it up in due time. :-) #include "Python.h" #include #include #include #include #include #include #include #include #include // parameters: string (interface name) // output: string (ip address of interface in decimal notation) PyObject * ipaddr(PyObject *self, PyObject *args) { char ip[ 200 ]; char *itf; if (! PyArg_ParseTuple(args, "s", &itf)) { PyErr_SetString(PyExc_Exception, "no interface given!"); return NULL; } struct ifaddrs *ifa = NULL, *ifp = NULL; if (getifaddrs (&ifp) < 0) { perror ("getifaddrs"); return NULL; } for (ifa = ifp; ifa; ifa = ifa->ifa_next) { socklen_t salen; if (ifa->ifa_addr->sa_family == AF_INET) salen = sizeof (struct sockaddr_in); else if (ifa->ifa_addr->sa_family == AF_INET6) salen = sizeof (struct sockaddr_in6); else continue; if (strncmp(ifa->ifa_name, itf, sizeof(itf))) { continue; } if (getnameinfo (ifa->ifa_addr, salen, ip, sizeof (ip), NULL, 0, NI_NUMERICHOST) < 0) { perror ("getnameinfo"); continue; } break; } freeifaddrs (ifp); return Py_BuildValue("s", ip); } static PyMethodDef ifconfig_methods[] = { {"ipaddr", (PyCFunction)ipaddr, METH_VARARGS, "ipaddr(string)\n"}, {NULL, NULL, 0, NULL} }; DL_EXPORT(void) initifconfig(void) { Py_InitModule3("ifconfig", ifconfig_methods, "Provides a function to get an ip address of a certain interface.\n"); } Inspiration came from and credit goes to the author of http://www.hungry.com/~alves/local-ip-in-C.html -- regards, BBBart Hobbes : How is the diorama coming along? Calvin : I'm almost finished. Hobbes : I don't see the roadrunner. Weren't you going to put one in? Calvin : See the cotton balls I glued down? Hobbes : Yeah? Calvin : The roadrunner just ran out of the scene leaving behind clouds of dust! -- http://mail.python.org/mailman/listinfo/python-list
Re: portable Python ifconfig
It was Sun, 4 Mar 2007 14:09:20 +0500, when Bart Van Loon wrote: > It was Sun, 4 Mar 2007 02:38:58 +0500, when Bart Van Loon wrote: >> Hi all, >> >> I'm looking for a portable (FreeBSD and Linux) way of getting typical >> ifconfig information into Python. > > After lots of trial and error (I'm proficient in C at all), I puzzled err... I'm NOT proficient in c at all :-) -- groetjes, BBBart Hobbes : How is the diorama coming along? Calvin : I'm almost finished. Hobbes : I don't see the roadrunner. Weren't you going to put one in? Calvin : See the cotton balls I glued down? Hobbes : Yeah? Calvin : The roadrunner just ran out of the scene leaving behind clouds of dust! -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for Java final/Ruby freeze functionality in Python
It was Sun, 04 Mar 2007 20:38:16 +0100, when Antoine De Groote wrote: > Hello, > > I've been googling for quite a while now but can't find anything about a > function/keyword to make a list (or something else) immutable. Could > anybody point me to docs about this matter or give me a reason why this > (apparently) doesn't exist in Python? what about a tuple? -- regards, BBBart My family is dysfunctional and my parents won't empower me. Consequently I'm not self actualized. -- Calvin -- http://mail.python.org/mailman/listinfo/python-list
cherrypy sub-process
Hi all, I have written a small program in Python which acts as a wrapper around mpd and natd on a FreeBSD system. It gets the status, restarts the processes, etc... Then, I created a tiny cherrypy webapp which provides a webinterface to this program. All works fine, but for the following problem: cherrypy listens on port 1234. whenever I browse to that port and invoke any action that involves (re)starting mpd or natd, which happens with commands.getstatusoutput() (which uses os.popen()), I cannot restart cherrypy without killing the mpd and/or natd processes spawned by it in the previous session, because port 1234 willl still be in use. lsof -i tcp tells me that it's exactly those mpd and/or natd processes which are keeping that port 1234 from freeing up after I (succesfully) kill cherrypy. Is there any explanation to this, or, even better, a solution? Thank you very much in advance. -- regards, BBBart "To make a bad day worse, spend it wishing for the impossible." -Calvin -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does this not work?
It was 2 Feb 2007 04:41:48 -0800, when alain wrote: > I tried the following: > > myobj=object() > myobj.newattr=5 > > results in: > > > Traceback (most recent call last): > File "", line 1, in ? > AttributeError: 'object' object has no attribute 'newattr' > > Any idea? I think it's because... object has no attribute 'newattr' what else is there left to say? try: myobj=object() print dir(myobj) does that contain 'myattr'? -- groetjes, BBBart "To make a bad day worse, spend it wishing for the impossible." -Calvin -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I print out in the standard output coloured lines
It was 2 Feb 2007 04:27:06 -0800, when [EMAIL PROTECTED] wrote: > print "Hello World!!" > > I want it in red colour. > > That's all. Use colour escape codes: print "\033[1;31mHello World\033[0m" That's all. :-) -- groetjes, BBBart Golly, I'd hate to have a kid like me! -- Calvin -- http://mail.python.org/mailman/listinfo/python-list
in place-ness of list.append
Hi all, I would like to find out of a good way to append an element to a list without chaing that list in place, like the builtin list.append() does. currently, I am using the following (for a list of integers, but it could be anything, really) #-- def addnumber(alist, num): """ work around the inplace-ness of .append """ mylist = alist[:] mylist.append(num) return mylist #-- and I am wondering if this is good practice or not. any advice on this matter? thanks! -- regards, BBBart "Someday I'll write my own philosophy book." -Calvin -- http://mail.python.org/mailman/listinfo/python-list
Re: in place-ness of list.append
It was Mon, 05 Feb 2007 11:00:50 GMT, when Kent Johnson wrote: > Bart Van Loon wrote: >> Hi all, >> >> I would like to find out of a good way to append an element to a list >> without chaing that list in place, like the builtin list.append() does. >> >> currently, I am using the following (for a list of integers, but it >> could be anything, really) >> >> #-- >> def addnumber(alist, num): >> """ work around the inplace-ness of .append """ >> mylist = alist[:] >> mylist.append(num) >> return mylist >> #-- > > Use + : > > In [1]: a=[1,2] > > In [2]: b=a+[3] > > In [3]: a > Out[3]: [1, 2] > > In [4]: b > Out[4]: [1, 2, 3] should have known that... thanks for you fast response! -- regards, BBBart "Who can fathom the feminine mind?" -Calvin "I like `em anyway" -Hobbes -- http://mail.python.org/mailman/listinfo/python-list
Re: in place-ness of list.append
It was Mon, 5 Feb 2007 05:01:28 -0600, when [EMAIL PROTECTED] wrote: > > Bart> #-- > Bart> def addnumber(alist, num): > Bart> """ work around the inplace-ness of .append """ > Bart> mylist = alist[:] > Bart> mylist.append(num) > Bart> return mylist > Bart> #-- > > Bart> and I am wondering if this is good practice or not. > > Bart> any advice on this matter? > > Such an operation will be O(N**2), why is that? > and thus expensive if performed frequently on lists of moderate > length. I've never been tempted to do this. Can you say a little > about why you'd want to do this? Knowing that, perhaps someone here > can point you in a more Pythonic direction. I am building a binary tree where each node is a list. the two children are the parent list + 1 and the parent list + 2. I am using it recursively as such: #--- def makescoretree(scorelist): """ generates the Tree of possible scores """ if waves(scorelist) >= MAXWAVES: return [] return Scoretree(scorelist, makescoretree(addnumber(scorelist, 1)), makescoretree(addnumber(scorelist, 6))) #--- but now I found out that I can do this easily like #--- def makescoretree(scorelist): """ generates the Tree of possible scores """ if waves(scorelist) >= MAXWAVES: return [] return Scoretree(scorelist, makescoretree(scorelist + [1]), makescoretree(scorelist + [6])) #--- -- regards, BBBart "Who can fathom the feminine mind?" -Calvin "I like `em anyway" -Hobbes -- http://mail.python.org/mailman/listinfo/python-list