peerlink/example/link.py
2014-09-01 14:29:36 +02:00

43 lines
1.1 KiB
Python

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division
import json
import urllib2
import os
PEERLINK = os.environ.get('PEERLINK', 'http://[::1]:8842/')
SERVICE = os.environ.get('PEERLINK_SERVICE', '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 SERVICE
SERVICE = name
return post('add', {'name': name, 'url': url})
def remote(peer, action, data):
url = PEERLINK + '%s/%s/%s' % (peer, SERVICE, 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))