meshenger/meshenger_clientserve.py

119 lines
3.3 KiB
Python
Raw Normal View History

2014-04-04 18:39:32 +02:00
#!/usr/bin/python
import logging
import cgi
import os
import socket
from BaseHTTPServer import HTTPServer
2014-04-04 18:39:32 +02:00
import SimpleHTTPServer
import urlparse
2014-04-28 22:44:59 +02:00
import unicodedata
import logging, logging.config
logging.config.fileConfig('pylog.conf')
logger = logging.getLogger('meshenger'+'.clientserve')
2014-04-04 18:39:32 +02:00
2014-04-28 22:44:59 +02:00
class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
2014-04-28 23:57:39 +02:00
messageDir = "msg"
def do_GET(self):
2014-11-06 21:52:50 +01:00
"""
Serve index and messages
"""
2014-04-28 23:49:38 +02:00
2014-11-06 21:34:56 +01:00
if self.path == '/index' or self.path.startswith( "/"+self.messageDir ):
return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
2014-11-07 16:21:10 +01:00
elif self.path == '/id':
if meshenger and meshenger.own_hash:
self.send_response(200)
self.send_header('Content-type', 'text-html')
self.end_headers()
self.wfile.write(meshenger.own_hash)
else:
self.send_error(404,'Id not yet available')
elif self.path == '/alias':
if meshenger and meshenger.alias:
self.send_response(200)
self.send_header('Content-type', 'text-html')
self.end_headers()
self.wfile.write(meshenger.alias)
else:
self.send_error(404,'Alias not yet available')
2014-11-06 16:01:46 +01:00
elif self.path == '/log':
self.send_response(200)
self.send_header('Content-type', 'text-html')
self.end_headers()
f = os.path.relpath('log/meshenger.log')
with open( f, 'r') as the_file:
self.wfile.write(the_file.read())
2014-11-07 01:40:19 +01:00
elif self.path.startswith('/web'):
f = os.path.relpath(self.path[1:])
if not os.path.exists( f ) or os.path.isdir( f ):
f = os.path.join('web', 'index.html')
self.path = '/'+f
return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
2014-11-07 16:21:10 +01:00
elif self.path == '/old':
self.send_response(200) #serve the webapp on every url requested
2014-04-04 18:39:32 +02:00
self.send_header('Content-type', 'text/html')
self.end_headers()
2014-11-06 21:34:56 +01:00
f = os.path.relpath( 'webapp.html')
with open( f, 'r') as the_file:
self.wfile.write(the_file.read())
2014-04-04 18:39:32 +02:00
2014-11-07 16:21:10 +01:00
else:
self.path = '/' + os.path.join('web', 'index.html')
return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
2014-04-04 18:39:32 +02:00
def do_POST(self):
2014-11-06 21:52:50 +01:00
"""
Allow clients to post messages
"""
if self.path == '/send':
2014-11-06 21:34:56 +01:00
logger.info('incoming message from client!')
2014-04-04 18:39:32 +02:00
length = int(self.headers['Content-Length'])
post_data = urlparse.parse_qs(self.rfile.read(length).decode('utf-8'))
2014-04-28 22:44:59 +02:00
self.writeMessage( post_data["time"][0].encode('ascii','ignore'), post_data["message"][0])
2014-04-04 18:39:32 +02:00
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write('message created')
#let main rebuild message index
2014-11-06 21:34:56 +01:00
try:
logger.debug('try to call meshenger.build_index: %s', repr(meshenger))
meshenger.build_index()
except:
logger.error('failed to call meshenger.build_index')
pass
2014-04-04 18:39:32 +02:00
def writeMessage(self, time, message):
2014-11-06 21:52:50 +01:00
"""
Write message to disk
"""
2014-04-28 22:44:59 +02:00
f = os.path.join( self.messageDir, time)
2014-04-04 18:39:32 +02:00
if os.path.isfile( f ):
return
with open( f, 'a') as the_file:
the_file.write(message)
2014-04-28 22:44:59 +02:00
class ClientServe():
2014-11-06 20:21:19 +01:00
def __init__(self, port):
2014-11-06 21:34:56 +01:00
logger.info('ClientServe.__init__')
2014-04-28 22:44:59 +02:00
server = HTTPServer( ('', port), ClientServeHandler)
server.serve_forever()
2014-11-06 20:21:19 +01:00
def main():
clientServe = ClientServe(80)
if __name__ == '__main__':
main()