基于BaseHTTPServer的HTTPS服务器

最近写了一个Https的Server,用BaseHttpServer写的,重写了setup方法,使用python2.6的ssl库

#!/usr/bin/env python

from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
import ssl
import os
class HTTPRequestHandler(BaseHTTPRequestHandler):
    def setup(self):
        SSLSocket = ssl.wrap_socket(self.request,server_side=True, keyfile="key.pem", certfile="cert.pem",ca_certs="ca.crt",ssl_version=ssl.PROTOCOL_TLSv1)
        self.rfile = SSLSocket.makefile('rb', self.rbufsize)
        self.wfile = SSLSocket.makefile('wb', self.wbufsize)

    def do_GET(self):
        req_time = int(time.time())
        clt_IP = self.client_address[0]

        self.protocol_version = "HTTP/1.1"
        self.server_version = "CenterServiceMock"

        self.send_response(200)
        self.end_headers()
        self.wfile.write("This is ServiceSerice %s"%clt_IP)

if __name__=='__main__':

    server = HTTPServer(('',443),HTTPRequestHandler)
    #Thread.server.serve_forever()
    import thread
    thread.start_new_thread(server.serve_forever,())

程序很好写就添加1行代码,但是获取证书密钥把我折腾坏了:

<OPENSSL=${OPENSSL:-openssl}

COUNT=${COUNT:-10}
NPROCS=${NPROC:-$(getconf _NPROCESSORS_ONLN)}

makecert() {
  name=$1

  $OPENSSL genrsa -out ${name}.key 2048
  $OPENSSL req -new -key ${name}.key -out ${name}.csr \
    -subj /C=US/ST=CA/L=Norm/O=YLU/OU=Test/CN=${name}.com
  $OPENSSL x509 -req -days 365 \
    -in ${name}.csr -signkey ${name}.key -out ${name}.crt
  cat ${name}.crt ${name}.key > ${name}.pem
  rm -rf ${name}.csr ${name}.key ${name}.crt
}

for (( i = 0 ; i < $COUNT ; i += $NPROCS )); do
  for ((j = i; j < (i + $NPROCS) && j < $COUNT; j++ )) ; do
    makecert "test${j}" &
  done
  wait
  for ((j = i; j < (i + $NPROCS) && j < $COUNT; j++ )) ; do
    echo ssl_cert_name=test$i.pem
  done >> ssl_multicert.config
done





你可能感兴趣的:(基于BaseHTTPServer的HTTPS服务器)