##Python 2, 525 510 493 (bending the rules 483) chars from socket import* from os.path import* a=socket(2,1) a.bind(("",80)) a.listen(5) while 1: c,_=a.accept();i=c.recv(512).split("\n")[0].split();z=i[1][1:];m=i[2]+(i[0]!="GET"and"405 Not Supported\n\n"*2or exists(z)-1and"404 File Not Found\n\n"*2or"200 OK\n") if(len(m)>16)+isdir(z)<1: try:f=open(z,"rb");m+="Content-Type: "+{".txt":"text/plain","html":"text/html"}.get(z[-4:],"application/octet-stream")+"\n\n"+f.read() except:m=i[2]+"500 Server Error\n\n"*2 c.send(m);c.close()
Python 2, 525 510 493 (bending the rules 483) chars
from socket import* from os.path import* a=socket(2,1) a.bind(("",80)) a.listen(5) while 1: c,_=a.accept();i=c.recv(512).split("\n")[0].split();z=i[1][1:];m=i[2]+(i[0]!="GET"and"405 Not Supported\n\n"*2or exists(z)-1and"404 File Not Found\n\n"*2or"200 OK\n") if(len(m)>16)+isdir(z)<1: try:f=open(z,"rb");m+="Content-Type: "+{".txt":"text/plain","html":"text/html"}.get(z[-4:],"application/octet-stream")+"\n\n"+f.read() except:m=i[2]+"500 Server Error\n\n"*2 c.send(m);c.close() I stated that I only need 483 chars if I bend the rules because the last ;c.close() could be omitted. This is because the moment the next client gets accepted, the socket gets closed anyway. This will of course increase waiting time somewhat (Firefox will only display the page when the next client connects, Chrome will display it before, but will continue to load), but the rules do not require me to respond to the request immediately, only to do so at some point.
I am not certain whether this will work on Unixes because I used send instead of sendall and send does not guarantee to actually send everything it is fed. It does work on Windows.
