如何在 Windows 7 中运行的 python 2.5 中设置保持活动计时器 [英] How to set keep-alive timer in python 2.5 running in Windows 7
问题描述
我需要一些帮助.我正在开发使用在 Windows7 上运行的 python 2.5.4 的旧软件,我需要在我的套接字连接中启用 keepalive.
I need some help. I'm working on a legacy software that uses python 2.5.4 running on Windows7 and I need to enable keepalives in my socket connection.
我在下面的线程中看到您可以使用
I've seen in the thread below that you can enable keepalives in python using
object.setsockopt( socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
但是,此设置使用默认的 2 小时 Windows 保持活动计时器.
However this setup uses the default windows keep alive timer of 2 hours.
我还看到我们可以使用以下 API 设置计时器,但它仅适用于 Python 2.6 及更高版本.
I've also seen that we can set the timer using the following API, however it is only available for Python 2.6 onwards.
sock.ioctl(socket.SIO_KEEPALIVE_VALS, (1, 10000, 3000))
无论如何我可以使用 python 2.5.4 设置这个 SIO_KEEPALIVE_VALS 吗?我的遗留代码也有模块 pywin32-214.我真的无法升级python版本.
Is there anyway I can set this SIO_KEEPALIVE_VALS using python 2.5.4 ? The legacy code I have also has the module pywin32-214. I really can't upgrade the python version.
我也想知道python2.6及更新版本如何调用windows api
I also wonder how python2.6 and newer calls the 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
);
感谢您的帮助.
参考资料:如何使用python脚本更改tcp keepalive定时器?
https://msdn.microsoft.com/en-us/library/dd877220%28v=vs.85%29.aspx
推荐答案
这是你在 c 中的做法
Here is how you do it in c
static PyObject*
sock_ioctl(PyObject *argO , PyObject *arg)
{
PyObject *s;
DWORD recv;
struct tcp_keepalive ka;
if (!PyArg_ParseTuple(arg, "O(kkk):keepalive",&s,
&ka.onoff, &ka.keepalivetime, &ka.keepaliveinterval))
return NULL;
if (WSAIoctl(PyObject_AsFileDescriptor(s), SIO_KEEPALIVE_VALS, &ka, sizeof(ka),
NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
return set_error();
}
return PyLong_FromUnsignedLong(recv);
}
我在github上做了一个小的python扩展https://github.com/rawinput/ioctl为python 2.5编译
I made a small python extension in github https://github.com/rawinput/ioctl compiled for python 2.5
这篇关于如何在 Windows 7 中运行的 python 2.5 中设置保持活动计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!