One recurring complaint is that nobody talks about code on /prog/. So everyone write go and write some code, any code, that does something, anything, and post it. A small explanation wouldn't go amiss either.
# check whether to keep the connection open
if http_ver != 'http/1.1' or ('connection:', 'close') in headers or 'keep-alive:' in map(lambda (a, b): a, headers):
break
# get rid of empty lines before the next request
line = []
while line == []:
line = f.readline().lower().strip().split()
# parse command line arguments
argv = sys.argv[1:]
while argv:
if argv[0] == '-h':
# print help and exit
print 'options: -h display this help'
print ' -p [port] set port'
print ' -H [hostname] set hostname'
print ' -r [dir] set root directory'
exit()
elif argv[0] == '-p':
# set port number
if len(argv) == 1:
print '\'-p\' requires an argument'
exit()
port = int(argv[1])
argv = argv[2:]
continue
elif argv[0] == '-H':
# set hostname
if len(argv) == 1:
print '\'-H\' requires an argument'
exit()
host = argv[1]
argv = argv[2:]
continue
elif argv[0] == '-r':
# set root directory
if len(argv) == 1:
print '\'-r\' requires an argument'
exit()
root = argv[1]
argv = argv[2:]
# open a port
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn.bind((host, port))
conn.listen(1)
# accept requests and handle in a new thread
while True:
thread.start_new_thread(handle, conn.accept())