快速检测或模拟WSAECONNREFUSED

sch*_*mar 11 python sockets windows winsock

对于WSAECONNREFUSED(这意味着积压已满或端口不可用),Windows套接字有一些奇怪的行为,请参阅/sf/answers/721583691/.如果Windows检测到这些条件之一,则重试(最多)两次,间隔为0.5秒.这意味着在套接字连接尝试(http://support.microsoft.com/kb/175523/en-us)上检测WSAECONNREFUSED至少需要1秒钟.

有没有办法加速这种检测而不会弄乱注册表值?我需要在单元测试中模拟拒绝套接字连接.像模拟与原始套接字的拒绝连接一样的解决方法也是可以接受的.

这是一个简单的Python脚本,演示了这个问题:

import errno
import socket
import time

PORT = 50123


def main():
    s = socket.socket()
    s.bind(('127.0.0.1', PORT))
    s.listen(0)
    client = socket.socket()
    client.connect(('127.0.0.1', PORT))

    client2 = socket.socket()
    start = time.time()

    try:
        client2.connect(('127.0.0.1', PORT))
    except socket.error as e:
        assert e.errno == errno.WSAECONNREFUSED
        print 'connection attempt took', time.time() - start
    finally:
        client2.close()
        client.close()
        s.close()


if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

Dmi*_*hev 3

这不完全是你问的问题。但如果您仅在单元​​测试中需要这个,模拟库将会很有用。

import errno
import socket
import time
import mock

PORT = 50123


def connect_mock(*agrs):
    raise socket.error(errno.WSAECONNREFUSED, "Testing")


def main():
    s = socket.socket()
    s.bind(('127.0.0.1', PORT))
    s.listen(0)
    client = socket.socket()
    client.connect(('127.0.0.1', PORT))

    client2 = socket.socket()
    start = time.time()

    with mock.patch('socket.socket.connect', connect_mock):
        try:
            client2.connect(('127.0.0.1', PORT))
            print "done"
        except socket.error as e:
            assert e.errno == errno.WSAECONNREFUSED
            print 'connection attempt took', time.time() - start
        finally:
            client2.close()
            client.close()
            s.close()


if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)