24.02.2018

Example web hook receiver for FastNetMon Advanced

This pages provides example code for HTTP web server implemented in Python to test web_hook capability in FastNetMon and return all data received from FastNetMon.

Please put following code to file fastnetmon_webhook_receiver_server.py:

#!/usr/bin/python

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import SocketServer
import random

class S(BaseHTTPRequestHandler):
    def _set_headers(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

    def do_GET(self):
        self._set_headers()
        f = open("index.html", "r")
        self.wfile.write(f.read())

    def do_HEAD(self):
        self._set_headers()

    def do_POST(self):
        self._set_headers()
        print "in post method"
        self.data_string = self.rfile.read(int(self.headers['Content-Length']))

        self.send_response(200)
        self.end_headers()

        print "Got POST data: " + self.data_string
        #data = simplejson.loads(self.data_string)
        #with open("test123456.json", "w") as outfile:
        #    simplejson.dump(data, outfile)
        #print "{}".format(data)
        #f = open("for_presen.py")
        #self.wfile.write(f.read())
        return


def run(server_class=HTTPServer, handler_class=S, port=8080):
    server_address = ('', port)
    httpd = server_class(server_address, handler_class)
    print 'Starting httpd...'
    httpd.serve_forever()

if __name__ == "__main__":
    from sys import argv

if len(argv) == 2:
    run(port=int(argv[1]))
else:
    run()

And run it this way:

python fastnetmon_webhook_receiver_server.py 8080

And enable FastNetMon’s feature:

sudo fcli set main web_callback_enabled enable
sudo fcli set main web_callback_url http://127.0.0.1:8080/attack/notify
sudo fcli commit