BOSWatch/plugins/httpRequest/httpRequest.py

113 lines
3 KiB
Python
Raw Normal View History

2015-05-20 19:31:50 +02:00
#!/usr/bin/python
2016-10-01 19:51:20 +02:00
# -*- coding: cp1252 -*-
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 httplib #for the HTTP request
import urllib2
import urllib
import time
import logging # Global logger
2016-10-01 19:51:20 +02:00
import string
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:
#
# Create URL
#
if typ == "FMS":
2016-10-03 12:02:18 +02:00
url = globalVars.config.get("httpRequest", "fms_url") #Get URL
url = wildcardHandler.replaceWildcards(url, data) # replace wildcards with helper function
2016-10-01 19:51:20 +02:00
url = url.replace(" ","%20") # replace space with %20 to be a vaild http request
elif typ == "ZVEI":
2016-10-03 12:02:18 +02:00
url = globalVars.config.get("httpRequest", "zvei_url") #Get URL
url = wildcardHandler.replaceWildcards(url, data) # replace wildcards with helper function
2016-10-01 19:51:20 +02:00
url = url.replace(" ","%20") # replace space with %20 to be a vaild http request
elif typ == "POC":
2016-10-03 12:02:18 +02:00
url = globalVars.config.get("httpRequest", "poc_url") #Get URL
url = wildcardHandler.replaceWildcards(url, data) # replace wildcards with helper function
2016-10-01 19:51:20 +02:00
url = url.replace(" ","%20") # replace space with %20 to be a vaild http request
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
#
# HTTP-Request
#
logging.debug("send %s HTTP request", typ)
2016-10-03 12:50:20 +02:00
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)
2016-10-01 19:51:20 +02:00
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)