{"library":"stem","title":"Stem - Tor Controller Library","type":"library","description":"Stem is a Python controller library that provides a high-level API for interacting with the Tor daemon. It allows applications to query Tor's status, manage circuits and streams, and retrieve information about Tor relays. The current stable version is 1.8.2, with releases occurring as needed for bug fixes and minor enhancements.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install stem"],"cli":{"name":"stem","version":"sh: 1: stem: not found"}},"imports":["from stem.control import Controller","from stem.process import launch_tor_with_config","from stem import SocketClosed"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":"https://stem.torproject.org/","github":null,"docs":null,"changelog":null,"pypi":"https://pypi.org/project/stem/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"import stem.control\nimport os\n\n# Default Tor control port is 9051. Authenticate with password or cookie.\ntry:\n    with stem.control.Controller.from_port(port=9051) as controller:\n        # Attempt authentication. If TOR_CONTROL_PASSWORD env var is set, use it.\n        # Otherwise, authenticate() without arguments tries cookie authentication.\n        auth_password = os.environ.get('TOR_CONTROL_PASSWORD', '')\n        if auth_password:\n            controller.authenticate(password=auth_password)\n        else:\n            controller.authenticate() # Attempts cookie authentication by default\n\n        print(\"Successfully connected to Tor controller!\")\n        print(f\"Tor version: {controller.get_version()}\")\n        \n        # Example: Get all active circuits\n        circuits = controller.get_circuits()\n        print(f\"Number of active Tor circuits: {len(circuits)}\")\n\nexcept stem.SocketClosed:\n    print(\"Error: Could not connect to Tor's control port. Is Tor running?\")\n    print(\"Check your Tor configuration (torrc) for ControlPort and authentication settings.\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")\n","lang":"python","description":"This example connects to a running Tor daemon's control port (typically 9051) and authenticates using either an environment variable for a password or via Tor's default cookie-based authentication. It then prints the Tor version and the number of active circuits.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}