Install & Compatibility
Where this runs
tested against v26.6.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 1.084s · 72.7MB
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 5.4s · import 1.037s · 73MB
75MB installed
● package 75MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
txtorcon
✓ import txtorcon
Primary library import
connect
✓ from txtorcon import connect
✗ from txtorcon.controller import connect
Directly available from the top-level package in recent versions for high-level API
launch
✓ from txtorcon import launch
✗ from txtorcon.torconfig import launch_tor
Use the high-level 'launch' from the top-level package; 'launch_tor' is an older/deprecated pattern.
UNIXClientEndpoint
✓ from twisted.internet.endpoints import UNIXClientEndpoint
Commonly used for connecting to Tor control port via Unix socket.
react
✓ from twisted.internet.task import react
Standard Twisted entry point for running event loops.
inlineCallbacks
✓ from twisted.internet.defer import inlineCallbacks
Decorator for Twisted's coroutine-like Deferred chains.
This quickstart demonstrates connecting to an existing Tor instance (either via a Unix socket or TCP), performing a web request through Tor, and building a custom circuit. It uses `txtorcon.connect` for connection and `tor.web_agent()` for HTTP requests with `treq`. It's crucial to handle reactor management (e.g., with `@react`).
import os
from twisted.internet.task import react
from twisted.internet.defer import inlineCallbacks, ensureDeferred
from twisted.internet.endpoints import UNIXClientEndpoint, TCP4ClientEndpoint
import treq
import txtorcon
@react
@inlineCallbacks
def main(reactor):
# Connect to a running Tor instance, e.g., Tor Browser Bundle's control port (default 9151)
# Or a system-wide Tor daemon (default 9051, or /var/run/tor/control)
# You can also use txtorcon.launch(reactor) to start a new Tor process managed by txtorcon.
# Example: Connect to a Unix socket (common for system Tor)
control_endpoint = UNIXClientEndpoint(reactor, '/var/run/tor/control')
# Example: Connect to a TCP port (common for TBB or custom Tor setup)
# control_endpoint = TCP4ClientEndpoint(reactor, 'localhost', 9151)
# Password for Tor control port, if required. Use environment variable for security.
# For a newly launched Tor, this is usually not needed immediately.
password = os.environ.get('TOR_CONTROL_PASSWORD', '')
try:
tor = yield txtorcon.connect(
reactor,
control_endpoint,
password_function=lambda: password
)
print(f"Connected to Tor version {tor.version}")
url = u'https://www.torproject.org:443'
print(f"Downloading {repr(url)} via Tor...")
# Use tor.web_agent() to make requests over Tor's general circuit
resp = yield treq.get(url, agent=tor.web_agent())
body_data = yield resp.text()
print(f"Got {len(body_data)} bytes from {url}:")
print(body_data[:200] + ('...' if len(body_data) > 200 else '')) # Print first 200 chars
print("\nCreating a new Tor circuit...")
state = yield tor.create_state()
circ = yield state.build_circuit()
yield circ.when_built()
print(f"New circuit built with path: {' -> '.join([r.ip for r in circ.path])}")
except Exception as e:
print(f"An error occurred: {e}")
# Handle specific connection errors or Tor failures
finally:
# It's good practice to disconnect or shut down Tor if launched by txtorcon
if hasattr(tor, 'shutdown') and callable(tor.shutdown):
print("Shutting down Tor (if launched by txtorcon)...")
yield ensureDeferred(tor.shutdown())
else:
print("Not shutting down Tor, it was an external instance.")
txtorcon --version
Errors
Common errors & fixes
Tor unexpectedly disconnected while running: GETINFO md/id/...
In versions prior to 23.11.0, receiving overly long `GETINFO` responses (e.g., for certain relay descriptors) could cause Twisted's line receiver to exceed its buffer, leading to an unexpected disconnect or hang.
fixUpgrade `txtorcon` to version 23.11.0 or newer. This release addresses the handling of long lines in the control protocol.
AttributeError: 'TorControlProtocol' object has no attribute 'on_disconnect'
You are attempting to use the `on_disconnect` callback on a `TorControlProtocol` instance, which was deprecated in `txtorcon` v19.1.0.
fixReplace usage of `protocol.on_disconnect` with `protocol.when_disconnected`. The `when_disconnected` method returns a Deferred that fires when the protocol disconnects.
SyntaxError: invalid syntax (on Python 2.x) or other Python 3 specific errors
`txtorcon` dropped support for Python 2 entirely in version 23.0.0, and newer features are Python 3-only.
fixEnsure your project is running on Python 3.8 or newer. Update your environment and codebase to be fully Python 3 compliant.
Upgrade
Version history
26.6.0latest on PyPI · released Jun 1, 2026
Audit
Dependencies
TwistedrequiredCore event-driven networking framework that txtorcon is built upon.
automatrequiredUsed for concise, idiomatic Python expression of finite-state automata, a dependency for recent versions.
treqoptionalCommonly used 'requests'-like library for Twisted for making web requests over Tor, as shown in examples.