Skip to content

AttributeError: module 'ssl' has no attribute 'wrap_socket' #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
davidshumway opened this issue Jul 13, 2024 · 2 comments · May be fixed by #54
Open

AttributeError: module 'ssl' has no attribute 'wrap_socket' #53

davidshumway opened this issue Jul 13, 2024 · 2 comments · May be fixed by #54

Comments

@davidshumway
Copy link

davidshumway commented Jul 13, 2024

> pip3 install torpy[requests]
> python3 --version
Python 3.12.4
> uname -a
Linux fedora 6.9.7-200.fc40.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Jun 27 18:11:45 UTC 2024 x86_64 GNU/Linux

test.py

from torpy.http.requests import TorRequests
with TorRequests() as tor_requests:
    with tor_requests.get_session() as sess:
        r = sess.get('https://example.com').text
> python3 test.py
ERROR:root:[ignored]
Traceback (most recent call last):
  File "/home/david/.local/lib/python3.12/site-packages/torpy/utils.py", line 79, in newfn
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/david/.local/lib/python3.12/site-packages/torpy/consesus.py", line 235, in renew
    raw_string = self.download_consensus(prev_hash)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/david/.local/lib/python3.12/site-packages/torpy/consesus.py", line 183, in newfn
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/david/.local/lib/python3.12/site-packages/torpy/consesus.py", line 390, in download_consensus
    with self._get_dir_client() as dir_client:
         ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/david/.local/lib/python3.12/site-packages/torpy/consesus.py", line 375, in _get_dir_client
    self._dir_guard, self._dir_circuit = self._create_dir_circuit(purpose='Internal dir client')
                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/david/.local/lib/python3.12/site-packages/torpy/consesus.py", line 365, in _create_dir_circuit
    guard = TorGuard(router, purpose=purpose)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/david/.local/lib/python3.12/site-packages/torpy/guard.py", line 66, in __init__
    self.__tor_socket.connect()
  File "/home/david/.local/lib/python3.12/site-packages/torpy/cell_socket.py", line 57, in connect
    self._socket = ssl.wrap_socket(
                   ^^^^^^^^^^^^^^^
AttributeError: module 'ssl' has no attribute 'wrap_socket'
WARNING:torpy.utils:Retry with another router...
@SrSerranoo
Copy link

i have the same problem, with python3.12 i recommend you change a "stem" library.

@droe droe linked a pull request Nov 19, 2024 that will close this issue
@Mr-Frst
Copy link

Mr-Frst commented Dec 30, 2024

To resolve the SSL-related and compatibility issues in the torpy code, you need to edit two files:

  1. File: "/lib/python3.12/site-packages/torpy/cell_socket.py" line 57
  • Issue: [SSL: UNEXPECTED_EOF_WHILE_READING] due to improper SSLContext usage.

  • Edit: Update the connect method to remove the ssl_version argument and configure the SSL context correctly.

  • How to Edit:
    Locate the following block in the connect method:

self._socket = ssl.wrap_socket(
    socket.socket(socket.AF_INET, socket.SOCK_STREAM), ssl_version=ssl.PROTOCOL_TLSv1_2
)
  • Replace it with:
secure_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
self._socket = secure_context.wrap_socket(
    socket.socket(socket.AF_INET, socket.SOCK_STREAM)
)
  1. File: "/lib/python3.12/site-packages/torpy/http/adapter.py" line 88
  • Issue: AttributeError: 'MyHTTPConnectionPool' object has no attribute 'strict'.

  • Edit: Remove the strict argument from the _new_conn method.

  • How to Edit:
    Locate this part of the _new_conn method:

strict=self.strict,
  • Remove the line entirely: The final block should look like this:
return MyHTTPConnection(
    circuit,
    host=self.host,
    port=self.port,
    timeout=self.timeout.connect_timeout,
    **self.conn_kw,
)

After making these changes, save the files and rerun your script. These edits address the SSL handshake issue and update the code for compatibility with newer versions of Python and urllib3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants