BOSWatch/plugins/httpRequest/httpRequest.py

116 lines
2.8 KiB
Python
Raw Normal View History

2015-05-20 19:31:50 +02:00
#!/usr/bin/python
2016-10-03 13:38:30 +02:00
# -*- coding: UTF-8 -*-
2015-05-20 19:31:50 +02:00
2015-05-28 08:43:08 +02:00
"""
httpRequest-Plugin to dispatch FMS-, ZVEI- and POCSAG - messages to an URL
@author: Bastian Schroll
2016-10-01 19:51:20 +02:00
@author: TheJockel
2015-05-28 08:43:08 +02:00
@requires: httpRequest-Configuration has to be set in the config.ini
"""
2015-05-20 19:31:50 +02:00
2016-10-01 19:51:20 +02:00
#
# Imports
#
import urllib
2016-10-01 19:51:20 +02:00
import urllib2
import logging # Global logger
from includes import globalVars # Global variables
2016-10-01 19:51:20 +02:00
# Helper function, uncomment to use
#from includes.helper import timeHandler
from includes.helper import wildcardHandler
from includes.helper import configHandler
2015-06-29 12:19:44 +02:00
##
#
# onLoad (init) function of plugin
# will be called one time by the pluginLoader on start
2015-06-29 12:19:44 +02:00
#
def onLoad():
"""
While loading the plugins by pluginLoader.loadPlugins()
this onLoad() routine is called one time for initialize the plugin
2015-06-29 12:19:44 +02:00
@requires: nothing
2015-07-02 09:02:49 +02:00
2015-06-29 12:19:44 +02:00
@return: nothing
"""
# nothing to do for this plugin
return
2015-07-02 09:02:49 +02:00
2015-05-31 12:40:43 +02:00
##
#
# Main function of HTTP-plugin
# will be called by the alarmHandler
#
2015-05-20 19:31:50 +02:00
def run(typ,freq,data):
2015-05-28 08:43:08 +02:00
"""
This function is the implementation of the httpRequest-Plugin.
It will send the data to an URL via http Request
@type typ: string (FMS|ZVEI|POC)
@param typ: Typ of the dataset
@type data: map of data (structure see readme.md in plugin folder)
2015-05-28 08:43:08 +02:00
@param data: Contains the parameter
@type freq: string
@keyword freq: frequency of the SDR Stick
@requires: httpRequest-Configuration has to be set in the config.ini
2015-07-02 09:02:49 +02:00
2015-05-28 08:43:08 +02:00
@return: nothing
"""
2015-05-20 19:31:50 +02:00
try:
if configHandler.checkConfig("httpRequest"): #read and debug the config
2015-07-02 09:02:49 +02:00
2015-05-31 12:40:43 +02:00
try:
#
# Make a copy of the data field to not overwrite the data in it
# Replace special characters in dataCopy Strings for URL
#
dataCopy = dict(data)
for key in dataCopy:
if isinstance(dataCopy[key], basestring):
dataCopy[key] = urllib.quote(dataCopy[key])
#
# Get URLs
#
if typ == "FMS":
urls = globalVars.config.get("httpRequest", "fms_url").split(",")
elif typ == "ZVEI":
urls = globalVars.config.get("httpRequest", "zvei_url").split(",")
elif typ == "POC":
urls = globalVars.config.get("httpRequest", "poc_url").split(",")
2015-05-22 10:44:39 +02:00
else:
2015-07-02 09:02:49 +02:00
logging.warning("Invalid Typ: %s", typ)
2015-07-01 20:33:43 +02:00
return
2015-07-02 09:02:49 +02:00
#
# replace wildcards
#
for (i, url) in enumerate(urls):
urls[i] = wildcardHandler.replaceWildcards(urls[i].strip(), dataCopy)
#
# HTTP-Request
#
logging.debug("send %s HTTP requests", typ)
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)
2015-07-02 09:02:49 +02:00
except:
logging.error("cannot send HTTP request")
logging.debug("cannot send HTTP request", exc_info=True)
return
2015-05-20 19:31:50 +02:00
except:
logging.error("unknown error")
2015-07-02 09:02:49 +02:00
logging.debug("unknown error", exc_info=True)