Giter VIP home page Giter VIP logo

Comments (3)

snooppr avatar snooppr commented on June 15, 2024

requests=2.31.0

urllib3=2.0.1
This code works:

import ssl
import requests
import urllib3

ctx = urllib3.util.create_urllib3_context(ciphers=':HIGH:!DH:!aNULL', cert_reqs=ssl.CERT_NONE)
adapter = requests.adapters.HTTPAdapter()
adapter.init_poolmanager(connections=2, maxsize=2, block=False, ssl_minimum_version=ssl.TLSVersion.TLSv1, ssl_context=ctx)
requests_future = requests.Session()
requests_future.mount('https://', adapter)

print(requests_future.get("https://nhl.ru"))

<Response [404]>


The same code, but does not work for multiprocessing (1 process):

import ssl
import requests
import urllib3

from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, TimeoutError

executor1 = ProcessPoolExecutor(max_workers=1)

ctx = urllib3.util.create_urllib3_context(ciphers=':HIGH:!DH:!aNULL', cert_reqs=ssl.CERT_NONE)
adapter = requests.adapters.HTTPAdapter()
adapter.init_poolmanager(connections=2, maxsize=2, block=False, ssl_minimum_version=ssl.TLSVersion.TLSv1, ssl_context=ctx)
req = requests.Session()
req.mount('https://', adapter)

a = []
b = executor1.submit(req.get, url="https://nhl.ru/", timeout=12)

a.append(b)

for i in a:
    print(i.result())

raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='nhl.ru', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1, '[SSL: DH_KEY_TOO_SMALL] dh key too small (_ssl.c:1125)')))

The same code, but processes are replaced with threads and the code works successfully:

import ssl
import requests
import urllib3

from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, TimeoutError

executor1 = ThreadPoolExecutor(max_workers=1)

ctx = urllib3.util.create_urllib3_context(ciphers=':HIGH:!DH:!aNULL', cert_reqs=ssl.CERT_NONE)
adapter = requests.adapters.HTTPAdapter()
adapter.init_poolmanager(connections=2, maxsize=2, block=False, ssl_minimum_version=ssl.TLSVersion.TLSv1, ssl_context=ctx)
req = requests.Session()
req.mount('https://', adapter)

a = []
b = executor1.submit(req.get, url="https://nhl.ru/", timeout=12)

a.append(b)

for i in a:
    print(i.result())

<Response [404]>


urllib3=1.26.18 This code works. or multiprocessing (1 process):

import ssl
import requests
import urllib3

from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, TimeoutError

executor1 = ProcessPoolExecutor(max_workers=1)

requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS += ':HIGH:!DH:!aNULL'
adapter = requests.adapters.HTTPAdapter()
adapter.init_poolmanager(connections=2, maxsize=2, block=False)
req = requests.Session()
req.mount('https://', adapter)

a = []
b = executor1.submit(req.get, url="https://nhl.ru/", timeout=12)

a.append(b)

for i in a:
    print(i.result())

<Response [404]>


If you have difficulty migrating to v2.0 or following this guide you can open an issue on GitHub Doc.

How to make the code work after migrating to urllib3 >2?

from urllib3.

snooppr avatar snooppr commented on June 15, 2024

I tested the above code on Android/Termux python3.11 (same error when using multiprocessing, but threads work).

I found a workaround:
$ /etc/ssl/openssl.cnf
CipherString = DEFAULT@SECLEVEL=2 -> CipherString = DEFAULT@SECLEVEL=1
Then multiprocessing does not cause an error.

Any ideas on how to use multiprocessing without editing 'openssl.cnf'? Let me remind you that in previous versions of urllib3 the hack urllib3.util.ssl_.DEFAULT_CIPHERS += ':HIGH:!DH:!aNULL' worked. Now it is broken and the proposed alternative does not work (It would be more correct to say that it only works for threads, not processes.).

broken processes (Replace processes with threads and the code will work (replace the line executor1 = ProcessPoolExecutor(max_workers=2) -> executor1 = ThreadPoolExecutor(max_workers=2)))

import ssl
import urllib3

from urllib3 import PoolManager
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor

executor1 = ProcessPoolExecutor(max_workers=2)

ctx = urllib3.util.create_urllib3_context(ciphers=':HIGH:!DH:!aNULL', cert_reqs=ssl.CERT_NONE)
http = urllib3.PoolManager(ssl_context=ctx)

a = []
b = executor1.submit(http.request, "GET", "https://nhl.ru/")

a.append(b)

for i in a:
    print(i.result().status)

Please respond to the developers, or the problem will be closed due to the impossibility of feedback.

from urllib3.

snooppr avatar snooppr commented on June 15, 2024

solution, use the old version 1x.

from urllib3.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.