post functionality in serve.py

This commit is contained in:
jngrt 2014-04-04 18:39:32 +02:00
parent 611321aefb
commit 88b342764e

41
serve.py Normal file → Executable file
View File

@ -1,8 +1,15 @@
#!/usr/bin/python
import logging
import cgi
import os
import socket import socket
from BaseHTTPServer import HTTPServer from BaseHTTPServer import HTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler import SimpleHTTPServer
import urlparse
class MyHandler(SimpleHTTPRequestHandler):
class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self): def do_GET(self):
if self.path == '/ip': if self.path == '/ip':
self.send_response(200) self.send_response(200)
@ -10,8 +17,36 @@ class MyHandler(SimpleHTTPRequestHandler):
self.end_headers() self.end_headers()
self.wfile.write('Your IP address is %s' % self.client_address[0]) self.wfile.write('Your IP address is %s' % self.client_address[0])
return 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: 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): class HTTPServerV6(HTTPServer):
address_family = socket.AF_INET6 address_family = socket.AF_INET6