freakin out over C++ module in python

N

nephish

lo there all !

i have a huge delima, i have to be able to connect to a data server and
recieve info from it. The servers software guys release some visual C++
modules that one can incorporate into a visual C++ project. Which is
great, but i am developing in linux, and only am very familliar with
python. i have tried to use swig to make a python module out of it, and
it wouldn't work. Tried to just compile them with gcc and that would
not work either. So, i have to go thru and try to re-create them in
python. So here is the trick, i don't know anything about C++, but this
has got to work.


and so, i guess the main question i have is.... is there a module or
program that will parse these classes and duplicate them for a python
module ? Some kind of code translator ? Or is that just way out there?

i would go thru it line by line, but i just dont know enough about C++,
how it pulls off a socket connection, etc.. and some of the things i
dont know how to do in python. like how to make an unsigned long init.

whew. so, i guess i could google each line... but if someone has an
easier alternative, i am all ears.. its about 400 lines total.

thanks.
sk
 
D

Diez B. Roggisch

and so, i guess the main question i have is.... is there a module or
program that will parse these classes and duplicate them for a python
module ? Some kind of code translator ? Or is that just way out there?

No, there isn't.
i would go thru it line by line, but i just dont know enough about C++,
how it pulls off a socket connection, etc.. and some of the things i
dont know how to do in python. like how to make an unsigned long init.

Use module struct.

Regards,

Diez
 
N

nephish

ok, well enough, looked at struct and it does seem to be what i am
after. for that anyway.
thanks, guess i will just have to take the time and pull it apart.

cheers
sk
 
M

Michael Ekstrand

i would go thru it line by line, but i just dont know enough about C++,
how it pulls off a socket connection, etc.. and some of the things i
dont know how to do in python. like how to make an unsigned long init.

The networking code in C++ should be at least vaguely similar to the
appropriate Python code using the socket module. Python's socket module
is a fairly thin wrapper around BSD sockets; its method names, etc.
match the socket system calls. If the C++ is using plain berkely-ish
socket code, you'll find connect(), send(), recv() calls (or something
similar - I don't know what Winsock calls them for sure); these map to
appropriate methods and functions in the socket module. If the C++ code
is using some C++ sockets wrapper, I don't know; it still would probably
be somewhat similar. Or similar enough that you can read it. Unless
they're somehow finagling sockets in to the iostreams library (possible,
but unlikely).

- Michael
 
J

Jay Parlar

ok, well enough, looked at struct and it does seem to be what i am
after. for that anyway.
thanks, guess i will just have to take the time and pull it apart.

I recommend you also take a look at http://pyconstruct.sourceforge.net/
It has the same purpose as 'struct', but is MUCH more Pythonic. I've
described Contstruct as "the replacement for 'struct' I've been looking
for since I started coding in Python".

Jay P.
 
N

nephish

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. thanks for the tips
gents....
 
S

Serge Orlov

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.
 
N

nephish

wow , thanks for the tips and the link.. i can at least see whats going
on here.
this project is beginning to look believable to me.

i have another question.. later , in this same class, after it goes
thru some error handling, it returns like this
return COM_SUCCESS;
but i cannot find where COM_SUCCESS is defined.
can something in C++ just represent itself?

thanks again for all your help.
 
S

Serge Orlov

wow , thanks for the tips and the link.. i can at least see whats going
on here.
this project is beginning to look believable to me.

i have another question.. later , in this same class, after it goes
thru some error handling, it returns like this
return COM_SUCCESS;
but i cannot find where COM_SUCCESS is defined.
can something in C++ just represent itself?

Unlikely. This is just C-style error handling. If function returns 0 it
means there were no error and if it return non-zero it means error. You
can do the same in python, but I would recommend using exceptions. If
there is an error you raise an exception, if there is no error, you
don't do anything.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top