import json import urllib2 PEERLINK='http://[::1]:8842/' NAME = 'chat' def urlopen(url, data=None, headers=None): if data and not isinstance(data, str): data = json.dumps(data) if not headers: headers = { 'Content-Type': 'application/json', 'User-Agent': 'ChatServer/0.0' } opener = urllib2.build_opener() print 'urlopen', url, data req = urllib2.Request(url, data=data, headers=headers) response = opener.open(req) return response.read() def post(action, data=None): url = PEERLINK + action return json.loads(urlopen(url, data)) def add(name, url): global NAME NAME = name return post('add', {'name': name, 'url': url}) def remote(peer, action, data): url = PEERLINK + '%s/%s/%s' % (peer, NAME, action) if not data: data = {'test': True} print 'REMOTE', url return urlopen(url, data) def remote_json(peer, action, data): return json.loads(remote(peer, action, data))