123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- ## Author: Alois Mahdal at vornet cz
- # Front-end for very primitive remote logging service. Use htlog.cgi
- # as back-end: put it on a HTTP server and provide URL as "path" option
- # when instantiating this class
- #
- # htlogr.py
- #
- # Copyright (c) 2013, Alois Mahdal. All rights reserved.
- #
- # This library is free software; you can redistribute it and/or
- # modify it under the terms of the GNU Lesser General Public
- # License as published by the Free Software Foundation; either
- # version 2.1 of the License, or (at your option) any later version.
- #
- # This library is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- # Lesser General Public License for more details.
- #
- # You should have received a copy of the GNU Lesser General Public
- # License along with this library; if not, write to the Free Software
- # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- # MA 02110-1301 USA
- #
-
-
- import httplib
- import urlparse
- import urllib
-
-
- class Htlogr:
-
- def __init__(self, url):
- self.DIV_VALUE = "="
- self.DIV_FIELD = ";"
- self.url = url
- self.parsed_url = urlparse.urlparse(url)
- self.conn = httplib.HTTPConnection(self.parsed_url.hostname)
- self.last_error = None
-
- def _zipup_params(self, params):
- args = []
- for name, value in params.iteritems():
- if params[name]:
- value = str(value)
- args.append("%s=%s" % (name, urllib.quote(value)))
- return "&".join(args)
-
- def _serialize(self, data):
- fields = []
- for key in sorted(data.keys()):
- value = data[key]
- fields.append("%s%s%s" % (key, self.DIV_VALUE, value))
- return self.DIV_FIELD.join(fields)
-
- def log(self, msg, tag=None, i=None):
- params = {"msg": msg}
-
- try:
- params["tag"] = tag()
- except TypeError:
- params["tag"] = tag
- try:
- params["i"] = i()
- except TypeError:
- params["i"] = i
-
- pq = "%s?%s" % (self.parsed_url.path, self._zipup_params(params))
- self.conn.request("GET", pq)
- self.last_error = None
- return_msg = None
- try:
- r = self.conn.getresponse()
- assert r.status == 200, ("logging server returned error %s,"
- " message not logged" % r.status)
- return_msg = r.read()
- except httplib.BadStatusLine as e:
- return_msg = ("httplib does not like this line:\n\n %s"
- % e.line)
- self.last_error = return_msg
- return return_msg
-
- def data(self, data, tag=None, i=None):
- assert isinstance(data, dict), "data must be dict"
- msg = self._serialize(data)
- return self.log(msg, tag=tag, i=i)
|