将C ++类包装到python

时间:2016-06-17 15:46:50

标签: python c++ c sockets ctypes

对于我的项目(对于游戏来说),我需要一个C网络接口。我想用Python编写我的项目代码,所以我用C TCP-Socket函数创建了一个C ++包装器,在我的Python代码中用它c-types

#include <iostream>
#include <string>
#include "Socket.h"
#include "stdio.h"

void        Socket::s_connect(char *host, int port){
    const char *str;
    pe = getprotobyname("tcp");
    if (!pe){
        perror("Error get protocol");
        exit(1);
    }
    f = socket(AF_INET, SOCK_STREAM, pe->p_proto);
    if (f == -1){
        perror("Error create socker");
        exit(1);
    }
    s_in.sin_family      = AF_INET;
    s_in.sin_port        = htons(port);
    s_in.sin_addr.s_addr = inet_addr(host);
    if (connect(f, (const struct sockaddr *&)s_in, sizeof(s_in)) == -1){
        perror("Error on connect");
        close(f);
    }
}


void        Socket::s_send(char *msg){
    if (write(f, msg, strlen(msg)) == -1){
        printf("error write\n");
        perror("write");
        exit(1);
    }
}

char        *Socket::s_recv(){
    char    *buff;
    int     ret;

    buff = (char *)malloc(4096);
    ret = read(f, buff, 4096);
    if (ret < 0){
        if (close(f))
            exit(1);
    }
    return buff;
}
extern "C" {
    Socket  *Socket_new()
    {
        return (new Socket); 
    }

    void    Socket_connect(Socket *sock, char *host, int port)
    {
        sock->s_connect(host, port);
    }

    void    Socket_send(Socket *sock, char *msg)
    {
        sock->s_send(msg);
    }
}

Socket.h:

#ifndef SOCKET_HPP_
# define SOCKET_HPP_


#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>

class Socket {
    int                 f, port;
    struct protoent     *pe;
    struct sockaddr_in  s_in;
    char                *ip;
public:
    void s_connect(char *host, int port);
    void s_send(char * msg);
    char *s_recv();
};

#endif

还有我的Python课程:

#!/usr/bin/python

import sys
from ctypes import cdll

lib = cdll.LoadLibrary('./libsocket.so')

class Socket(object):
    def __init__(self):
        self.obj = lib.Socket_new()


    def s_connect(self, host, port):
        print(host, port)
        lib.Socket_connect(self.obj, host, int(port))


    def s_send(self, msg):
        lib.Socket_send(self.obj, msg)

    def s_recv(self):
        lib.Socket_recv(self.obj)

    def main(arg):
        sock = Socket()
        sock.s_connect(arg[1], arg[2])
        sock.s_send("coucou")

    if __name__ == "__main__":
        main(sys.argv)

我的extern C func无法读取从Python发送的字符串我可以发送端口号,但我的C ++函数无法读取主机的字符串值。 如果我将char *更改为std::string,我会遇到同样的问题。 我做错了吗?

由于

1 个答案:

答案 0 :(得分:1)

正如您所说的使用Python 3版本,str模块的文档指出Python unicode字符串(wchar_t *)对应于C(bytes)中的宽字符数组,并且Python字节字符串(char *)对应于窄字符数组(bytes)。

因此,如果您想要实际处理unicode字符,您应该:

  • 使用sock.s_connect(arg[1].encode('latin1'), arg[2]) Python端(平凡):

    wchar_t
  • 或使用button { color: red; font-family: sans-serif; } C方面(可能更难......)