-
Notifications
You must be signed in to change notification settings - Fork 16
/
server.py
63 lines (45 loc) · 1.46 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from library import Library
from client import Client
import urllib2
from xml.etree.ElementTree import XML
class Server(object):
def __init__(self, address, port=32400):
# TODO: clean up address, remove http:// etc
# remove slash at end of address
if address[-1] == '/':
address = address[:-1]
self.address = address
self.port = int(port)
def execute(self, path):
if path[0] == '/':
path = path[1:]
# open url
try:
urllib2.urlopen("http://%s:%d/%s" % (self.address, self.port, path))
except urllib2.URLError, e:
print e
def query(self, path):
if path[0] == '/':
path = path[1:]
# open url and get raw xml data
try:
response = urllib2.urlopen("http://%s:%d/%s" % (self.address, self.port, path))
except urllib2.URLError, e:
print e
# create element from xml data
xmldata = response.read()
element = XML(xmldata)
return element
def __str__(self):
return "<Server: %s:%d/>" % (self.address, self.port)
def __repr__(self):
return "<Server: %s:%d/>" % (self.address, self.port)
@property
def library(self):
elem = self.query("/library")
return Library(self)
@property
def clients(self):
elem = self.query("/clients")
clist = [Client(e, self) for e in elem]
return clist