Merge pull request #254 from Mikrocontroller/develop

httpRequest-Plugin
This commit is contained in:
Florian 2017-04-12 23:28:39 +02:00 committed by GitHub
commit 1d62fe34f8
2 changed files with 26 additions and 18 deletions

View file

@ -169,6 +169,8 @@ tableSIG = bos_signal
[httpRequest]
# example URL http://example.com/remote.php?DESCR=%DESCR%
# multiple URLs can be separated by comma
# you can use the following wildcards in your URL as GET params:
# http://en.wikipedia.org/wiki/Query_string

View file

@ -14,6 +14,7 @@ httpRequest-Plugin to dispatch FMS-, ZVEI- and POCSAG - messages to an URL
#
# Imports
#
import urllib
import urllib2
import logging # Global logger
from includes import globalVars # Global variables
@ -67,36 +68,41 @@ def run(typ,freq,data):
try:
#
# Create URL
# Replace special characters in data Strings for URL
#
for key in data:
if isinstance(data[key], basestring):
data[key] = urllib.quote(data[key])
#
# Get URLs
#
if typ == "FMS":
url = globalVars.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
urls = globalVars.config.get("httpRequest", "fms_url").split(",")
elif typ == "ZVEI":
url = globalVars.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
urls = globalVars.config.get("httpRequest", "zvei_url").split(",")
elif typ == "POC":
url = globalVars.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
urls = globalVars.config.get("httpRequest", "poc_url").split(",")
else:
logging.warning("Invalid Typ: %s", typ)
return
#
# replace wildcards
#
for (i, url) in enumerate(urls):
urls[i] = wildcardHandler.replaceWildcards(urls[i].strip(), data)
#
# HTTP-Request
#
logging.debug("send %s HTTP request", typ)
logging.debug("send %s HTTP requests", typ)
try:
#resp = urllib2.urlopen(url)
urllib2.urlopen(url)
except urllib2.HTTPError as e:
logging.warning("HTTP response: %s", e.code)
except urllib2.URLError as e:
logging.warning("HTTP-specific error: %s", e.args)
for url in urls:
try:
urllib2.urlopen(url)
except urllib2.HTTPError as e:
logging.warning("HTTP response: %s", e.code)
except urllib2.URLError as e:
logging.warning("HTTP-specific error: %s", e.args)
except:
logging.error("cannot send HTTP request")