From 0df55885d9b0e8228db1ddb759fdd1f72559a833 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Plogmann Date: Sat, 1 Oct 2016 19:51:20 +0200 Subject: [PATCH 1/4] changed httprequest plugin --- config/config.template.ini | 8 ++--- plugins/httpRequest/httpRequest.py | 51 ++++++++++-------------------- 2 files changed, 21 insertions(+), 38 deletions(-) diff --git a/config/config.template.ini b/config/config.template.ini index 8b64326..6866ce9 100644 --- a/config/config.template.ini +++ b/config/config.template.ini @@ -154,7 +154,7 @@ signal_ric = ## SIGNAL-RICS ## [httpRequest] -# URL without http:// +# example URL http://example.com/remote.php?DESCR=%DESCR% # you can use the following wildcards in your URL as GET params: # http://en.wikipedia.org/wiki/Query_string @@ -167,14 +167,14 @@ signal_ric = ## SIGNAL-RICS ## # %DESCR% = Description from csv-file # %TIME% = Time (by script) # %DATE% = Date (by script) -#fms_url = www.google.de?code=%FMS%&stat=%STATUS% +#fms_url = http://www.google.de?code=%FMS%&stat=%STATUS% fms_url = # %ZVEI% = ZVEI 5-tone Code # %DESCR% = Description from csv-file # %TIME% = Time (by script) # %DATE% = Date (by script) -#zvei_url = www.google.de?zvei=%ZVEI% +#zvei_url = http://www.google.de?zvei=%ZVEI% zvei_url = # %RIC% = Pocsag RIC @@ -185,7 +185,7 @@ zvei_url = # %DESCR% = Description from csv-file # %TIME% = Time (by script) # %DATE% = Date (by script) -#poc_url = www.google.de?ric=%RIC%&subric=%FUNC%&msg=%MSG% +#poc_url = http://www.google.de?ric=%RIC%&subric=%FUNC%&msg=%MSG% poc_url = diff --git a/plugins/httpRequest/httpRequest.py b/plugins/httpRequest/httpRequest.py index a795fd9..15c74d5 100644 --- a/plugins/httpRequest/httpRequest.py +++ b/plugins/httpRequest/httpRequest.py @@ -1,21 +1,29 @@ #!/usr/bin/python -# -*- coding: UTF-8 -*- +# -*- coding: cp1252 -*- """ httpRequest-Plugin to dispatch FMS-, ZVEI- and POCSAG - messages to an URL @author: Bastian Schroll +@author: TheJockel + @requires: httpRequest-Configuration has to be set in the config.ini """ +# +# Imports +# +#import httplib #for the HTTP request +import urllib2 +import urllib import time import logging # Global logger -import httplib #for the HTTP request -from urlparse import urlparse #for split the URL into url and path - +import string from includes import globals # Global variables +# Helper function, uncomment to use +#from includes.helper import timeHandler from includes.helper import wildcardHandler from includes.helper import configHandler @@ -68,13 +76,15 @@ def run(typ,freq,data): if typ == "FMS": url = globals.config.get("httpRequest", "fms_url") #Get URL url = wildcardHandler.replaceWildcards(url, data) # replace wildcards with helper function + url = url.replace(" ","%20") # replace space with %20 to be a vaild http request elif typ == "ZVEI": url = globals.config.get("httpRequest", "zvei_url") #Get URL url = wildcardHandler.replaceWildcards(url, data) # replace wildcards with helper function + url = url.replace(" ","%20") # replace space with %20 to be a vaild http request elif typ == "POC": url = globals.config.get("httpRequest", "poc_url") #Get URL url = wildcardHandler.replaceWildcards(url, data) # replace wildcards with helper function - + url = url.replace(" ","%20") # replace space with %20 to be a vaild http request else: logging.warning("Invalid Typ: %s", typ) return @@ -84,37 +94,10 @@ def run(typ,freq,data): # HTTP-Request # logging.debug("send %s HTTP request", typ) - url = urlparse(url) #split URL into path and querry - httprequest = httplib.HTTPConnection(url[2]) #connect to URL Path - httprequest.request("GET", url[5]) #send URL Querry per GET + urllib2.urlopen(url) + except: logging.error("cannot send HTTP request") logging.debug("cannot send HTTP request", exc_info=True) return - - else: - try: - # - # check HTTP-Response - # - httpresponse = httprequest.getresponse() - if str(httpresponse.status) == "200": #Check HTTP Response an print a Log or Error - logging.debug("HTTP response: %s - %s" , str(httpresponse.status), str(httpresponse.reason)) - else: - logging.warning("HTTP response: %s - %s" , str(httpresponse.status), str(httpresponse.reason)) - except: #otherwise - logging.error("cannot get HTTP response") - logging.debug("cannot get HTTP response", exc_info=True) - return - - finally: - logging.debug("close HTTP-Connection") - try: - httprequest.close() - except: - pass - - except: - logging.error("unknown error") - logging.debug("unknown error", exc_info=True) From 87238e60cc958578947f9459f4ef9e6c095dc0b7 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Plogmann Date: Sat, 1 Oct 2016 20:35:44 +0200 Subject: [PATCH 2/4] added missing except for httpRequest --- plugins/httpRequest/httpRequest.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/plugins/httpRequest/httpRequest.py b/plugins/httpRequest/httpRequest.py index 15c74d5..910ff81 100644 --- a/plugins/httpRequest/httpRequest.py +++ b/plugins/httpRequest/httpRequest.py @@ -101,3 +101,7 @@ def run(typ,freq,data): logging.error("cannot send HTTP request") logging.debug("cannot send HTTP request", exc_info=True) return + except: + logging.error("cannot send HTTP request") + logging.debug("cannot send HTTP request", exc_info=True) + return From 6da22e331ae61014f85824592024411903683439 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Plogmann Date: Sat, 1 Oct 2016 20:37:38 +0200 Subject: [PATCH 3/4] added missing except for httpRequest --- plugins/httpRequest/httpRequest.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/plugins/httpRequest/httpRequest.py b/plugins/httpRequest/httpRequest.py index 910ff81..c502c3c 100644 --- a/plugins/httpRequest/httpRequest.py +++ b/plugins/httpRequest/httpRequest.py @@ -102,6 +102,5 @@ def run(typ,freq,data): logging.debug("cannot send HTTP request", exc_info=True) return except: - logging.error("cannot send HTTP request") - logging.debug("cannot send HTTP request", exc_info=True) - return + logging.error("unknown error") + logging.debug("unknown error", exc_info=True) From 06c5846709ccadf9ef8fb4f933e5f96b95596f29 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Plogmann Date: Mon, 3 Oct 2016 12:50:20 +0200 Subject: [PATCH 4/4] added error logging for http plugin --- plugins/httpRequest/httpRequest.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/plugins/httpRequest/httpRequest.py b/plugins/httpRequest/httpRequest.py index c502c3c..bed2820 100644 --- a/plugins/httpRequest/httpRequest.py +++ b/plugins/httpRequest/httpRequest.py @@ -94,7 +94,13 @@ def run(typ,freq,data): # HTTP-Request # logging.debug("send %s HTTP request", typ) - urllib2.urlopen(url) + + try: + resp = urllib2.urlopen(url) + except urllib2.HTTPError as e: + logging.error("HTTP response: %s", e.code) + except urllib2.URLError as e: + logging.error("HTTP-specific error: %s", e.args) except: