htlogr.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ## Author: Alois Mahdal at vornet cz
  2. # Front-end for very primitive remote logging service. Use htlog.cgi
  3. # as back-end: put it on a HTTP server and provide URL as "path" option
  4. # when instantiating this class
  5. #
  6. # htlogr.py
  7. #
  8. # Copyright (c) 2013, Alois Mahdal. All rights reserved.
  9. #
  10. # This library is free software; you can redistribute it and/or
  11. # modify it under the terms of the GNU Lesser General Public
  12. # License as published by the Free Software Foundation; either
  13. # version 2.1 of the License, or (at your option) any later version.
  14. #
  15. # This library is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. # Lesser General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU Lesser General Public
  21. # License along with this library; if not, write to the Free Software
  22. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  23. # MA 02110-1301 USA
  24. #
  25. import httplib
  26. import urlparse
  27. import urllib
  28. class Htlogr:
  29. def __init__(self, url):
  30. self.DIV_VALUE = "="
  31. self.DIV_FIELD = ";"
  32. self.url = url
  33. self.parsed_url = urlparse.urlparse(url)
  34. self.conn = httplib.HTTPConnection(self.parsed_url.hostname)
  35. self.last_error = None
  36. def _zipup_params(self, params):
  37. args = []
  38. for name, value in params.iteritems():
  39. if params[name]:
  40. value = str(value)
  41. args.append("%s=%s" % (name, urllib.quote(value)))
  42. return "&".join(args)
  43. def _serialize(self, data):
  44. fields = []
  45. for key in sorted(data.keys()):
  46. value = data[key]
  47. fields.append("%s%s%s" % (key, self.DIV_VALUE, value))
  48. return self.DIV_FIELD.join(fields)
  49. def log(self, msg, tag=None, i=None):
  50. params = {"msg": msg}
  51. try:
  52. params["tag"] = tag()
  53. except TypeError:
  54. params["tag"] = tag
  55. try:
  56. params["i"] = i()
  57. except TypeError:
  58. params["i"] = i
  59. pq = "%s?%s" % (self.parsed_url.path, self._zipup_params(params))
  60. self.conn.request("GET", pq)
  61. self.last_error = None
  62. return_msg = None
  63. try:
  64. r = self.conn.getresponse()
  65. assert r.status == 200, ("logging server returned error %s,"
  66. " message not logged" % r.status)
  67. return_msg = r.read()
  68. except httplib.BadStatusLine as e:
  69. return_msg = ("httplib does not like this line:\n\n %s"
  70. % e.line)
  71. self.last_error = return_msg
  72. return return_msg
  73. def data(self, data, tag=None, i=None):
  74. assert isinstance(data, dict), "data must be dict"
  75. msg = self._serialize(data)
  76. return self.log(msg, tag=tag, i=i)