Skip to content

Instantly share code, notes, and snippets.

@FluffyDietEngine
Created December 1, 2023 04:26
Show Gist options
  • Save FluffyDietEngine/94c0137445555a418ac9f332edfa6f4b to your computer and use it in GitHub Desktop.
Save FluffyDietEngine/94c0137445555a418ac9f332edfa6f4b to your computer and use it in GitHub Desktop.
Bypass `[SSL: UNSAFE_LEGACY_RENEGOTIATION_DISABLED] unsafe legacy renegotiation disabled (_ssl.c:1006)` with custom `requests.adapters.HTTPAdapter`
from requests import Session
from requests import adapters
from urllib3 import poolmanager
from ssl import create_default_context, Purpose, CERT_NONE
class CustomHttpAdapter (adapters.HTTPAdapter):
def __init__(self, ssl_context=None, **kwargs):
self.ssl_context = ssl_context
super().__init__(**kwargs)
def init_poolmanager(self, connections, maxsize, block=False):
self.poolmanager = poolmanager.PoolManager(
num_pools=connections, maxsize=maxsize,
block=block, ssl_context=self.ssl_context)
def ssl_supressed_session():
ctx = create_default_context(Purpose.SERVER_AUTH)
# to bypass verification after accepting Legacy connections
ctx.check_hostname = False
ctx.verify_mode = CERT_NONE
# accepting legacy connections
ctx.options |= 0x4
session = Session()
session.mount('https://', CustomHttpAdapter(ctx))
return session
# testing session
url = "http://bhulekh.uk.gov.in/public/public_ror/Public_ROR.jsp"
response = ssl_supressed_session().get(url, verify=False) # notice verify=False
with open("test.html", "w") as _f:
_f.write(response.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment