简体   繁体   English

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

[英]How can I pass a Python Socket object to a DLL written in C?

Is there any way of converting the python socket object to a void pointer? 有什么方法可以将python套接字对象转换为void指针吗?

I am using ctypes and I have tried using cast and the conversion functions of python. 我正在使用ctypes,并且尝试使用cast和python的转换函数。

The C code that I am trying to convert in python is this: 我试图在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;
}

The Equivalent Python Code is -> 等效的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;  

This is a call back function and I have used: 这是一个回调函数,我使用了:

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

to create the call back. 创建回叫。 UNILIB_CALL is __stdcall and I have used WinDLL to get library handle. UNILIB_CALL__stdcall ,我已经使用WinDLL来获取库句柄。

When the callback occurs I get an error: 当回调发生时,我得到一个错误:

Socket Pointer must have been allocated. 套接字指针必须已分配。

Is there any way of dereferencing pointer to a pointer? 有什么办法可以取消对指针的引用?

Can you suggest some changes in the code so that sockets can be handled as they have been in the C code? 您可以建议对代码进行一些更改,以便套接字可以像在C代码中一样进行处理吗?

Sockets have the fileno() function. 套接字具有fileno()函数。

Maybe you can use it and create a new socket out of this file descriptor. 也许您可以使用它并根据此文件描述符创建一个新的套接字。

Converting the Python object to a void pointer will not help. 将Python对象转换为void指针将无济于事。 The C function requires a pointer to a C socket object, and the Python socket object is not the same as a C socket object. C函数需要一个指向C套接字对象的指针,而Python套接字对象与C套接字对象不同。

As far as I know, you cannot use Python in this way to get a socket back for a C program to use. 据我所知,您不能以这种方式使用Python来获取供C程序使用的套接字。 Why are you trying to do this? 你为什么要这样做?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python将数组传递给用c编写的共享库(.dll) - How can I pass an array to shared library(.dll) written in c using python 如何将用 C 编写的 Win32 DLL 导入 Python? - How do I import a Win32 DLL written in C into Python? 如何使用Python脚本中的C#DLL? - How can I use a C# DLL from Python Script? 如何将(C ++)与重命名的Python .lib和.dll链接起来? - How can I link (C++) with renamed Python .lib and .dll? 如何正确将十六进制值传递给Python ctypes中的C dll? - How do I properly pass a hex value into a C dll in Python ctypes? 我如何将python对象作为参数传递给C#程序 - how do i pass python object as argument to C# program I'm trying to open a dll written in c with python ctypes and run the function in it, but it comes as a char, not a string - I'm trying to open a dll written in c with python ctypes and run the function in it, but it comes as a char, not a string 如何将从 C++ 客户端套接字接收的二进制数据转换为 python 服务器套接字 - How can I convert binary data received from C++ client socket to python Server socket 如何在我的python代码中使用经过修改的openssl库(用C编写)? - How can I use a modified openssl library (written in C) in my python code? PyPI:我如何测试和分发用free pascal编写的本机python扩展(没有任何C代码)? - PyPI: How can I test and distribute a native python extension written in free pascal (with no C code whatsoever)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM