changed web server to class, ready for integration in threads in main.py

This commit is contained in:
jngrt 2014-04-04 20:51:52 +02:00
parent a5d61f517d
commit 8f61d977ce
3 changed files with 33 additions and 19 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
interfaceip6adress interfaceip6adress
msg/*

1
index Normal file
View File

@ -0,0 +1 @@
test

View File

@ -9,26 +9,32 @@ import SimpleHTTPServer
import urlparse import urlparse
class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): class MeshengerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
messageDir = "msg"
"""
Serve index and messages
"""
def do_GET(self): def do_GET(self):
if self.path == '/ip': if self.path == '/':
self.send_response(200) self.path = "/index"
self.send_header('Content-type', 'text/html')
self.end_headers() if self.path == '/index' or self.path.startswith( self.messageDir ):
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 SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
def do_POST(self): else:
self.send_response(404)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write('404 - Not Found')
if self.path == 'send'
"""
Allow clients to post messages
"""
def do_POST(self):
if self.path == '/send':
length = int(self.headers['Content-Length']) length = int(self.headers['Content-Length'])
post_data = urlparse.parse_qs(self.rfile.read(length).decode('utf-8')) post_data = urlparse.parse_qs(self.rfile.read(length).decode('utf-8'))
@ -41,7 +47,7 @@ class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def writeMessage(self, time, message): def writeMessage(self, time, message):
f = os.path.join( "msg", time) f = os.path.join( self.message, time)
if os.path.isfile( f ): if os.path.isfile( f ):
return return
with open( f, 'a') as the_file: with open( f, 'a') as the_file:
@ -51,9 +57,15 @@ class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
class HTTPServerV6(HTTPServer): class HTTPServerV6(HTTPServer):
address_family = socket.AF_INET6 address_family = socket.AF_INET6
class MeshengerServe():
def __init__(self, port):
server = HTTPServerV6(('::', port), MeshengerHandler)
server.serve_forever()
def main(): def main():
server = HTTPServerV6(('::', 13338), MyHandler) clientServe = MeshengerServe(80)
server.serve_forever() nodeServe = MeshengerServe(13338)
if __name__ == '__main__': if __name__ == '__main__':
main() main()