freakin out over C++ module in python

Serge Orlov Serge.Orlov at gmail.com
Wed Apr 19 03:27:43 EDT 2006


nephish at xit.net wrote:
> pyconstruct looks cool. i dont know if the classes are using the plain
> berkely-ish code. i couldn't find anything in it that pointed to send()
> recv(), & such. i found other stuff like SetSocketOpt() and so on like
> this :
>
> long CClientSocket::ConnectToServer(LPCTSTR serverAddr, UINT port)
>
> {
>
>         BOOL socketOption = TRUE;
>
> 	const int rcvBuffsize = 64 *1024;
>
> 	const int sendBuffsize = 64*1024;
>
>
>
> 	if ( !Create() )
>
> 		return ERR_SOCKET_CREATE;
>
> 	int intvalue = sizeof(int);
>
> 	if(!( SetSockOpt(SO_DONTROUTE,&socketOption,sizeof(BOOL),SOL_SOCKET)
> &&
>
> 	    SetSockOpt(SO_RCVBUF,&rcvBuffsize,sizeof(int),SOL_SOCKET) &&
>
> 	    SetSockOpt(SO_SNDBUF,&sendBuffsize,sizeof(int),SOL_SOCKET) &&
>
> 	    SetSockOpt(TCP_NODELAY,&socketOption,sizeof(BOOL),SOL_SOCKET) &&
>
> 	    SetSockOpt(SO_KEEPALIVE,&socketOption,sizeof(BOOL),SOL_SOCKET)))
>
> 		return ERR_SOCKET_CREATE;
>
>
> i looked around for some of these and found most references to UDP. I
> guess its also a windows specific thing too.

Nope. It's a UNIX function:
http://www.opengroup.org/onlinepubs/007908799/xns/setsockopt.html
And corresponding Python stuff is in the socket module. The code above
can be roughly translated as

def ConnectToServer(self, serverAddr, port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.connection = s
    s.connect((serverAddr, port))
    s.setsockopt(socket.SOL_SOCKET, socket.SO_DONTROUTE, 1)
    s.setsockopt(socket.SOL_SOCKET, socket.TCP_NODELAY, 1)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 64*1024)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 64*1024)

Note, you don't need to handle errors in this method like C++ code,
instead catch socket.error exception where you handle *all* fatal
exceptions during connection setup.

With regards to your first question about automatic language
translation - don't even dream about it. IMHO, C++ is one of the most
complicated high-level languages for automatic translation.




More information about the Python-list mailing list