Skip to content

Instantly share code, notes, and snippets.

@frennkie
Last active January 30, 2017 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frennkie/d77552a6a6211d7172b94e623ebda46a to your computer and use it in GitHub Desktop.
Save frennkie/d77552a6a6211d7172b94e623ebda46a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Copyright 2017 frennkie (https:/github.com/frennkie)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from __future__ import print_function
import cgi
import requests
JSON_WEBSERVICE_ENDPOINT = 'http://api.example.com/lookup'
BOOTSTRAP_CSS_URL = 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css'
""" # Download and Serve Bootstrap CSS from OTRS local path
sudo mkdir /opt/otrs/var/httpd/htdocs/skins/Agent/default/css/thirdparty/bootstrap
curl https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css | sudo tee /opt/otrs/var/httpd/htdocs/skins/Agent/default/css/thirdparty/bootstrap/bootstrap.min.css
sudo chown otrs:apache /opt/otrs/var/httpd/htdocs/skins/Agent/default/css/thirdparty/bootstrap/bootstrap.min.css
BOOTSTRAP_CSS_URL = 'https://otrs.example.com/otrs-web/skins/Agent/default/css/thirdparty/bootstrap/bootstrap.min.css'
"""
form = cgi.FieldStorage() # instantiate only once!
match_unsafe = form.getfirst('match', None)
# Test Data - static 'match' GET parameter
#match_unsafe = '1und1.de'
# Test Data - static result dict
#sample = {
# "result": [
# {
# "records": [
# "82.165.230.17",
# "82.165.229.138"
# ],
# "server": {
# "comment": "Google88",
# "ip_address": "8.8.8.8"
# }
# },
# {
# "records": [
# "82.165.229.138",
# "82.165.230.17"
# ],
# "server": {
# "comment": "Google44",
# "ip_address": "8.8.4.4"
# }
# }
# ]
#}
# Server Headers
print('Content-Type: text/html;charset=utf-8')
print('X-FRAME-OPTIONS: SAMEORIGIN')
# Empty line to separate Server Headers from Page Content
print()
# Page Content
print('<!DOCTYPE html>')
print('<html lang="en">')
# HTML Headers
print(' <head>')
print(' <meta charset="utf-8">')
print(' <meta http-equiv="X-UA-Compatible" content="IE=edge">')
print(' <meta name="viewport" content="width=device-width, initial-scale=1">')
print(' <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->')
print(' <title>Web API to HTML Output CGI Script</title>')
print(''' <link rel="stylesheet" href="{0}"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">'''.format(BOOTSTRAP_CSS_URL))
print(' </head>')
# Body
print(' <body>')
print(' <div class="container">')
print(' <div class="well">')
if match_unsafe:
# Avoid script injection escaping the user input
match = cgi.escape(match_unsafe)
# Get Data
try:
r = requests.get('{0}/{1}'.format(JSON_WEBSERVICE_ENDPOINT, match), proxies={"http": "", "https": ""}, verify=False)
data = r.json()
# Start Data output
print(' <table id="info-table" class="table table-condensed">')
print(' <th>Results for {0}</th><th>from Server</th>'.format(match))
for row in data["result"]:
print(' <tr>')
print(' <td>')
print(' <table id="info-table" class="table table-condensed table-hover">')
row_data = row.get("records", None)
if row_data:
for record in row_data:
print(' <tr><td><h2>{0}</h2></td></tr>'.format(record))
else:
print(' <tr><td><h2 class="text-warning">Empty Result!</h2></td></tr>')
print(' </table>')
print(' </td>')
print(' <td><h3>{0} ({1})</h3></td>'.format(row["server"]["ip_address"], row["server"]["comment"]))
print(' </tr>')
print(' </table>')
# End Data output
except Exception as err:
print('<h2 class="text-danger">Failed to fetch/parse data! Error: {0}</h2>'.format(err))
else:
print(' <h2 class="text-danger">Error: No "match" parameter provided!</h2>')
print(' </div>')
print(' </div>')
print(' </body>')
print('</html>')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment