-
- #/usr/bin/python3
- #
- #
-
- # Original copied from https://thepacketgeek.com/give-exabgp-an-http-api-with-flask/,
- # liberally modified after.
-
- import cgi
- import http.server
- import socketserver
- from sys import stdout,stdin
- from syslog import syslog,openlog,LOG_DEBUG
-
- PORT = 9977
-
- class ServerHandler(http.server.SimpleHTTPRequestHandler):
-
- def createResponse(self, command):
- """ Send command string back as confirmation """
- self.send_response(200)
- self.send_header('Content-Type', 'application/text')
- self.end_headers()
- self.wfile.write(command.encode())
-
-
- def do_POST(self):
- """ Process command from POST and output to STDOUT """
- form = cgi.FieldStorage(
- fp=self.rfile,
- headers=self.headers,
- environ={'REQUEST_METHOD':'POST'})
- command = form.getvalue('command')
- syslog(LOG_DEBUG,'Received: <%s>' % command)
-
- response = self.handle_data(command)
- self.createResponse('Success: %s' % response)
-
- def handle_data(self,command):
- stdout.write('%s\n' % command)
- stdout.flush()
- syslog(LOG_DEBUG,'Waiting for response')
- response = ''
- for line in stdin:
- syslog(LOG_DEBUG,'Response: <%s>' % line.rstrip())
- response += 'Received line: <%s>' % line
- syslog(LOG_DEBUG,'End of response.')
- return response
-
-
- handler = ServerHandler
- httpd = socketserver.TCPServer(('', PORT), handler)
- stdout.write('serving at port %s\n' % PORT)
- stdout.flush()
- httpd.serve_forever()
-