tlslite 经典例子

 

import os.path
import socket

from tlslite.api import *

lsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
address = ("localhost", 4443)
lsock.bind(address)
lsock.listen(5)

connection = TLSConnection(lsock.accept()[0])

dir="."
x509Cert = X509().parse(open(os.path.join(dir, "serverX509Cert.pem")).read())
x509Chain = X509CertChain([x509Cert])
s = open(os.path.join(dir, "serverX509Key.pem")).read()
x509Key = parsePEMKey(s, private=True)

#
settings = HandshakeSettings()
settings.minVersion = (3,0)
settings.maxVersion = (3,0)
connection.handshakeServer(certChain=x509Chain, privateKey=x509Key, reqCert=True, settings=settings)
#

#connection.handshakeServer(certChain=x509Chain, privateKey=x509Key, reqCert=True)

print "Test 14 - good mutual X.509"
assert(isinstance(connection.session.serverCertChain, X509CertChain))
connection.close()
connection.sock.close()

 

 

import os.path
import socket

from tlslite.api import *

def connect():
	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	if hasattr(sock, 'settimeout'): #It's a python 2.3 feature
	    sock.settimeout(5)
	address = ("localhost", 4443)
	sock.connect(address)
	c = TLSConnection(sock)
	return c

connection = connect()

dir="."
print "Test 14 - good mutual X509"
x509Cert = X509().parse(open(os.path.join(dir, "clientX509Cert.pem")).read())
x509Chain = X509CertChain([x509Cert])
s = open(os.path.join(dir, "clientX509Key.pem")).read()
x509Key = parsePEMKey(s, private=True)

#
settings = HandshakeSettings()
settings.minVersion = (3,0)
settings.maxVersion = (3,0)
connection.handshakeClientCert(x509Chain, x509Key, settings=settings)
#

#connection.handshakeClientCert(x509Chain, x509Key)

assert(isinstance(connection.session.serverCertChain, X509CertChain))
connection.close()
connection.sock.close()

你可能感兴趣的:(C++,c,socket,python,OS)