通过主机/端口阻止套接字创建。
项目描述
用法
预期用途是确保单元测试不会建立网络连接。使用如下谓词激活 sockfilter:
# only allow http connections to localhost
def socket_address_allowed(address):
return (address.host in ['localhost', '127.0.0.1'] and
address.port == 80)
sockfilter.enable(socket_address_allowed)
如果后续尝试在地址不满足谓词的情况下打开连接,将引发 sockfilter.SockFilterError。
with_sockfiltering 装饰器
from sockfilter import with_sockfiltering
@with_sockfiltering(socket_address_allowed)
def test_foo():
foo()
sockfiltering 上下文管理器
from sockfilter import sockfiltering
def test_foo():
with sockfiltering(socket_address_allowed):
foo()
启用 和 禁用
import sockfilter
from unittest import TestCase
class FooTest(TestCase):
def setUp(self):
sockfilter.enable(socket_address_allowed)
def tearDown(self):
sockfilter.disable()
def test_foo(self):
foo()
内部原理
Sockfilter通过猴子补丁Python的 socket 库工作,灵感来自 HTTPretty。
项目详情
关闭
sockfilter-1.4.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 0aef68d7d76a5842d32174a8a20c5e480f91ed015d535a57d2192b12e152b05a |
|
MD5 | 414cfc42f8779c4ca880e64b97634967 |
|
BLAKE2b-256 | 0f99e6a1f75d6d6069c96cbedffb23b11f673492c3b4daebf2367aa52bc79b5e |