source: python/TLSLite/cert/server.py @ 26

Revision 26, 1.3 KB checked in by nakiki, 13 years ago (diff)
  • Property svn:executable set to *
Line 
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4import socket
5import SocketServer as ss
6import tlslite.api as tls
7
8# 証明書を使った場合の例
9class Handler(ss.StreamRequestHandler):
10  def setup(self):
11    self.connection = tls.TLSConnection(self.request)
12    self.connection.closeSocket = True
13    s = open("./serverX509Cert.pem").read()
14    x509 = tls.X509()
15    x509.parse(s)
16    certChain = tls.X509CertChain([x509])
17    s = open("./serverX509Key.pem").read()
18    privateKey = tls.parsePEMKey(s, private=True)
19 
20    settings = tls.HandshakeSettings()
21    settings.cipherNames = ["aes128"]
22    # 提示されるクライアントの証明書のfingerprint
23    checker = tls.Checker(x509Fingerprint= 'cbfef53d18298d7a432720a39232f0ce22e9a30a')
24    self.connection.handshakeServer(certChain=certChain, 
25      privateKey =privateKey, checker = checker, reqCert = True,
26      settings = settings)
27    self.rfile = self.connection.makefile('rb', self.rbufsize)
28    self.wfile = self.connection.makefile('wb', self.wbufsize)
29
30  def handle(self):
31    print 'handle'
32    while 1:
33      line = self.rfile.readline()
34      if len(line) == 0:
35        return
36      self.wfile.write(line)
37
38class Server(ss.ThreadingTCPServer):
39  allow_reuse_address = 1
40  daemon_threads = 1
41
42server = Server(('localhost', 3746), Handler)
43print 'listening:', server.socket.getsockname()
44server.serve_forever()
Note: See TracBrowser for help on using the repository browser.