Allow more fine grained access: include host/port combinations
enhancementstale
It would be useful if pytest-socket supported allowing `connect()` to specific host/port combinations, instead of a blanket guard on all socket creation. The reason for this is when using testcontainers that spawn docker images, those containers are typically only accessible via localhost socket communication. With more fine-grained abilities I could do something like
```
container = RedisContainer()
pytest_socket.enable_connect(("localhost", container.port))
```
This would imply that socket creation could be allowed, but other socket operations such as `connect()` and `bind()` could be disallowed.
For now I am working around this by enabling sockets before my code creates a Redis connection, and then disable sockets immediately after creating the connection.
Note that some modules are not impacted by a socket guard because they are implemented in C (psycopg driver for postgres for example).
关闭于 2025-10-05 5 条评论