freakin out over C++ module in python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nephish@xit.net

    #1

    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

  • Diez B. Roggisch

    #2
    Re: freakin out over C++ module in python

    > and so, i guess the main question i have is.... is there a module or[color=blue]
    > 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?[/color]

    No, there isn't.
    [color=blue]
    > 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.[/color]

    Use module struct.

    Regards,

    Diez

    Comment

    • nephish@xit.net

      #3
      Re: freakin out over C++ module in python

      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

      Comment

      • Jay Parlar

        #4
        Re: freakin out over C++ module in python


        On Apr 18, 2006, at 4:43 AM, nephish@xit.net wrote:
        [color=blue]
        > 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.
        >[/color]

        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.

        Comment

        • Michael Ekstrand

          #5
          Re: freakin out over C++ module in python

          nephish@xit.net wrote:[color=blue]
          > 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.[/color]

          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

          Comment

          • nephish@xit.net

            #6
            Re: freakin out over C++ module in python

            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_CREA TE;

            int intvalue = sizeof(int);

            if(!( SetSockOpt(SO_D ONTROUTE,&socke tOption,sizeof( BOOL),SOL_SOCKE T)
            &&

            SetSockOpt(SO_R CVBUF,&rcvBuffs ize,sizeof(int) ,SOL_SOCKET) &&

            SetSockOpt(SO_S NDBUF,&sendBuff size,sizeof(int ),SOL_SOCKET) &&

            SetSockOpt(TCP_ NODELAY,&socket Option,sizeof(B OOL),SOL_SOCKET ) &&

            SetSockOpt(SO_K EEPALIVE,&socke tOption,sizeof( BOOL),SOL_SOCKE T)))

            return ERR_SOCKET_CREA TE;


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

            Comment

            • Serge Orlov

              #7
              Re: freakin out over C++ module in python

              nephish@xit.net wrote:[color=blue]
              > 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_CREA TE;
              >
              > int intvalue = sizeof(int);
              >
              > if(!( SetSockOpt(SO_D ONTROUTE,&socke tOption,sizeof( BOOL),SOL_SOCKE T)
              > &&
              >
              > SetSockOpt(SO_R CVBUF,&rcvBuffs ize,sizeof(int) ,SOL_SOCKET) &&
              >
              > SetSockOpt(SO_S NDBUF,&sendBuff size,sizeof(int ),SOL_SOCKET) &&
              >
              > SetSockOpt(TCP_ NODELAY,&socket Option,sizeof(B OOL),SOL_SOCKET ) &&
              >
              > SetSockOpt(SO_K EEPALIVE,&socke tOption,sizeof( BOOL),SOL_SOCKE T)))
              >
              > return ERR_SOCKET_CREA TE;
              >
              >
              > i looked around for some of these and found most references to UDP. I
              > guess its also a windows specific thing too.[/color]

              Nope. It's a UNIX function:

              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(s ocket.AF_INET, socket.SOCK_STR EAM)
              self.connection = s
              s.connect((serv erAddr, port))
              s.setsockopt(so cket.SOL_SOCKET , socket.SO_DONTR OUTE, 1)
              s.setsockopt(so cket.SOL_SOCKET , socket.TCP_NODE LAY, 1)
              s.setsockopt(so cket.SOL_SOCKET , socket.SO_KEEPA LIVE, 1)
              s.setsockopt(so cket.SOL_SOCKET , socket.SO_RCVBU F, 64*1024)
              s.setsockopt(so cket.SOL_SOCKET , socket.SO_SNDBU F, 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.

              Comment

              • nephish@xit.net

                #8
                Re: freakin out over C++ module in python

                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.

                Comment

                • Serge Orlov

                  #9
                  Re: freakin out over C++ module in python


                  nephish@xit.net wrote:[color=blue]
                  > 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?[/color]

                  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.

                  Comment

                  • nephish@xit.net

                    #10
                    Re: freakin out over C++ module in python

                    sounds cool, most stuff i write is strife with try - except

                    thanks for the tip

                    -sk

                    Comment

                    Working...