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 postimport import when_imported | |
| @when_imported('threading') | |
| def warn_threads(mod): | |
| print('Threads? Are you crazy?') | |
| if __name__ == '__main__': | |
| import threading |
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 postimport import when_imported | |
| from functools import wraps | |
| def logged(func): | |
| @wraps(func) | |
| def wrapper(*args, **kwargs): | |
| print('Calling', func.__name__, args, kwargs) | |
| return func(*args, **kwargs) | |
| return wrapper |
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
| # postimport.py | |
| import importlib | |
| import sys | |
| from collections import defaultdict | |
| _post_import_hooks = defaultdict(list) | |
| class PostImportFinder: | |
| def __init__(self): |
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
| import mymodule | |
| a = mymodule.A() | |
| a.spam() | |
| b = mymodule.B() | |
| b.bar() |
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
| # echoclient.py | |
| # | |
| # An example of a client that connects to an SSL server | |
| # and verifies its certificate | |
| from socket import socket, AF_INET, SOCK_STREAM | |
| import ssl | |
| s = socket(AF_INET, SOCK_STREAM) |
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 socket import socket, AF_INET, SOCK_STREAM | |
| from socket import SOL_SOCKET, SO_REUSEADDR | |
| import ssl | |
| KEYFILE = 'server_key.pem' # Private key of the server | |
| CERTFILE = 'server_cert.pem' # Server certificate (given to client) | |
| def echo_client(s): | |
| while True: | |
| data = s.recv(8192) |
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
| # ssl_xmlrpc_client.py | |
| # | |
| # An XML-RPC client that verifies the server certificate | |
| from xmlrpc.client import SafeTransport, ServerProxy | |
| import ssl | |
| class VerifyCertSafeTransport(SafeTransport): | |
| def __init__(self, cafile, certfile=None, keyfile=None): | |
| super().__init__() |
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
| # ssl_xmlrpc_server.py | |
| # | |
| # An example of an SSL-XMLRPC Server. | |
| import ssl | |
| from xmlrpc.server import SimpleXMLRPCServer | |
| from sslmixin import SSLMixin | |
| class SSLSimpleXMLRPCServer(SSLMixin, SimpleXMLRPCServer): | |
| pass |
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
| import ssl | |
| class SSLMixin: | |
| def __init__(self, *args, | |
| keyfile=None, certfile=None, ca_certs=None, cert_reqs=ssl.CERT_NONE, | |
| **kwargs): | |
| self._keyfile = keyfile | |
| self._certfile = certfile | |
| self._ca_certs = ca_certs | |
| self._cert_reqs = cert_reqs |
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 urllib.request import urlopen | |
| u = urlopen('http://localhost:8080/hello?name=Guido') | |
| print(u.read().decode('utf-8')) | |
| u = urlopen('http://localhost:8080/localtime') | |
| print(u.read().decode('utf-8')) |