如何将Python Socket对象传递给用C编写的DLL?

时间:2011-04-05 06:08:48

标签: python sockets

有没有办法将python套接字对象转换为void指针?

我正在使用ctypes,我尝试过使用cast和python的转换函数。

我试图在python中转换的C代码是:

long UNILIB_CALL UL_cbOpen ( void **psocket, char * ipAddress, 
    unsigned int port, int connectionTimeout, int sendTimeout, int recvTimeout)  
{  
    printf("%s  %d  ",ipAddress,port);  
    long ssendTimeout=sendTimeout *100;  
    long rrecvTimeout=recvTimeout *100;  
    printf("Address:%u,%u,%u", psocket,*psocket,&psocket);    


    (*psocket)  =  (void*)socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
    printf("Address after:%u,%u,%u", psocket,*psocket,&psocket);
    getchar();
    RETURN_IF_ERRROR ((SOCKET)(*psocket) == INVALID_SOCKET,"SOCKET", 0);

    long iResult=0;
    //char RecvTimeout[21]; sprintf_s (RecvTimeout, sizeof RecvTimeout, "%d", recvTimeout);
    iResult = setsockopt((SOCKET)(*psocket),SOL_SOCKET,SO_RCVTIMEO, (char*)&rrecvTimeout, 
        rrecvTimeout) ;
    RETURN_IF_ERRROR ( iResult == SOCKET_ERROR,"setsockopt",     WSAGetLastError()); 

    char SendTimeout[21]; 
    sprintf_s (SendTimeout, sizeof SendTimeout, "%d", sendTimeout);
    iResult = setsockopt((SOCKET)(*psocket),SOL_SOCKET,SO_SNDTIMEO, (char*)&ssendTimeout, 
        sizeof ssendTimeout) ;

    RETURN_IF_ERRROR ( iResult == SOCKET_ERROR,"setsockopt",     WSAGetLastError()); 
    SOCKADDR_IN clientService;   
    clientService.sin_family = AF_INET; 
    clientService.sin_addr.s_addr = inet_addr( ipAddress );
    clientService.sin_port = htons( port );

    iResult = connect( (SOCKET)(*psocket), (SOCKADDR*) &clientService, 
        sizeof(clientService) ); 

    RETURN_IF_ERRROR ( iResult == SOCKET_ERROR,"connect",    WSAGetLastError()); 

    return 1;
}

等效Python代码是 - >

def UL_cbOpen(psocket,ipAddress,port,connectionTimeout,sendTimeout,recvTimeout):  
    print "Open CallBack\n";  
    print "PORT: {0}".format(port);  
    #print "IPAddress:{0}".format(ipAddress);  
    #print "Port:{0}".format(port);  
    ssendTimeout=c_long();  
    rrecvTimeout=c_long();  
    ssendTimeout=sendTimeout*100;  
    rrecvTimeout=recvTimeout*100;  
    try:  
        psocket=socks.socksocket(AF_INET,SOCK_STREAM,IPPROTO_TCP);  
        print "Socket created";  
    except:  
        print "Failed to create socket";  
    psocket.setproxy(socks.PROXY_TYPE_SOCKS5,"my.web-proxy.example.com")  
    #PROXY_TYPE_SOCKS4,PROXY_TYPE_HTTP  
    hostname=gethostbyaddr(ipAddress);  
    psocket.setsockopt(SOL_SOCKET, SO_RCVTIMEO, rrecvTimeout);  
    psocket.setsockopt(SOL_SOCKET,SO_SNDTIMEO,ssendTimeout);  
    try:  
        psocket.connect((ipAddress,int(port)));  
        print "Connected to Printer";  
    except:   
        print "Failed Connection";  
    return 1;  

这是一个回调函数,我使用过:

ulopen=WINFUNCTYPE(c_long,POINTER(c_void_p),c_char_p,c_uint,c_int,c_int,c_int);  

创建回叫。 UNILIB_CALL__stdcall,我使用WinDLL来获取库句柄。

当回调发生时,我收到错误:

  

必须已分配套接字指针。

有没有办法解除指向指针的指针?

您能否建议对代码进行一些更改,以便可以像处理C代码那样处理套接字?

2 个答案:

答案 0 :(得分:0)

将Python对象转换为void指针无济于事。 C函数需要一个指向C套接字对象的指针,而Python套接字对象与C套接字对象不同。

据我所知,你不能以这种方式使用Python来获取套接字以供C程序使用。你为什么要这样做?

答案 1 :(得分:0)

套接字具有fileno()功能。

也许你可以使用它并从这个文件描述符中创建一个新的套接字。