Hi,
I am modifying the code found below to work as a CGI handler.
To enable CGI, I modified the HTTPRequestHandler to inherit from CGIHTTPRequestHandler instead. I changed the port to 4443 so that I could run it as non-root. I modified the .pem file to be ./server.pem, which is relative to the path the server script is running on.
To verify that this worked correctly, I ran it, loaded up https://mypage/ and clicked around. Files outside of the CGI directory load correctly.
I created the cgi-bin subdirectory as per the expectations of CGIHTTPRequestHandler, and placed a test.py file in there, which simply returns a content header and "hello" message.
Test.py is set to have permissions 755.
However, I get this error: Error code: ssl_error_rx_record_too_long from Firefox v. 3.
According to searching on this error, this often comes from accessing a HTTP server when HTTPS is denoted. However, I can access a test HTML page; it's only the CGI that falls over.
What am I doing wrong?
'''
SimpleSecureHTTPServer.py - simple HTTP server supporting SSL.
- replace fpem with the location of your .pem server file.
- the default port is 443.
usage: python SimpleSecureHTTPServer.py
'''
import socket, os
from SocketServer import BaseServer
from BaseHTTPServer import HTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
from CGIHTTPServer import * #added this
from OpenSSL import SSL
class SecureHTTPServer(HTTPServer):
def __init__(self, server_address, HandlerClass):
BaseServer.__init__(self, server_address, HandlerClass)
ctx = SSL.Context(SSL.SSLv23_METHOD)
#server.pem's location (containing the server private key and
#the server certificate).
fpem = './server.pem' #changed this
ctx.use_privatekey_file (fpem)
ctx.use_certificate_file(fpem)
self.socket = SSL.Connection(ctx, socket.socket(self.address_family,
self.socket_type))
self.server_bind()
self.server_activate()
class SecureHTTPRequestHandler(CGIHTTPRequestHandler):
def setup(self):
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):
server_address = ('', 4443) # (address, port) Also changed this.
httpd = ServerClass(server_address, HandlerClass)
sa = httpd.socket.getsockname()
print "Serving HTTPS on", sa[0], "port", sa[1], "..."
httpd.serve_forever()
if __name__ == '__main__':
test()