#!/usr/bin/python # Very simple webserver written in Python. # Copyright (c) 2005 Steve.Cassidy@mq.edu.au # # To use this script, run it (eg. python webserver.py) # and then connect to http://localhost:50004/foo/bar.html # with your web browser # The script will only accept one connection and then terminate. # import socket HOST = '' # Symbolic name meaning the local host PORT = 50004 # Arbitrary non-privileged port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(1) conn, addr = s.accept() data = conn.recv(4096) words = data.split() if len(words) > 0 and words[0] == "GET": page = """ Hello

Your request was:

""" + data + """



"""

    header = """HTTP/1.0  200 ok
Content-length: """ + str(len(page)) + """
Content-type: text/html

"""
else:
    header = "HTTP/1.0  440 Page Not Found\n\n"
    page = ""

print header+page
conn.send(header+page)

# close the connection
conn.close()
# close the socket
s.close()