| OLD | NEW |
| 1 # Author: Trevor Perrin |
| 2 # See the LICENSE file for legal information regarding use of this file. |
| 3 |
| 1 """TLS Lite + SocketServer.""" | 4 """TLS Lite + SocketServer.""" |
| 2 | 5 |
| 3 from tlslite.tlsconnection import TLSConnection | 6 from tlslite.tlsconnection import TLSConnection |
| 4 | 7 |
| 5 class TLSSocketServerMixIn: | 8 class TLSSocketServerMixIn: |
| 6 """ | 9 """ |
| 7 This class can be mixed in with any L{SocketServer.TCPServer} to | 10 This class can be mixed in with any L{SocketServer.TCPServer} to |
| 8 add TLS support. | 11 add TLS support. |
| 9 | 12 |
| 10 To use this class, define a new class that inherits from it and | 13 To use this class, define a new class that inherits from it and |
| 11 some L{SocketServer.TCPServer} (with the mix-in first). Then | 14 some L{SocketServer.TCPServer} (with the mix-in first). Then |
| 12 implement the handshake() method, doing some sort of server | 15 implement the handshake() method, doing some sort of server |
| 13 handshake on the connection argument. If the handshake method | 16 handshake on the connection argument. If the handshake method |
| 14 returns True, the RequestHandler will be triggered. Below is a | 17 returns True, the RequestHandler will be triggered. Below is a |
| 15 complete example of a threaded HTTPS server:: | 18 complete example of a threaded HTTPS server:: |
| 16 | 19 |
| 17 from SocketServer import * | 20 from SocketServer import * |
| 18 from BaseHTTPServer import * | 21 from BaseHTTPServer import * |
| 19 from SimpleHTTPServer import * | 22 from SimpleHTTPServer import * |
| 20 from tlslite.api import * | 23 from tlslite import * |
| 21 | 24 |
| 22 s = open("./serverX509Cert.pem").read() | 25 s = open("./serverX509Cert.pem").read() |
| 23 x509 = X509() | 26 x509 = X509() |
| 24 x509.parse(s) | 27 x509.parse(s) |
| 25 certChain = X509CertChain([x509]) | 28 certChain = X509CertChain([x509]) |
| 26 | 29 |
| 27 s = open("./serverX509Key.pem").read() | 30 s = open("./serverX509Key.pem").read() |
| 28 privateKey = parsePEMKey(s, private=True) | 31 privateKey = parsePEMKey(s, private=True) |
| 29 | 32 |
| 30 sessionCache = SessionCache() | 33 sessionCache = SessionCache() |
| (...skipping 18 matching lines...) Expand all Loading... |
| 49 | 52 |
| 50 def finish_request(self, sock, client_address): | 53 def finish_request(self, sock, client_address): |
| 51 tlsConnection = TLSConnection(sock) | 54 tlsConnection = TLSConnection(sock) |
| 52 if self.handshake(tlsConnection) == True: | 55 if self.handshake(tlsConnection) == True: |
| 53 self.RequestHandlerClass(tlsConnection, client_address, self) | 56 self.RequestHandlerClass(tlsConnection, client_address, self) |
| 54 tlsConnection.close() | 57 tlsConnection.close() |
| 55 | 58 |
| 56 #Implement this method to do some form of handshaking. Return True | 59 #Implement this method to do some form of handshaking. Return True |
| 57 #if the handshake finishes properly and the request is authorized. | 60 #if the handshake finishes properly and the request is authorized. |
| 58 def handshake(self, tlsConnection): | 61 def handshake(self, tlsConnection): |
| 59 raise NotImplementedError() | 62 raise NotImplementedError() |
| OLD | NEW |