Python HTTP SSL服务器接受连接的速度很慢

时间:2014-07-24 04:50:05

标签: python linux ssl rhel

以下python代码用于创建HTTPS服务器。

'''
Created on 23/07/2014

@author: o880732
'''

import sys
import BaseHTTPServer
import base64
from multiprocessing import Process, Queue
from httplib import HTTPSConnection
import ssl
import datetime

class MyHttpEndpointHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def __init__(self, q, *args):
        self.q = q
        BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, *args)

    def do_POST(self):
        self.send_response(200, 'OK')
        self.send_header('Server', 'Apache-Coyote/1.1')
        self.send_header('Content-type','text/xml')
        self.end_headers()
        # Extract and print the contents of the POST
        length = int(self.headers['Content-Length'])
        print "Length of content is " + str(length)
        post_data = self.rfile.read(length)
        self.q.put(post_data)


class ServerEndpointHandler:
    def __init__(self,q,keyfile,certfile):
        self.q = q
        self.keyfile = keyfile
        self.certfile = certfile

    def setupEndpointHandler(self, queue):
        return lambda *args: MyHttpEndpointHandler(queue, *args)

    def handleEndpoint(self):
        print "Starting endpoint handler"
        handler = self.setupEndpointHandler(self.q)
        httpd = BaseHTTPServer.HTTPServer(('',8443), handler)
        httpd.socket = ssl.wrap_socket(httpd.socket, keyfile=self.keyfile, certfile = self.certfile, server_side=True)
        httpd.serve_forever()

if __name__ == '__main__':
    q = Queue()
    print "trying to start endpoint"
    ep = ServerEndpointHandler(q, keyfile="~/certificates/server.key", certfile = "~/certificates/server.crt")
    t2 = Process(target=ep.handleEndpoint)
    t2.start()
    t2.join()

我遇到的问题是在RedhatLinux服务器上接受连接的时间大约是20秒,而在Windows7下运行的相同代码的连接时间大约是200毫秒。

两个系统都运行Python 2.6.6。

redhat版本是6.4(圣地亚哥)

Linux myserver.domain.com 2.6.32-358.18.1.el6.x86_64 #1 SMP Fri Aug 2 17:04:38 EDT 2013 x86_64 x86_64 x86_64 GNU/Linux

我已将openssl和python包更新为Redhat的当前版本。

Installed Packages
openssl.x86_64                                                       1.0.1e-16.el6_5.14                                                        @rhel-x86_64-server-6
python.x86_64                                                        2.6.6-52.el6                                                              @rhel-x86_64-server-6
Available Packages
openssl.i686                                                         1.0.1e-16.el6_5.14                                                        rhel-x86_64-server-6
python.i686                                                          2.6.5-3.el6_0.2                                                           rhel-x86_64-server-6

我认为延迟与协议协商有关,我想知道在哪里寻求解决这个问题,或者问题可能是什么。

任何线索?

1 个答案:

答案 0 :(得分:2)

听起来像DNS失败。您可以尝试将此类内容添加到MyHttpEndpointHandler

def address_string(self):
    """ Override address_string() with a version that skips the 
        reverse lookup.

    """
    return '%s:%s' % self.client_address