Return to Snippet

Revision: 47639
at June 12, 2011 11:05 by Affix


Initial Code
#!/usr/bin/python
#           _     __  __ _        __  __ _____
#          / \   / _|/ _(_)_  __ |  \/  | ____|
#         / _ \ | |_| |_| \ \/ / | |\/| |  _|  
#        / ___ \|  _|  _| |>  < _| |  | | |___
#       /_/   \_\_| |_| |_/_/\_(_)_|  |_|_____|
#
#       Title           : Simple Python HTTP Server
#       Author          : Affix <[email protected]>
#       Website         : http://Affix.ME
#       License         : GNU/GPL V3
#       Description     : Serves simple HTML Pages
#	stored inside a folder called htdocs in the
#	same directory as this script.
#
#	DO NOT EXPECT UPDATES I WAS BORED
 
#################################################
####    DO NOT EDIT BELOW THIS LINE          ####
#################################################

import socket, os, re

serverSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

print "+ Server Launched\n"
serverSock.bind(('', 8000))
serverSock.listen(1)
while True:
	chan, details = serverSock.accept()
	data = chan.recv(100)
	print data

	re1='(GET)'
	re2='.*?'
	re3='((?:\\/[\\w\\.\\-]+)+)'
	re4='.*?'
	re5='((?:[a-z][a-z]+))'

	rg = re.compile(re1+re2+re3+re4+re5,re.IGNORECASE|re.DOTALL)
	m = rg.search(data)
	if m:
	    word1=m.group(1)
	    unixpath1=m.group(2)
	    word2=m.group(3)
	    print "("+word1+")"+"("+unixpath1+")"+"("+word2+")"+"\n"
	    if unixpath1 == "/1.1":
		unixpath1 = "/index.html"	

	print "+ Attempting to serve "+ unixpath1 +"\n"
	if os.path.exists("htdocs" + unixpath1):
		file = open("htdocs" + unixpath1, "r")
		chan.send(file.read())
	else:
		print "+ File was not found!\n"
		chan.send("<html><head><title>404</title></head><body><h1>404 File not Found</h1><br />The file you requested was not found on the server<hr /><small>Affix' Simple Python HTTP Server 0.0.1</small></body></html>")
	chan.close()

Initial URL
http://affix.me

Initial Description
A very simple HTTP Server written in Python out of boredom.

USAGE :
   1 . Create a htdocs folder in the directory of this script 
   2 . create an index.html file and you are good to go. 

This works with multiple pages however index.html is the default

Initial Title
Simple Python HTTP Server

Initial Tags
http

Initial Language
Python