Created
April 10, 2015 17:01
-
-
Save Averroes/63762183e91d752db96a to your computer and use it in GitHub Desktop.
adding ssl to network servers
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) | |
| if data == b'': | |
| break | |
| s.send(data) | |
| s.close() | |
| print('Connection closed') | |
| def echo_server(address): | |
| s = socket(AF_INET, SOCK_STREAM) | |
| s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) | |
| s.bind(address) | |
| s.listen(1) | |
| # Wrap with an SSL layer requiring client certs | |
| s_ssl = ssl.wrap_socket(s, | |
| keyfile=KEYFILE, | |
| certfile=CERTFILE, | |
| server_side=True | |
| ) | |
| # Wait for connections | |
| while True: | |
| try: | |
| c,a = s_ssl.accept() | |
| print('Got connection', c, a) | |
| echo_client(c) | |
| except Exception as e: | |
| print('{}: {}'.format(e.__class__.__name__, e)) | |
| echo_server(('', 20000)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment