Building Python Modules on Windows

Python is a great cross platform application development environment. There is a  wide range of python modules (libraries) available. In most cases, module distributors supply platform specific installers which already built or at least they distribute the source code and let it to be built and installed by developers.

It is easy to build and install any python module in a Unix system. Because all *nix systems have a built in  development environment to build python modules ( or any language). So, just typing “python setup.py buil” and “python setup.py install” will be enough to get things done. Hard part is
having a windows. There are some options this issue:

1 -) Using MS Visual Studio ( I hate it)
2 -) Using Cygwin to get gcc for Windows
3 -) Using MinGW to get gcc for Windows

I choosed the 3rd option, it is the easiest one. What you have to do is get MinGW installer
and install required compilers and set it to your OS path. Mine is “C:MinGWbin”. Then, open a command prompt and cd to the source folder of the python module. Run build statement “python setup.py build -c mingw32″ this will build your module and get it ready to use. The build tool will generate a folder called “buildlib.win32-*.*” which contains compiled libraries. Copy those libraries to somewhere in python classpath. Mine is located at “C:Python25Lib”. That is it.You are ready to use your module. To test it, you can use pycrypto.

When Bogazici Guys Suck

When you need to access remote windows share over network, you just mount the share to your local linux folder like you do when mounting local disks.

mount -t cifs //10.187.12.107/e -o username=merturk /media/remote/ –verbose

This command should ask the password of the user then mounts the remote win share to your local. However, if you cannot see what you are supposed to see in your local mount point, simply inspect the kernel message using dmesg. If you see an output like what I saw today

CIFS VFS: Error connecting to IPv4 socket. Aborting operation
CIFS VFS: cifs_mount failed w/return code = -512

then you should start to think if all windows I.T. managers sucks or just windows guys who are graduated from Bogazici.

In Brief, this log message tells you, you have a network issue, probably you are banned to access windows shares in some subnetworks of your network like me ….

How Microsoft restricts developers on networking

Nowadays I’m interested in raw network programming which gives you ability to construct you building custom headers and makes sure that OS kernel does not modify these headers. I’ve started a VS.NET C++ console application (unmanaged) and typed several lines to test.

#include “stdafx.h”
#include

int _tmain(int argc, _TCHAR* argv[])
{
SOCKET s;
int optval = 1;

s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *)&optval, sizeof optval);

return 0;
}

I got an error message;

Error 1 error C2065: ‘IP_HDRINCL’ : undeclared identifier ..

After some quick research I’ve come up to this solution. If you are interested in low level networking like me, you should have Unix based boxes instead of Microsoft. The reason is simple; restrictions. Raw socket programming is available in all Unix, and Linux OSs on the other hand MS based OSs is restricted to NT based ones. None of Windows 95, 98, 98SE supported raw sockets. It becames available on Windows from Windows 2000, Windows XP but with Windows XP SP2 this feature is disabled. Quick summary from Microsoft’s specification for this issue:

The Windows implementation of TCP/IP still supports receiving traffic on raw IP sockets. However, the ability to send traffic over raw sockets has been restricted in two ways:

* TCP data cannot be sent over raw sockets.
*UDP datagrams with invalid source addresses cannot be sent over raw sockets. The IP source address for any outgoing UDP datagram must exist on a network interface or the datagram is dropped.

Microsoft encourages the developers to use their products but restricts them to full access. There is a conflict but this is Microsoft. They are doing their best !!!