按主机/端口阻止创建套接字。
sockfilter的Python项目详细描述
用法
其目的是确保您的单元测试不会使网络 连接。使用如下谓词激活sockfilter:
# only allow http connections to localhostdefsocket_address_allowed(address):return(address.hostin['localhost','127.0.0.1']andaddress.port==80)sockfilter.enable(socket_address_allowed)
在不满足要求的地址打开连接的任何后续尝试 谓词将引发sockfilter.SockFilterError。
with_sockfilteringdecorator
fromsockfilterimportwith_sockfiltering@with_sockfiltering(socket_address_allowed)deftest_foo():foo()
sockfiltering上下文管理器
fromsockfilterimportsockfilteringdeftest_foo():withsockfiltering(socket_address_allowed):foo()
enable和disable
importsockfilterfromunittestimportTestCaseclassFooTest(TestCase):defsetUp(self):sockfilter.enable(socket_address_allowed)deftearDown(self):sockfilter.disable()deftest_foo(self):foo()
发动机罩下
sockfilter通过猴子修补python的socket库来工作, 灵感来自HTTPretty。