#Original file Copyright Jon Berg , turtlemeat.com
import string,cgi,time,sys, os, commands
from os import curdir, sep
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import subprocess
#import pri
local_files = [ 'iryb.css', 'jquery.js', 'smile.png', 'stickmen.png', 'reading.png' ]
content_types = {
'css': 'text/css',
'js': 'text/javascript',
'png': 'image/png'
}
class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
try:
file = self.path.lstrip('/')
if not file.count('/') and local_files.count(file) > 0:
u1, u2, extn = file.rpartition('.')
f = open(curdir + sep + self.path, 'rb') #self.path has /test.html
contents = f.read()
self.send_response(200)
self.send_header('Content-type', content_types[extn])
self.send_header('Content-length', len(contents))
self.end_headers()
self.wfile.write(contents)
f.close()
return
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(subprocess.Popen(["python", "index.py", self.path], stdout=subprocess.PIPE).communicate()[0])
return
except IOError:
self.send_error(404,'File Not Found: %s' % self.path)
except:
self.send_error(404, 'Server error: %s at %s' % (str(sys.exc_info()[1]), str(sys.exc_info()[2])))
def do_POST(self):
global rootnode
try:
ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
if ctype == 'multipart/form-data':
query=cgi.parse_multipart(self.rfile, pdict)
self.send_response(301)
self.end_headers()
upfilecontent = query.get('upfile')
print "filecontent", upfilecontent[0]
self.wfile.write("POST OK.
");
self.wfile.write(upfilecontent[0]);
except :
pass
def main():
try:
# different port for me, so I don't conflict with other
# users on the same machine.
port = 4080
if os.getenv('USERNAME') == "andy":
port = 4090
server = HTTPServer(('', port), MyHandler)
print 'started iryb test httpserver on http://localhost:%d/ ...' % port
print 'Press Control-C in this window to exit'
server.serve_forever()
except KeyboardInterrupt:
print '^C received, shutting down server'
server.socket.close()
if __name__ == '__main__':
main()