470,647 Members | 1,248 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,647 developers. It's quick & easy.

freakin out over C++ module in python

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

Apr 18 '06 #1
9 1588
> 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
Apr 18 '06 #2
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

Apr 18 '06 #3

On Apr 18, 2006, at 4:43 AM, ne*****@xit.net wrote:
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.

Apr 18 '06 #4
ne*****@xit.net wrote:
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

--
mouse, n: a device for pointing at the xterm in which you want to type.
-- Fortune
Visit me on the Web: http://www.elehack.net
Apr 18 '06 #5
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....

Apr 19 '06 #6
ne*****@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/...etsockopt.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.

Apr 19 '06 #7
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.

Apr 19 '06 #8

ne*****@xit.net wrote:
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.

Apr 20 '06 #9
sounds cool, most stuff i write is strife with try - except

thanks for the tip

-sk

Apr 20 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

54 posts views Thread by Brandon J. Van Every | last post: by
5 posts views Thread by sophie_newbie | last post: by
1 post views Thread by alain MONTMORY | last post: by
17 posts views Thread by Gal Diskin | last post: by
13 posts views Thread by stef mientki | last post: by
1 post views Thread by Sami Vaisanen | last post: by
1 post views Thread by Korara | last post: by
reply views Thread by warner | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.