105 lines
3 KiB
Python
105 lines
3 KiB
Python
|
#!/usr/bin/python
|
||
|
|
||
|
import json
|
||
|
import os
|
||
|
import shutil
|
||
|
import SimpleHTTPServer
|
||
|
import socket
|
||
|
import SocketServer
|
||
|
import sys
|
||
|
import urllib2
|
||
|
|
||
|
peers = {}
|
||
|
|
||
|
NETBASE='http://[::1]:8842/'
|
||
|
NAME = 'chat'
|
||
|
|
||
|
def remote(peer, action, data):
|
||
|
url = NETBASE + '%s/%s/%s' % (peer, NAME, action)
|
||
|
if data and not isinstance(data, str):
|
||
|
data = json.dumps(data)
|
||
|
opener = urllib2.build_opener()
|
||
|
req = urllib2.Request(url, data=data, headers={
|
||
|
'Content-Type': 'application/json',
|
||
|
'User-Agent': 'ChatServer/0.0'
|
||
|
})
|
||
|
response = opener.open(req)
|
||
|
return response.read()
|
||
|
|
||
|
|
||
|
def add_service(name, url):
|
||
|
add = NETBASE + 'add'
|
||
|
urllib2.urlopen(add, json.dumps({'name': name, 'url': url}))
|
||
|
|
||
|
class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
||
|
|
||
|
def do_GET(self):
|
||
|
print 'GET', self.path
|
||
|
path = os.path.join('static', self.path[1:] if self.path != '/' else 'index.html')
|
||
|
if os.path.exists(path):
|
||
|
with open(path) as fd:
|
||
|
shutil.copyfileobj(fd, self.wfile)
|
||
|
|
||
|
def _remote_request(self, action, data):
|
||
|
response = {}
|
||
|
if action == 'message':
|
||
|
pass
|
||
|
elif action == 'ping':
|
||
|
print self.headers
|
||
|
response['userid'] = self.headers.getheader('From')
|
||
|
response['remote ping'] = True
|
||
|
response['data'] = data
|
||
|
return response
|
||
|
|
||
|
def _request(self, action, data):
|
||
|
response = {}
|
||
|
if action == 'test':
|
||
|
response['test'] = 'ok'
|
||
|
response['data'] = data
|
||
|
elif action in ('ping', 'pong'):
|
||
|
id = data['id']
|
||
|
del data['id']
|
||
|
response = remote(id, action, data)
|
||
|
return response
|
||
|
|
||
|
def do_POST(self):
|
||
|
print 'POST', self.path
|
||
|
length = int(self.headers.getheader('content-length'))
|
||
|
body = self.rfile.read(length)
|
||
|
data = json.loads(body)
|
||
|
if self.path.startswith('/remote'):
|
||
|
action = self.path.split('/')[2]
|
||
|
response = self._remote_request(action, data)
|
||
|
else:
|
||
|
action = self.path.split('/')[1]
|
||
|
response = self._request(action, data)
|
||
|
|
||
|
response = json.dumps(response, indent=2)
|
||
|
self.send_response(200)
|
||
|
self.send_header('Content-Type', 'application/json')
|
||
|
self.send_header('Content-Length', str(len(response)))
|
||
|
self.end_headers()
|
||
|
self.wfile.write(response)
|
||
|
|
||
|
class Server(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
|
||
|
'''
|
||
|
IPv4/IPv6 Dual Stack
|
||
|
'''
|
||
|
address_family = socket.AF_INET6
|
||
|
allow_reuse_address = True
|
||
|
|
||
|
def server_bind(self):
|
||
|
self.socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, False)
|
||
|
SocketServer.TCPServer.server_bind(self)
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
if len(sys.argv) == 2:
|
||
|
port = int(sys.argv[1])
|
||
|
else:
|
||
|
port = 8000
|
||
|
print "listening on port", port
|
||
|
url = 'http://127.0.0.1:%s/remote/' % port
|
||
|
add_service(NAME, url)
|
||
|
httpd = Server(("", port), Handler)
|
||
|
httpd.serve_forever()
|