-
Notifications
You must be signed in to change notification settings - Fork 25
Threaded Server
Nicolas Couture edited this page Apr 19, 2026
·
2 revisions
MockSSH was designed with automated testing in mind. Its primary strength is the ability to run a mock SSH server in a separate thread within your test suite, allowing for true end-to-end verification of your automation logic.
Unlike runServer, which blocks the main thread, startThreadedServer launches the Twisted reactor in a background thread.
import MockSSH
# Start the server with port 0 to use any available port
server = MockSSH.startThreadedServer(
commands,
prompt="hostname>",
interface="127.0.0.1",
port=0,
**users
)
# Retrieve the assigned port
port = server.getHost().port
# ... run your tests connecting to 'port' ...
# Stop the server
MockSSH.stopThreadedServer(server)Twisted's reactor has a critical limitation: it cannot be restarted.
To handle this in a test suite, MockSSH recommends:
- Starting the reactor once at the beginning of the test session.
- Using
server_port.stopListening()(wrapped instopThreadedServer) to close specific ports between tests without stopping the global reactor.