1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- ## 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
-
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
-
- # This program 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 General Public License for more details.
-
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
-
- 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, value in data.iteritems():
- 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)
|