Python3: asyncio + SOCKS5 Client support

Sockets, TCP/IP, (XML-)RPC und ähnliche Themen gehören in dieses Forum
Antworten
flugplatz89
User
Beiträge: 1
Registriert: Samstag 31. Mai 2014, 15:14

Hallo zusammen,

ich würde gerne das asyncio Modul mit einem Socksproxy auf einem lokalen Port verwenden. Nach ein wenig Recherche habe ich die PySocks gefunden, die nach ein paar Korrekturen auch mir Python3 läuft. Ich kann eine Anfrage (synchron) durch den Proxy schicken und erhalte eine Antwort.

Nach viel Recherche habe ich rausgefunden, wo in asyncio die Verbindung zu einem Socket aufgebaut wird und habe dort, probehalber, meinen Socks5 Socket reingepfriemelt:

Der ursprüngliche Code:

Code: Alles auswählen

base_events.py, 396-399:
                try:
                    sock = socket.socket(family=family, type=type, proto=proto)

                    sock.setblocking(False)
Mein modifizierter Code:

Code: Alles auswählen

base_events.py:
                try:
                    #sock = socket.socket(family=family, type=type, proto=proto)
                    from . import socks
                    sock = socks.socksocket()
                    sock.setproxy(socks.PROXY_TYPE_SOCKS5, "localhost", 9050)

                    sock.setblocking(False)
Dabei erhalte ich folgende Fehlermeldung:

Code: Alles auswählen

Future/Task exception was never retrieved
future: Task(<create_connection>)<exception=ProxyConnectionError()>
Traceback (most recent call last):
  File "/home/flugplatz89/python/aiohttp/asyncio/socks.py", line 479, in connect
    _orig_socket.connect(self, (proxy_addr, proxy_port))
BlockingIOError: [Errno 115] Operation now in progress

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/flugplatz89/python/aiohttp/asyncio/tasks.py", line 300, in _step
    result = coro.send(value)
  File "/home/flugplatz89/python/aiohttp/asyncio/base_events.py", line 424, in create_connection
    raise exceptions[0]
  File "/home/flugplatz89/python/aiohttp/asyncio/base_events.py", line 415, in create_connection
    yield from self.sock_connect(sock, address)
  File "/home/flugplatz89/python/aiohttp/asyncio/futures.py", line 350, in __iter__
    return self.result()  # May raise too.
  File "/home/flugplatz89/python/aiohttp/asyncio/futures.py", line 243, in result
    raise self._exception
  File "/home/flugplatz89/python/aiohttp/asyncio/selector_events.py", line 297, in _sock_connect
    sock.connect(address)
  File "/home/flugplatz89/python/aiohttp/asyncio/socks.py", line 489, in connect
    raise ProxyConnectionError(msg, error)
asyncio.socks.ProxyConnectionError: Error connecting to SOCKS5 proxy localhost:9050: [Errno 115] Operation now in progress
Der Socket wird also unterbrochen beim Aufbau der Verbindung, aber warum passiert das? Ein gewöhnlicher Verbindungsaufbau verläuft doch vernünftig. Was kann ich machen um asyncio Socks5 Unterstützung beizubringen? Gibt es vielleicht einen besseren/eleganteren Weg?

Vielen Dank für eure Hilfe.
Benutzeravatar
Balmung
User
Beiträge: 44
Registriert: Sonntag 17. März 2013, 18:36

Üblicherweise würde man sich ein Protokoll oder Transport für Proxys erstellen. Auf gar keinen Fall solttest du am asyncio source code rumfummeln.

Für eine allgemeine Transparente Verbindung zu einem Proxy Server empfiehlt sich womöglich das Entwickeln eines eigenen Transports? (ich bin mir allerdings nicht so sicher, ob das der richtige Fall ist)

Edit: und es schaut aus, dass ich falsch liege. Ein eigener Transport wird wohl eher selten von einem User der library selbst implementiert.

https://docs.python.org/3/library/asyncio-protocol.html
»Honk Honk«
Antworten