AttributeError: module 'OpenSSL.SSL' has no attribute 'SSLv3_METHOD'
Asked Answered
I

6

17

After running the scrapy shell with the defined url, I am getting the attribute error showing the following error: AttributeError: module 'OpenSSL.SSL' has no attribute 'SSLv3_METHOD'

scrapy shell "https://quotes.toscrape.com/tag/humor/"

Can anyone please help me solving the error?

2022-09-27 01:38:38 [scrapy.utils.log] INFO: Versions: lxml 4.9.1.0, libxml2 2.9.12, cssselect 1.1.0, parsel 1.6.0, w3lib 2.0.1, Twisted 22.8.0, Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)], pyOpenSSL 22.1.0 (OpenSSL 3.0.5 5 Jul 2022), cryptography 38.0.1, Platform Windows-10-10.0.19041-SP0
2022-09-27 01:38:38 [scrapy.crawler] INFO: Overridden settings:
{'DUPEFILTER_CLASS': 'scrapy.dupefilters.BaseDupeFilter',
 'LOGSTATS_INTERVAL': 0}
2022-09-27 01:38:38 [scrapy.utils.log] DEBUG: Using reactor: twisted.internet.selectreactor.SelectReactor
2022-09-27 01:38:38 [scrapy.extensions.telnet] INFO: Telnet Password: d760ab2d8573ec57
2022-09-27 01:38:38 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
 'scrapy.extensions.telnet.TelnetConsole']
2022-09-27 01:38:39 [scrapy.core.downloader.handlers] ERROR: Loading "scrapy.core.downloader.handlers.http.HTTPDownloadHandler" for scheme "http"
Traceback (most recent call last):
  File "d:\python\scraping-project\scrapy\venv\lib\site-packages\scrapy\core\downloader\handlers\__init__.py", line 49, in _load_handler
    dhcls = load_object(path)
  File "d:\python\scraping-project\scrapy\venv\lib\site-packages\scrapy\utils\misc.py", line 61, in load_object
    mod = import_module(module)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 790, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "d:\python\scraping-project\scrapy\venv\lib\site-packages\scrapy\core\downloader\handlers\http.py", line 2, in <module>
    from scrapy.core.downloader.handlers.http11 import (
  File "d:\python\scraping-project\scrapy\venv\lib\site-packages\scrapy\core\downloader\handlers\http11.py", line 23, in <module>
    from scrapy.core.downloader.contextfactory import load_context_factory_from_settings
  File "d:\python\scraping-project\scrapy\venv\lib\site-packages\scrapy\core\downloader\contextfactory.py", line 11, in <module>
    from scrapy.core.downloader.tls import DEFAULT_CIPHERS, openssl_methods, ScrapyClientTLSOptions
  File "d:\python\scraping-project\scrapy\venv\lib\site-packages\scrapy\core\downloader\tls.py", line 23, in <module>
    METHOD_SSLv3: SSL.SSLv3_METHOD,                     # SSL 3 (NOT recommended)
AttributeError: module 'OpenSSL.SSL' has no attribute 'SSLv3_METHOD'
Ineffectual answered 26/9, 2022 at 19:49 Comment(4)
SSL v2 and v3 were deprecated long time ago due to security issues. I would check if they weren't removed from recent SW versions.Umbrella
bro. How can I check whether they were removed or not from recent SW versions?Ineffectual
This answer by Barney Govan contains a helpful information that this is a reported issue. The issue number is now 5635 (5638 was a dup). Follow the discussion there. Hopefully there will be a fix or a workarond soon.Umbrella
I have solved this issue by commenting the line on the following virtual environment folder=> Lib\site-packages\scrapy\core\downloader\tls.py => commented out the METHOD_SSLv3: SSL.SSLv3_METHOD,Ineffectual
S
23

I had the same problem, and I solved it with this:

pip install pyopenssl==22.0.0
Steib answered 27/9, 2022 at 12:28 Comment(1)
Thanks, this helped me fix a similar error AttributeError: module 'OpenSSL.SSL' has no attribute 'TLS_SERVER_METHOD'. I had to change the version of your command because I got this error with version 22.0.0: mitmproxy 9.0.1 requires pyOpenSSL<22.2,>=22.1, but you have pyopenssl 22.0.0 which is incompatible.. So to make latest mitmproxy work, I had to execute pip install pyopenssl==22.1.Pacha
A
20
# ImportError: cannot import name 'SSLv3_METHOD' from 'OpenSSL.SSL'
pip3 install pyopenssl==22.0.0

# AttributeError: module 'lib' has no attribute 'OpenSSL_add_all_algorithms'
pip3 install cryptography==38.0.4

You need to set a special version of pyopenssl, SSLv2 and SSLv3 is no longer supported after 22.1.0. changelog here

enter image description here

Athletics answered 10/2, 2023 at 2:57 Comment(1)
Two errors, two solutions. ThanksCandlepower
S
5

The solution for me was using also cryptografy 38.0.4.

...
# need this version of pyopenssl to avoid SSL3 error
pyOpenSSL==22.0.0
# need this version of cryptography to avoid error "module 'lib' has no attribute 'OpenSSL_add_all_algorithms"
cryptography==38.0.4
...

I hope it may helps.

Sacha answered 31/1, 2023 at 13:32 Comment(0)
M
3

I think this is due to an update of cryptography through PyOpenSSL -- I've logged an issue with Scrapy (https://github.com/scrapy/scrapy/issues/5638) but the workaround that worked for me was to downgrade cryptography:

pip install "cryptography<38"
Matisse answered 26/9, 2022 at 22:47 Comment(2)
Bro. It still didn't solve this problem. I am getting the same problemIneffectual
Plus 1 for finding this. The issue number is now 5635 (5638 was a dup).Umbrella
I
1

I have solved this issue by commenting the line on the following virtual environment folder=> Lib\site-packages\scrapy\core\downloader\tls.py => commented out the METHOD_SSLv3: SSL.SSLv3_METHOD,

Ineffectual answered 27/9, 2022 at 12:53 Comment(3)
Great work. This fixed itAssamese
This solution will not work in production where you have no control(you should not have it) over the site-packages folder.Ceiling
The authority then replaced and deleted those lines of code afterward. This was a slight bug. Thank youIneffectual
P
0

I got the same error while deploying the code to scrapy hub.

Turned out Scrapy_hub was using an older version of scrapy i.e. version 2.0 and this forum helped me set the newer version.

For me, This worked:

  • add this code at the end of the file: scrapinghub.yml
stacks:
  default: scrapy:2.11-20240514
Poignant answered 25/5 at 11:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.