free hit counter

Tuesday, May 25, 2010

finding IP addresses of all local adapters in Python

We've got an XmlRpcServer app written in Python, and when we wanted it to run on several different servers I thought I was being clever and rather than hardcode the IP address I'd just look it up; a quick search on the intertubes turned suggested:

serverIP= socket.gethostbyname(socket.gethostname())

This worked great on the original workstation we'd been running the script from, but when I put it on another machine it failed. Turns out, that machine has two network adapters, and you need to call the gethostbyname_ex() version (which the python docs don't have much to say about). That version of the call returns a list with all the adapters, and you need to search through them for the one you want. this is how I did it:

Python 2.5.2 (r252:60911, Dec 2 2008, 09:26:14)
[GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> socket.gethostbyname(socket.gethostname())
>>> socket.gethostbyaddr(socket.gethostname())
('bigServer', [], [''])
>>> socket.gethostbyname_ex(socket.gethostname())
('', [], ['', ''])
>>> def getIpAddrForSubnet(subnetStr):
... ipAddrs= socket.gethostbyname_ex(socket.gethostname())
... for value in ipAddrs[2]:
... if str(value).find(subnetStr) >= 0: return value
>>> getIpAddrForSubnet("192.168.1.")
>>> getIpAddrForSubnet("192.168.20.")

Labels: ,

Thursday, May 20, 2010

Using a semicolon isn't hard; I read a website about how to use them.

Super funny and informative guide on using semicolons:
How to use a semicolon - The Oatmeal

I never knew you could use them as super list separators when you're writing a list of stuff that contains commas like:

While searching for a good place to get a unicorn burger, I went to Seattle, Washington; Tokyo, Japan; and London, England.

Tuesday, May 18, 2010

QT on Android

wow. QT on Android. It's not lightning fast yet, but I imagine it will get there as QT's Lighthouse project gains more momentum.

All the gory details are here:

exit(-1) in matlab *does* work

Experimentation shows that this matlab command wrapped in a perl script does do what you would expect it to do, contrary to documentation.

$status= system("matlab.exe -nosplash -nojvm -nodisplay -wait -r \"exit(1)\"");

print "\n\nmatlab exited with status: $status\n";

Why should I be surprised?

Matlab is a terrible hodge-podge and that's why I think people like it so much. Just like Perl, or labyrinthine city streets. Still, it's not my kind of city, even if I have to visit all the time.

If you're silly enough to read the documentation, the exit function doesn't take an integer argument for the process return value:

>> help exit
EXIT Exit from MATLAB.
EXIT terminates MATLAB after running finish.m, if finish.m exists.
It is the same as QUIT and takes the same termination options.
For more information, see the help for QUIT.

See also quit.

Reference page in Help browser
doc exit

Further reading through the help and doc results of quit show no numeric arguments for process return values.

Labels: ,