gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 11:56:13 25 4
gpt4 key购买 nike

有什么方法可以将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来获取库句柄。

当回调发生时,我得到一个错误:

Socket Pointer must have been allocated.



有什么办法可以取消对指针的引用?

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

最佳答案

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

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

关于python - 如何将Python Socket对象传递给用C编写的DLL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5547895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com