0

I am trying to run a few Python CGI scripts using CGIHTTPServer. This works fine, but now I'd like to run using SSL on the Web listener.

I started with the solution at Activestate code recipes and changed SimpleHTTPRequestHandler to CGIHTTPRequestHandler. This works fine serving static files, including the cgi scripts (cgi-bin is, for now, a link to ".") but for cgi scripts Firefox shows

"SSL received a record that exceeded the maximum permissible length"

Error code: ssl_error_rx_record_too_long.

Web searches indicate this is likely a server issue, not browser, which seems logical to me. Also, I've tried all the suggested browser settings. I've tried a variety of server settings also, but no joy.

  • Platform is RHEL 2.6.18
  • Python is 2.4.3 - please don't suggest using "import ssl" as it isn't available
  • Self signed certificate, accepted in browser (static files work)

code:

import socket, sys    
import SocketServer
import BaseHTTPServer
import CGIHTTPServer
from OpenSSL import SSL


class SecureHTTPServer(BaseHTTPServer.HTTPServer):
    def __init__(self, server_address, HandlerClass):
        self.allow_reuse_address = True
        SockeServer.BaseServer.__init__(self, server_address, HandlerClass)
        ctx = SSL.Context(SSL.SSLv23_METHOD)
        ctx.use_privatekey_file ("path/key.pem")
        ctx.use_certificate_file("path/cert.pem")
        self.socket = SSL.Connection(ctx, socket.socket(self.address_family,                                                       self.socket_type))
        self.server_bind()
        self.server_activate()

class SecureHTTPRequestHandler(CGIHTTPServer.CGIHTTPRequestHandler):
    def setup(self):
        self.cgi_directories= ['cgi-bin']
        self.connection = self.request
        self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
        self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)

def test(HandlerClass = SecureHTTPRequestHandler, ServerClass = SecureHTTPServer):
    httpd = ServerClass(('0.0.0.0',4443), HandlerClass)
    sa = httpd.socket.getsockname()
    print "Serving HTTPS on", sa[0], "port", sa[1], "..."
    httpd.serve_forever()

if __name__ == '__main__':
    test()

How may I get this working for a quick and easy web interface in Python 2.4?

Community
  • 1
  • 1
mpez0
  • 2,815
  • 17
  • 12

1 Answers1

0

Wound up using Apache httpd to get SSL working with the existing Python CGI scripts. I'd still like to know how to get it working with the CGIHTTPServer class, though

mpez0
  • 2,815
  • 17
  • 12