Created
January 31, 2024 19:09
-
-
Save hteo1337/efb295258181737bae95610f1a9138d9 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`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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