2

I need Robobrowser to use Tor, easiest way i thought would be like this: Robobrowser with Sessions. But i encountered a strange problem with requests, or more specific urllib3:

AttributeError: module 'socks' has no attribute 'create_connection'

As documented in Urllib3 Documentation create_connection is an attribute from socks.

Urllib3 version: 1.19.1

Requests version: 2.12.4

PySocks version: 1.6.5

import requests

session = requests.Session()
session.proxies = {'http':'socks5://127.0.0.1:9050'}
response = session.get('http://www.icanhazip.com', timeout=2)
print(response.text)

edit:

Stacktrace:

    Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/requests/sessions.py", line 501, in get
    return self.request('GET', url, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/requests/sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.5/dist-packages/requests/sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/requests/adapters.py", line 423, in send
    timeout=timeout
  File "/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/connectionpool.py", line 594, in urlopen
    chunked=chunked)
  File "/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/connectionpool.py", line 361, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib/python3.5/http/client.py", line 1106, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python3.5/http/client.py", line 1151, in _send_request
    self.endheaders(body)
  File "/usr/lib/python3.5/http/client.py", line 1102, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python3.5/http/client.py", line 934, in _send_output
    self.send(msg)
  File "/usr/lib/python3.5/http/client.py", line 877, in send
    self.connect()
  File "/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/connection.py", line 163, in connect
    conn = self._new_conn()
  File "/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/contrib/socks.py", line 79, in _new_conn
    conn = socks.create_connection(
AttributeError: module 'socks' has no attribute 'create_connection'
Community
  • 1
  • 1
Gugi
  • 23
  • 7

1 Answers1

0

It is because your python instance can't find the required socks libraries.

The libraries in /usr/lib/python* are installed via sudo apt-get install python3-requests etc.

However apt-get can't install the required pysocks libraries that the urllib3 uses. sudo apt-get install python3-socks won't work.

What will work is if you install it via sudo -H pip3 install pysocks urllib3[socks] requests[socks]

This will install it under here sudo ls -lrtah /usr/local/lib/python*/dist-packages/

Make sure you terminal session has /usr/lib:/usr/local/lib in the system path. If your python instance still can't find your socks library, then make sure both paths are part of your ~/.bashrc PYTHON_PATH export.