# For a server to be a web server, it must understand HTTP requests. # HTTP request = header\r\n\r\nbody ( \r\n = newline in HTTP ) # HTTP response as well. So, a response header can be : # HTTP/1.1 200 OK\r\n Content-type:text/html\r\n\r\n
from socket import * import threading
# So here's a web server, that's good for one connection. # Open http://127.0.0.1:10000/ in your browser after running # this and you will see a page saying 'Hello'.
# Server - socket, bind, listen, accept, send # Client - socket, connect, send/recv # C ---ISN---> S # C <---ISN+1|J---S # c ---J+1----->S
class serverthread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
sd = socket(AF_INET, SOCK_STREAM)
sd.bind(('127.0.0.1', 10000))
sd.listen(5)
sockconn, caddr = sd.accept()
sockconn.send("HTTP/1.1 200 OK\r\n Content-type:text/html\r\n\r\nHello")
sockconn.close()
srvr = serverthread() srvr.start()
—
Posted using TwitterKun