BOSWatch/plugins/httpRequest/httpRequest.py

101 lines
3 KiB
Python
Raw Normal View History

2015-05-20 19:31:50 +02:00
#!/usr/bin/python
# -*- coding: cp1252 -*-
2015-05-28 08:43:08 +02:00
"""
httpRequest-Plugin to dispatch FMS-, ZVEI- and POCSAG - messages to an URL
@author: Bastian Schroll
@requires: httpRequest-Configuration has to be set in the config.ini
"""
2015-05-20 19:31:50 +02:00
import logging # Global logger
2015-05-22 10:44:39 +02:00
import httplib #for the HTTP request
2015-05-28 08:43:08 +02:00
from urlparse import urlparse #for split the URL into url and path
2015-05-22 10:44:39 +02:00
from includes import globals # Global variables
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 interface.txt)
@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
@return: nothing
@exception: Exception if ConfigParser failed
@exception: Exception if http Request failed
@exception: Exception if http Response failed
"""
2015-05-20 19:31:50 +02:00
try:
2015-05-31 12:40:43 +02:00
#
# ConfigParser
#
2015-05-20 19:31:50 +02:00
logging.debug("reading config file")
try:
2015-05-20 19:41:18 +02:00
for key,val in globals.config.items("httpRequest"):
2015-05-20 19:31:50 +02:00
logging.debug(" - %s = %s", key, val)
except:
logging.exception("cannot read config file")
2015-05-28 08:43:08 +02:00
2015-05-22 10:44:39 +02:00
try:
2015-05-31 12:40:43 +02:00
#
# Create URL
#
2015-05-22 10:44:39 +02:00
logging.debug("send %s HTTP request", typ)
if typ == "FMS":
url = globals.config.get("httpRequest", "fms_url") #Get URL
url = url.replace("%FMS%", data["fms"]).replace("%STATUS%", data["status"]) #replace Wildcards in URL
url = url.replace("%DIR%", data["direction"]).replace("%TSI%", data["tsi"]) #replace Wildcards in URL
2015-05-22 10:44:39 +02:00
elif typ == "ZVEI":
url = globals.config.get("httpRequest", "zvei_url") #Get URL
url = url.replace("%ZVEI%", data["zvei"]) #replace Wildcards in URL
2015-05-22 10:44:39 +02:00
elif typ == "POC":
url = globals.config.get("httpRequest", "poc_url") #Get URL
url = url.replace("%RIC%", data["ric"]).replace("%FUNC%", data["function"]) #replace Wildcards in URL
url = url.replace("%MSG%", data["msg"]).replace("%BITRATE%", data["bitrate"]) #replace Wildcards in URL
2015-05-22 10:44:39 +02:00
else:
logging.warning("Invalid Typ: %s", typ)
2015-05-28 08:43:08 +02:00
2015-05-31 12:40:43 +02:00
#
# HTTP-Request
#
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
2015-05-28 08:43:08 +02:00
2015-05-22 10:44:39 +02:00
except:
2015-05-23 08:40:35 +02:00
logging.exception("cannot send HTTP request")
2015-05-20 19:31:50 +02:00
else:
2015-05-31 12:40:43 +02:00
try:
#
# check HTTP-Response
#
2015-05-28 08:43:08 +02:00
httpresponse = httprequest.getresponse()
2015-05-22 10:44:39 +02:00
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.exception("cannot get HTTP response")
2015-05-22 20:17:57 +02:00
finally:
2015-05-22 22:58:27 +02:00
logging.debug("close HTTP-Connection")
2015-05-22 20:17:57 +02:00
httprequest.close()
2015-05-28 08:43:08 +02:00
2015-05-20 19:31:50 +02:00
except:
logging.exception("unknown error")