You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.6 KiB
72 lines
1.6 KiB
11 years ago
|
#!/usr/bin/python
|
||
|
|
||
|
import logging
|
||
|
import cgi
|
||
|
import os
|
||
11 years ago
|
import socket
|
||
|
from BaseHTTPServer import HTTPServer
|
||
11 years ago
|
import SimpleHTTPServer
|
||
|
import urlparse
|
||
|
|
||
11 years ago
|
|
||
11 years ago
|
class MeshengerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
||
|
|
||
|
messageDir = "msg"
|
||
|
|
||
|
"""
|
||
|
Serve index and messages
|
||
|
"""
|
||
11 years ago
|
def do_GET(self):
|
||
11 years ago
|
if self.path == '/':
|
||
|
self.path = "/index"
|
||
|
|
||
|
if self.path == '/index' or self.path.startswith( self.messageDir ):
|
||
|
return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
|
||
|
|
||
|
else:
|
||
|
self.send_response(404)
|
||
11 years ago
|
self.send_header('Content-type', 'text/html')
|
||
|
self.end_headers()
|
||
11 years ago
|
self.wfile.write('404 - Not Found')
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
"""
|
||
|
Allow clients to post messages
|
||
|
"""
|
||
|
def do_POST(self):
|
||
|
if self.path == '/send':
|
||
11 years ago
|
|
||
|
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):
|
||
|
|
||
11 years ago
|
f = os.path.join( self.message, time)
|
||
11 years ago
|
if os.path.isfile( f ):
|
||
|
return
|
||
|
with open( f, 'a') as the_file:
|
||
|
the_file.write(message)
|
||
|
|
||
11 years ago
|
|
||
|
class HTTPServerV6(HTTPServer):
|
||
|
address_family = socket.AF_INET6
|
||
|
|
||
11 years ago
|
class MeshengerServe():
|
||
|
def __init__(self, port):
|
||
|
server = HTTPServerV6(('::', port), MeshengerHandler)
|
||
|
server.serve_forever()
|
||
|
|
||
|
|
||
11 years ago
|
def main():
|
||
11 years ago
|
clientServe = MeshengerServe(80)
|
||
|
nodeServe = MeshengerServe(13338)
|
||
11 years ago
|
|
||
|
if __name__ == '__main__':
|
||
|
main()
|