I'm trying to make a simple python webserver to save text that is Post
ed to a file called store.json
which is in the same folder as the python script. Here is half of my code, can someone tell me what the rest should be?
import string,cgi,time
from os import curdir, sep
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
#import pri
class StoreHandler(BaseHTTPRequestHandler):
def do_GET(self):
try:
if self.path == "/store.json":
f = open(curdir + sep + "store.json") #self.path has /test.html
self.send_response(200)
self.send_header('Content-type','text/json')
self.end_headers()
self.wfile.write(f.read())
f.close()
return
return
except IOError:
self.send_error(404,'File Not Found: %s' % self.path)
def do_POST(self):
//if the url is 'store.json' then
//what do I do here?
def main():
try:
server = HTTPServer(('', 80), StoreHandler)
print 'started...'
server.serve_forever()
except KeyboardInterrupt:
print '^C received, shutting down server'
server.socket.close()
if __name__ == '__main__':
main()
Important thing is that you will have to build
cgi.FieldStorage
properly from the raw posted data e.g.after that it is easy to dump file, here is a simple handler which shows a form on
do_GET
to upload any file user chooses and saves that file to /tmp indo_POST
when form is POSTedAlso note that
self.respond
is not a standard method I just added it for quickly returning some response.Here's the general idea: