Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python HTTP SSL Server slow to accept connections

The following python code is used to create a HTTPS Server.

'''
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()

The issue I have is that on a RedhatLinux server the time to accept a connection is approximately 20sec, whereas the same code running under Windows7 the connection time is approximately 200msec.

Both systems are running Python 2.6.6.

The redhat release is 6.4 (Santiago)

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

I have updated the openssl and python packages to the current release for 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

I am presuming that the delay has something to do with protocol negotiation and am wondering where to look to solve this issue, or what else the issue may be.

Any clues?

like image 464
sweetfa Avatar asked Feb 24 '26 10:02

sweetfa


1 Answers

It kind of sounds like DNS failure. You might try adding something like this to MyHttpEndpointHandler:

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

    """
    return '%s:%s' % self.client_address
like image 152
William McBrine Avatar answered Feb 27 '26 00:02

William McBrine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!