Created
October 10, 2012 14:57
-
-
Save messa/3866150 to your computer and use it in GitHub Desktop.
Python snippet for forcing SSL version
This file contains 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
#!/usr/bin/env python | |
# -*- coding: UTF-8 -*- | |
from contextlib import contextmanager | |
import ssl | |
@contextmanager | |
def inject_ssl_version(version=ssl.PROTOCOL_TLSv1): | |
""" | |
Upraví ssl.wrap_socket tak, aby byla použita konkrétní verze SSL. | |
Někdy je toto vnucení verze SSL potřeba pro připojení na některé | |
servery (např. paymentreports.facebook.com). Tento problém byl | |
pozorován např. na Debian testing a MacPorts. | |
""" | |
old_wrap_socket = ssl.wrap_socket | |
def new_wrap_socket(*args, **kwargs): | |
assert "ssl_version" not in kwargs | |
kwargs["ssl_version"] = version | |
return old_wrap_socket(*args, **kwargs) | |
ssl.wrap_socket = new_wrap_socket | |
try: | |
yield | |
finally: | |
ssl.wrap_socket = old_wrap_socket | |
if __name__ == "__main__": | |
import requests | |
with inject_ssl_version(): | |
print requests.get("https://paymentreports.facebook.com/") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment