Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python 2.7中实现SSL和CGIHTTPServer?(SSL错误)_Python_Python 2.7_Ssl_Webserver - Fatal编程技术网

如何在Python 2.7中实现SSL和CGIHTTPServer?(SSL错误)

如何在Python 2.7中实现SSL和CGIHTTPServer?(SSL错误),python,python-2.7,ssl,webserver,Python,Python 2.7,Ssl,Webserver,我试图在Python2.7中从BaseHTTPServer实现一个SSL包装的套接字和CGIHTTPServer。我在SimpleHTTPRequestHandler中使用了这个功能,但我需要包括CGI功能 注释的代码来自之前的迭代,因为我一直在尝试解决这个问题。如果 ServerClass=HTTPServer(请参阅run()),我得到(错误代码:ssl\u Error\u rx\u record\u too\u long)。如果我使用ServerClass=SecureHTTPServer

我试图在Python2.7中从BaseHTTPServer实现一个SSL包装的套接字和CGIHTTPServer。我在SimpleHTTPRequestHandler中使用了这个功能,但我需要包括CGI功能

注释的代码来自之前的迭代,因为我一直在尝试解决这个问题。如果
ServerClass=HTTPServer
(请参阅
run()
),我得到
(错误代码:ssl\u Error\u rx\u record\u too\u long)
。如果我使用
ServerClass=SecureHTTPServer
,我可以在端口80上加载页面,但在443上会重置连接

我非常感谢您帮助我获得支持CGI的支持SSL的Web服务器

#!/usr/bin/env python
import socket, os, sys
from SocketServer import BaseServer
from BaseHTTPServer import HTTPServer
from CGIHTTPServer import *
import cgitb; cgitb.enable()
from OpenSSL import SSL


class SecureHTTPServer(HTTPServer):
    def __init__(self, server_address, HandlerClass):
        BaseServer.__init__(self, server_address, HandlerClass)
        Context = SSL.Context(SSL.SSLv23_METHOD)
        pem = 'C:\SERV\cert.pem'
        Context.use_privatekey_file(pem)
        Context.use_certificate_file(pem)
        self.socket = SSL.Connection(Context, socket.socket(self.address_family,self.socket_type))
        self.server_bind()
        self.server_activate()

class SecureHTTPRequestHandler(CGIHTTPRequestHandler):
    cgi_directories = ['/cgi']


    #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 run(HandlerClass = SecureHTTPRequestHandler,ServerClass = SecureHTTPServer):
    server_address = ('localhost', 443)
    handler = CGIHTTPRequestHandler
    server = ServerClass(server_address, handler)
    sa = server.socket.getsockname()

    try:
        print "Server SSL HTTPS on", sa[0], "port", sa[1], "..."
        server.serve_forever()

    except:
        server.socket.closed()

if __name__ == '__main__':
    run()