Shammer's Philosophy

My private adversaria

Infinite KeepAlive Simple HTTP Server with python socket library

In Python Socket Server with socket library - Shammerism, I wrote a simple http server with python. This always returns same response and HTTP Request parsing is not sufficient. And there are a lot of lacks, this should be used only for test purpose.
But, I faced with the case that this server is not matched for my test case. This always closes client sockets after sending response, so this is not matched if I would like to test with HTTP keep-alive connections. So I re-wrote a little bit it. This version never closes client sockets without received FIN or RST from clients.

#!/usr/bin/env python
import argparse
import socket
from socket import gethostname
from time import gmtime, strftime

parser = argparse.ArgumentParser()
parser.add_argument('-host', dest = 'dest_ip', default = 'localhost')
parser.add_argument('-port', dest = 'dest_port', default = '8000')
parser.add_argument('-debug', dest = 'debug', default = False)
param = vars(parser.parse_args())

listen_addr = param['dest_ip']
listen_port = param['dest_port']
debug = param['debug']

server = socket.socket(socket.AF_INET)
try:
    print 'Listening on ' + listen_addr + ':' + listen_port
    server.bind((listen_addr,int(listen_port)))
    server.listen(1)
    while True:
        try:
            client, addr= server.accept()
            request = []
            while True:
                char = client.recv(1)
                try:
                    if char == '':
                        print 'FIN received from client.'
                        client.close()
                        break
                    byte = ord(char)
                    request.append(byte)
                    if 2 < len(request) and request[-2] == 13 and request[-1] == 10:
                        if request[-4] == 13 and request[-3] == 10:
                            if debug:
                                print '##########'
                                print '#'
                                print '# Finished HTTP REQUEST Header parsing!!!'
                                print '#'
                                print '##########'
                            body = strftime("%b %d %Y %H:%M:%S", gmtime()) + " " + gethostname() + '\n'
                            response = '\r\n'.join([
                                "HTTP/1.1 200 OK",
                                "Server: KeepAliveWebServer",
                                "Content-Type: text/plain;charset=UTF-8",
                                "Content-Length: " + str(len(body)),
                                ""
                            ]);
                            response = response + '\r\n' + body
                            client.send(response)
                except Error, e:
                    print 'Closing a client socket because unexpected error happened, ' + e.args[1]
                    client.close()
                    break
        except socket.error, se:
            print 'Error : ' + str(se.args[0]) + ':' + se.args[1]
finally:
    server.close()

I want to implement with threads in the future. This HTTP Server can handle only 1 client at the same time. Using thread can resolve this problem.