Linux Mint, python 3.12
NB: torify works well with curl.
So I activate venv, then run Tor, then run the snippet.
The code:
import aiohttp
import asyncio
async def main():
async with aiohttp.ClientSession(trust_env=True) as session:
async with session.get(
'http://python.org'
) as response:
print("Status:", response.status)
print("Content-type:", response.headers['content-type'])
html = await response.text()
print("Body:", html[:15], "...")
asyncio.run(main())
$ python3 test.py gives out:
Status: 200
Content-type: text/html; charset=utf-8
Body: ...
But $ torify python3 test.py gives:
(.venv) oleg@oleg:~/Документы/pyscripts/$ torify python3 test.py
Traceback (most recent call last):
File "/home/oleg/Документы/pyscripts/.venv/lib/python3.12/site-packages/aiohttp/resolver.py", line 103, in resolve
resp = await self._resolver.getaddrinfo(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
aiodns.error.DNSError: (11, 'Could not contact DNS servers')
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/oleg/Документы/pyscripts/.venv/lib/python3.12/site-packages/aiohttp/connector.py", line 1362, in _create_direct_connection
hosts = await self._resolve_host(host, port, traces=traces)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/oleg/Документы/pyscripts/.venv/lib/python3.12/site-packages/aiohttp/connector.py", line 995, in _resolve_host
return await asyncio.shield(resolved_host_task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/oleg/Документы/pyscripts/.venv/lib/python3.12/site-packages/aiohttp/connector.py", line 1026, in _resolve_host_with_throttle
addrs = await self._resolver.resolve(host, port, family=self._family)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/oleg/Документы/pyscripts/.venv/lib/python3.12/site-packages/aiohttp/resolver.py", line 112, in resolve
raise OSError(None, msg) from exc
OSError: [Errno None] Could not contact DNS servers
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/oleg/Документы/pyscripts/test.py", line 17, in
asyncio.run(main())
File "/usr/lib/python3.12/asyncio/runners.py", line 194, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/asyncio/runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "/home/oleg/Документы/pyscripts/test.py", line 7, in main
async with session.get(
File "/home/oleg/Документы/pyscripts/.venv/lib/python3.12/site-packages/aiohttp/client.py", line 1425, in __aenter__
self._resp: _RetType = await self._coro
^^^^^^^^^^^^^^^^
File "/home/oleg/Документы/pyscripts/.venv/lib/python3.12/site-packages/aiohttp/client.py", line 703, in _request
conn = await self._connector.connect(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/oleg/Документы/pyscripts/.venv/lib/python3.12/site-packages/aiohttp/connector.py", line 548, in connect
proto = await self._create_connection(req, traces, timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/oleg/Документы/pyscripts/.venv/lib/python3.12/site-packages/aiohttp/connector.py", line 1056, in _create_connection
_, proto = await self._create_direct_connection(req, traces, timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/oleg/Документы/pyscripts/.venv/lib/python3.12/site-packages/aiohttp/connector.py", line 1368, in _create_direct_connection
raise ClientConnectorDNSError(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorDNSError: Cannot connect to host python.org:80 ssl:default [Could not contact DNS servers]
I tried adding
ssl=False
to
async with session.get(ssl=False)
Tor version 0.4.8.10.
This build of Tor is covered by the GNU General Public License (https://www.gnu.org/licenses/gpl-3.0.en.html)
Tor is running on Linux with Libevent 2.1.12-stable, OpenSSL 3.0.13, Zlib 1.3, Liblzma 5.4.5, Libzstd 1.5.5 and Glibc 2.39 as libc.
Tor compiled with GCC version 13.2.0
(.venv) oleg@oleg:~/Документы/pyscripts$ service tor status
● tor.service - Anonymizing overlay network for TCP (multi-instanc>
Loaded: loaded (/usr/lib/systemd/system/tor.service; enabled;>
Active: active (exited) since Mon 2025-03-17 18:57:14 +08; 2h>
Main PID: 755 (code=exited, status=0/SUCCESS)
CPU: 1ms
$ curl https://api.ipify.org?format=json
"ip":"95.26.192.116"
$ torify curl https://api.ipify.org?format=jsonorg?format=json
"ip":"192.42.116.209"}
torify works