Simple HTTP Server that will give serve simple files and can emulate delay before giving response

On one of my projects i had a task where i needed to make a  simple http server that would give one xml file for our service (Trying to emulate and verify sync between two services). But besides that i had also to check that application behaves properly when you get timeout from the server on the other end. So, i've started googling and found really nice and fast solution with python SimpleHTTPServer. I found it here and modified it a little bit for my purposes with delay emulation. Here's the code :

#!/usr/bin/env python
 
import SimpleHTTPServer, BaseHTTPServer, SocketServer, socket, time, sys
 
class ThreadedHTTPServer(SocketServer.ThreadingMixIn,
                         BaseHTTPServer.HTTPServer) :
    """
    New features w/r to BaseHTTPServer.HTTPServer:
    - serves multiple requests simultaneously
    - catches socket.timeout and socket.error exceptions (raised from
      RequestHandler)
    """
    def setDelay(self):
        try:
            delay = sys.argv[2]
        except IndexError:
            delay = 0.0
        return delay
    
    def __init__(self, *args):
        BaseHTTPServer.HTTPServer.__init__(self,*args)
 
    def process_request_thread(self, request, client_address):
        """
        Overrides SocketServer.ThreadingMixIn.process_request_thread
        in order to catch socket.timeout
        """
        try:
            time.sleep(self.setDelay())
            self.finish_request(request, client_address)
            self.close_request(request)
        except socket.timeout:
            print 'Timeout during processing of request from',
            print client_address
        except socket.error, e:
            print e, 'during processing of request from',
            print client_address
        except:
            self.handle_error(request, client_address)
            self.close_request(request)
 
 
class TimeoutHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    """
    Abandon request handling when client has not responded for a
    certain time. This raises a socket.timeout exception.
    """
 
    # Class-wide value for socket timeout
    timeout = 3 * 60 
 
    def setup(self):
        "Sets a timeout on the socket"
        self.request.settimeout(self.timeout)
        SimpleHTTPServer.SimpleHTTPRequestHandler.setup(self)
 
 
def main():
    try:
        BaseHTTPServer.test(TimeoutHTTPRequestHandler, ThreadedHTTPServer)
        print "Delay time is %s" %self.delay
    except KeyboardInterrupt:
        print '^C received, shutting down server'
 
if __name__ == '__main__':
    main()

You can save it as server.py and they simple run it on your server as: python server.py 8081 It will run this server on 8081 port ( you can change it to any free port you want) . Second value is for delay - 0 if you want server to respond right away, !=0 - set how many seconds you want server to wait before responding.

It's very fast and easy way to check this things, and i'm pretty sure i will use it in the future and maybe will modify and improve it when it will be needed. The beauty of this method is that you don't have to setup any server and launch it. All you need is installed Python ( which is already preinstalled on any Unix machine) and this file.