from hypertor import AsyncClient, TimeoutError, HypertorError
import asyncio
async def main():
print("🧅 hypertor - Basic Client Example")
print("=" * 40)
async with AsyncClient(timeout=60) as client:
print("\n✅ Connected to Tor network\n")
print("📡 Example 1: Check if we're using Tor")
try:
resp = await client.get("https://check.torproject.org/api/ip")
data = resp.json()
print(f" Your Tor IP: {data.get('IP', 'unknown')}")
print(f" Using Tor: {data.get('IsTor', False)}")
except TimeoutError:
print(" ⏱️ Request timed out (Tor network can be slow)")
except HypertorError as e:
print(f" ⚠️ Error: {e}")
print("\n📡 Example 2: Access DuckDuckGo onion")
try:
resp = await client.get(
"https://duckduckgogg42xjoc72x3sjasowoarfbgcmvfimaftt6twagswzczad.onion",
)
print(f" Status: {resp.status_code}")
print(f" Content-Length: {len(resp.text())} bytes")
except TimeoutError:
print(" ⏱️ Request timed out (onion services can be slow)")
except HypertorError as e:
print(f" ⚠️ Error: {e}")
print("\n📡 Example 3: POST JSON data")
try:
resp = await client.post(
"https://httpbin.org/post",
json='{"message": "Hello from Tor!", "anonymous": true}'
)
print(f" Status: {resp.status_code}")
print(f" Echo: {resp.json().get('json', {})}")
except TimeoutError:
print(" ⏱️ Request timed out")
except HypertorError as e:
print(f" ⚠️ Error: {e}")
print("\n✅ Example completed!")
print("\n💡 Tip: Tor requests are slow. Timeouts are normal in dev environments.")
if __name__ == "__main__":
asyncio.run(main())