From 88b342764e7b18f23ca475c772767b757d1e273c Mon Sep 17 00:00:00 2001 From: jngrt Date: Fri, 4 Apr 2014 18:39:32 +0200 Subject: [PATCH] post functionality in serve.py --- serve.py | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) mode change 100644 => 100755 serve.py diff --git a/serve.py b/serve.py old mode 100644 new mode 100755 index d538c59..10fb0de --- a/serve.py +++ b/serve.py @@ -1,8 +1,15 @@ +#!/usr/bin/python + +import logging +import cgi +import os import socket from BaseHTTPServer import HTTPServer -from SimpleHTTPServer import SimpleHTTPRequestHandler +import SimpleHTTPServer +import urlparse + -class MyHandler(SimpleHTTPRequestHandler): +class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): def do_GET(self): if self.path == '/ip': self.send_response(200) @@ -10,8 +17,36 @@ class MyHandler(SimpleHTTPRequestHandler): self.end_headers() self.wfile.write('Your IP address is %s' % self.client_address[0]) return + elif self.path == '/huh': + self.send_response(200) + self.send_header('Content-type', 'text/html') + self.end_headers() + self.wfile.write('huh') + return else: - return SimpleHTTPRequestHandler.do_GET(self) + return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) + + def do_POST(self): + + if self.path == 'send' + + length = int(self.headers['Content-Length']) + post_data = urlparse.parse_qs(self.rfile.read(length).decode('utf-8')) + self.writeMessage( post_data["time"][0], post_data["message"][0]) + + self.send_response(200) + self.send_header('Content-type', 'text/html') + self.end_headers() + self.wfile.write('message created') + + def writeMessage(self, time, message): + + f = os.path.join( "msg", time) + if os.path.isfile( f ): + return + with open( f, 'a') as the_file: + the_file.write(message) + class HTTPServerV6(HTTPServer): address_family = socket.AF_INET6