1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- ## Author: Alois Mahdal at zxcvb 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)
-
- 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, "tag": tag, "i": i}
- pq = "%s?%s" % (self.parsed_url.path, self._zipup_params(params))
- self.conn.request("GET", pq)
- r = self.conn.getresponse()
- assert r.status == 200, ("logging server returned error %s,"
- "message not logged")
- return r.read()
-
- 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)
|