-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpSensor.py
More file actions
65 lines (52 loc) · 1.75 KB
/
HttpSensor.py
File metadata and controls
65 lines (52 loc) · 1.75 KB
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
63
64
import PowerControllerSensor
import urlparse
import httplib
import base64
from lxml import etree
import StringIO
import time
import logging
class HttpSensor(PowerControllerSensor.PowerControllerSensor):
def __init__(self, url, xpath=None, user=None, password=None, interval=2):
PowerControllerSensor.PowerControllerSensor.__init__(self)
self.url = url
self.headers = {}
self.poll_interval = interval
self.xpath = xpath
parsed_url = urlparse.urlparse(url)
hostname = parsed_url.hostname
self.headers['Host'] = parsed_url.netloc
port = parsed_url.port
if port == None: port = httplib.HTTP_PORT
user = user
if user == None: user = parsed_url.username
password = password
if password == None: password = parsed_url.password
if user != None and password != None:
self.headers['Authorization'] = "Basic " + base64.b64encode(user + ":" + password)
self.url_used = url[url.find(parsed_url.netloc) + len(parsed_url.netloc):]
self.http_connection = httplib.HTTPConnection(hostname, port)
self.send_query()
def send_query(self):
self.http_connection.request("GET", self.url_used, headers=self.headers)
response = self.http_connection.getresponse()
content = response.read()
logging.debug("HTTP response content: %s", content)
if (self.xpath != None):
parser = etree.HTMLParser()
tree = etree.fromstring(content,parser)
logging.debug("normalized tree: %s", etree.tostring(tree))
r = tree.xpath(self.xpath)
logging.debug("xpath result: %s", r)
#logging.debug("xpath result content: %s", r[0].text)
exit
def get_state(self):
return set()
def get_selectable_fds(self):
return []
def get_poll_interval(self):
return self.poll_interval
def do_poll(self):
self.send_query()
def do_read(self):
pass