Last active
December 29, 2017 20:42
-
-
Save aphor/3e700882cb74f7d51427eb7d09c6f53e to your computer and use it in GitHub Desktop.
a bare knuckles web server in ~40 lines of Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> | |
<HTML><HEAD> | |
<TITLE>404 Forbidden</TITLE> | |
</HEAD><BODY> | |
<H1>Forbidden</H1> | |
You do not have permission to access the file.<P> | |
<HR> | |
<ADDRESS>ws.py Server at nuada.local Port 6780</ADDRESS> | |
</BODY></HTML> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> | |
<HTML><HEAD> | |
<TITLE>404 Not Found</TITLE> | |
</HEAD><BODY> | |
<H1>File Not Found</H1> | |
The specified file does not exist on this server.<P> | |
<HR> | |
<ADDRESS>ws.py Server at nuada.local Port 6780</ADDRESS> | |
</BODY></HTML> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> | |
<HTML><HEAD> | |
<TITLE>500 Internal Server Error</TITLE> | |
</HEAD><BODY> | |
<H1>Internal Server Error</H1> | |
<P>The web server was unable to process the request because of an internal error. Please contact your site administrator to restore the love.</P> | |
<HR> | |
<ADDRESS>ws.py Server at nuada.local Port 6780</ADDRESS> | |
</BODY></HTML> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> | |
<HTML><HEAD> | |
<TITLE>501 Method Not Implemented</TITLE> | |
</HEAD><BODY> | |
<H1>Method Not Implemented</H1> | |
FORG to /index.html not supported.<P> | |
<HR> | |
<ADDRESS>ws.py Server at nuada.local Port 6780</ADDRESS> | |
</BODY></HTML> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# a bare knuckles web server in ~40 lines of Python | |
import socket, os | |
LISTENADDR = '' | |
LISTENPORT = 6780 | |
methods = ["GET"] | |
HTDOCROOT = '.' | |
HTERRROOT = '.' | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.bind((LISTENADDR, LISTENPORT)) | |
s.listen(1) | |
while 1: | |
conn, addr = s.accept() | |
print 'Connected via', addr | |
req = conn.recv(1024) | |
if not req: break | |
cmd = req.split(' ',2) | |
args = cmd.pop() | |
file = cmd.pop() | |
mthd = cmd.pop() | |
if mthd in methods: | |
filename = file.strip() | |
transname = os.path.join(HTDOCROOT,filename.lstrip('/')) | |
if os.access(transname,0): | |
if os.access(transname,0222): | |
file = open(transname,'r') | |
else: | |
file = open(os.path.join(HTERRROOT,'403.html'),'r') | |
else: | |
file = open(os.path.join(HTERRROOT,'404.html'),'r') | |
conn.send(file.read()) | |
file.close() | |
else: | |
HTTP501 = open(os.path.join(HTERRROOT,'501.html'),'r') | |
conn.send(HTTP501.read()) | |
HTTP501.close | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment