我需要一些幫助。我正在使用運行在Windows7上的Python 2.5.4的舊版軟件,我需要在套接字連接中啓用Keepalive。如何在Windows 7中運行python 2.5中設置保活計時器
我在下面的線程看到您可以使用
object.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
啓用蟒蛇保活不過這個安裝程序將使用默認的Windows保持2小時連接定時器。
我也看到,我們可以使用以下API設置計時器,但它僅適用於Python 2.6以上版本。
sock.ioctl(socket.SIO_KEEPALIVE_VALS, (1, 10000, 3000))
是否有無論如何我可以使用python 2.5.4設置此SIO_KEEPALIVE_VALS?遺留代碼我也有模塊pywin32-214。我真的不能升級python版本。
我還納悶怎麼python2.6的和新的調用Windows API
int WSAIoctl(
(socket) s, // descriptor identifying a socket
SIO_KEEPALIVE_VALS, // dwIoControlCode
(LPVOID) lpvInBuffer, // pointer to tcp_keepalive struct
(DWORD) cbInBuffer, // length of input buffer
NULL, // output buffer
0, // size of output buffer
(LPDWORD) lpcbBytesReturned, // number of bytes returned
(LPWSAOVERLAPPED) lpOverlapped, // OVERLAPPED structure
(LPWSAOVERLAPPED_COMPLETION_ROUTINE) lpCompletionRoutine, // completion routine
);
感謝您的幫助。
參考文獻: How to change tcp keepalive timer using python script?
https://msdn.microsoft.com/en-us/library/dd877220%28v=vs.85%29.aspx
你可以返回[sock_ioctl](https://hg.python.org/cpython/file/2.6/Modules/socketmodule.c#l2848)函數從Python 2.6或使用Ctypes調用[WSAIoctl](https://github.com/twisted/twisted/blob/e38cc25a67747899c6984d6ebaa8d3d134799415/src/twisted/internet/test/_win32ifaces.py#L26)函數。 – cgohlke
嗨@cgohlke,謝謝你的建議。但是,我還沒有真正嘗試過將C/C++與Python混合。我會開始研究它。如果您有任何關於良好參考的建議,將不勝感激。非常感謝。 – paulp