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
|
|
|
|
|
#
|
2017-04-12 23:20:40 +02:00
|
|
|
import urllib
|
2016-10-01 19:51:20 +02:00
|
|
|
import urllib2
|
2015-05-22 16:44:23 +02:00
|
|
|
import logging # Global logger
|
2016-10-03 11:56:27 +02:00
|
|
|
from includes import globalVars # Global variables
|
2015-07-09 07:52:21 +02:00
|
|
|
|
2016-10-01 19:51:20 +02:00
|
|
|
# Helper function, uncomment to use
|
|
|
|
|
#from includes.helper import timeHandler
|
2015-07-07 13:17:18 +02:00
|
|
|
from includes.helper import wildcardHandler
|
2015-07-09 07:52:21 +02:00
|
|
|
from includes.helper import configHandler
|
2015-05-22 16:44:23 +02:00
|
|
|
|
2015-06-29 12:19:44 +02:00
|
|
|
##
|
|
|
|
|
#
|
2015-06-29 23:24:48 +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()
|
2015-06-29 23:24:48 +02:00
|
|
|
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
|
|
|
|
|
"""
|
2015-06-29 23:24:48 +02:00
|
|
|
# nothing to do for this plugin
|
|
|
|
|
return
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-05-22 16:44:23 +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
|
2016-10-02 12:38:47 +02:00
|
|
|
@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:
|
2015-07-09 07:52:21 +02:00
|
|
|
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:
|
|
|
|
|
#
|
2017-12-19 08:35:10 +01:00
|
|
|
# Make a copy of the data field to not overwrite the data in it
|
|
|
|
|
# Replace special characters in dataCopy Strings for URL
|
2017-04-12 23:20:40 +02:00
|
|
|
#
|
2017-12-19 08:35:10 +01:00
|
|
|
dataCopy = data
|
|
|
|
|
for key in dataCopy:
|
|
|
|
|
if isinstance(dataCopy[key], basestring):
|
|
|
|
|
dataCopy[key] = urllib.quote(dataCopy[key])
|
2017-04-12 23:20:40 +02:00
|
|
|
#
|
|
|
|
|
# Get URLs
|
2015-06-29 12:25:21 +02:00
|
|
|
#
|
|
|
|
|
if typ == "FMS":
|
2017-04-12 23:20:40 +02:00
|
|
|
urls = globalVars.config.get("httpRequest", "fms_url").split(",")
|
2015-06-29 12:25:21 +02:00
|
|
|
elif typ == "ZVEI":
|
2017-04-12 23:20:40 +02:00
|
|
|
urls = globalVars.config.get("httpRequest", "zvei_url").split(",")
|
2015-06-29 12:25:21 +02:00
|
|
|
elif typ == "POC":
|
2017-04-12 23:20:40 +02:00
|
|
|
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
|
|
|
|
2015-06-29 12:25:21 +02:00
|
|
|
#
|
2017-04-12 23:20:40 +02:00
|
|
|
# replace wildcards
|
|
|
|
|
#
|
|
|
|
|
for (i, url) in enumerate(urls):
|
2017-12-19 08:35:10 +01:00
|
|
|
urls[i] = wildcardHandler.replaceWildcards(urls[i].strip(), dataCopy)
|
2017-04-12 23:20:40 +02:00
|
|
|
#
|
2015-06-29 12:25:21 +02:00
|
|
|
# HTTP-Request
|
|
|
|
|
#
|
2017-04-12 23:20:40 +02:00
|
|
|
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
|
|
|
|
2015-06-23 22:27:48 +02:00
|
|
|
except:
|
2015-06-29 12:25:21 +02:00
|
|
|
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:
|
2015-06-23 22:27:48 +02:00
|
|
|
logging.error("unknown error")
|
2015-07-02 09:02:49 +02:00
|
|
|
logging.debug("unknown error", exc_info=True)
|