Python CGI
Installing
Installation:
sudo apt-get install apache2 sudo apt-get install python3
Enable CGI processing in apache:
sudo a2enmod cgi
Configure Apache2:
sudo nano /etc/apache2/conf-enabled/host-cgi.conf
ScriptAlias /cgi-bin/ /home/test/scripts/techsupp_analyzer/ <Directory /home/test/scripts/techsupp_analyzer> AllowOverride None Allow from all Require all granted Options +ExecCGI AddHandler cgi-script .py </Directory>
Test Script
sudo nano /home/test/scripts/techsupp_analyzer/cgi.py
#!/usr/bin/python3
# -*- coding: UTF-8 -*-# enable debugging
import cgitb
cgitb.enable()
print("Content-Type: text/html;charset=utf-8")
print()
print("Hello World!")
Change Permissions:
sudo chown -R test:www-data cgi-bin/ sudo chmod +x cgi-bin/cgi.bin
Testing:
http://10.140.196.7/cgi-bin/cgi.py
Get inputs from URL
Create a test CGI script in cgi-bin dir:
nano test.py
#!/usr/bin/python
# Import modules for CGI handling
import cgi, cgitb
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
first_name = form.getvalue('first_name')
last_name = form.getvalue('last_name')
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Hello - Second CGI Program</title>"
print "</head>"
print "<body>"
print "<h2>Hello %s %s</h2>" % (first_name, last_name)
print "</body>"
print "</html>"
Make it executable:
sudo chmod +x test.py
Testing:
http://10.140.196.7/cgi-bin/test.py?first_name=My&last_name=Admin
Get Inputs from Form
File Structure:
--|---index.py | |---test.py
In the same dir as above test.py, create Form Page:
nano index.py
#!/usr/bin/python
# Import modules for CGI handling
import cgi, cgitb
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Hello - Second CGI Program</title>"
print "</head>"
print "<body>"
print'<form action = "/cgi-bin/test.py" method = "get">'
print'First Name: <input type = "text" name = "first_name"> <br />'
print'Last Name: <input type = "text" name = "last_name" />'
print'<input type = "submit" value = "Submit" />'
print"</form>"
print "</body>"
print "</html>"
Make it executable:
sudo chmod +x index.py
Testing
http://10.140.196.7/cgi-bin/index.py
Javascripts
#!/usr/bin/python3
import os
import json
import re
import cgi, cgitb
#import sys
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
path = form.getvalue('path')
dirlist = []
for root,dirc,files in os.walk(path):
for filename in files:
dirlist.append(os.path.join(os.path.realpath(root),filename))
#Avi Config Loader
for i in dirlist:
if i.split('/')[-1] == "avi_config":
f = open(i,'r')
config = json.load(f)
f.close
def convert(obj):
if isinstance(obj, bool):
return str(obj).lower()
if isinstance(obj, (list, tuple)):
return [convert(item) for item in obj]
if isinstance(obj, dict):
return {convert(key):convert(value) for key, value in obj.items()}
return obj
print("Content-Type: text/html;charset=utf-8\r\n\r\n")
print ("""
<!DOCTYPE html>
<html lang="en">
<title>Tech Support Analyzer Tool</title>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://www.w3schools.com/lib/w3-theme-black.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
html,body,h1,h2,h3,h4,h5,h6 {font-family: "Roboto", sans-serif;}
.w3-sidebar {
z-index: 3;
width: 250px;
top: 43px;
bottom: 0;
height: inherit;
}
</style>
<script src="/json-browse/jquery-3.4.1.slim.js"></script>
<script src="/json-browse/jquery.json-browse.js"></script>
<link href="/json-browse/jquery.json-browse.css"rel="stylesheet">
<script type="text/javascript">
window.onload=function(){
$('#json-renderer').jsonBrowse("""+str(convert(config["Tenant"]))+""",{
collapsed: true
});
}
</script>
</head>
<body>
<!-- Navbar -->
<div class="w3-top">
<div class="w3-bar w3-theme w3-top w3-left-align w3-large">
<a class="w3-bar-item w3-button w3-right w3-hide-large w3-hover-white w3-large w3-theme-l1" href="javascript:void(0)" onclick="w3_open()"><i class="fa fa-bars"></i></a>
<a href="#" class="w3-bar-item w3-button w3-theme-l1"><img src="https://avinetworks.com/client/logo.png"></a>
</div>
</div>
<!-- Overlay effect when opening sidebar on small screens -->
<div class="w3-overlay w3-hide-large" onclick="w3_close()" style="cursor:pointer" title="close side menu" id="myOverlay"></div>
<!-- Main content: shift it to the right by 250 pixels when the sidebar is visible -->
<div class="w3-main" style="margin-left:250px">
<div class="w3-row w3-padding-64">
<div class="w3-twothird w3-container">
<h1 class="w3-text-teal">Tech Support Analyzer</h1>
<p>
""")
try:
if config:
#print("Avi Config found! Processing.. ")
print('<pre id="json-renderer" class="json-body"></pre>')
else:
print("<h3>\nAvi Config not found\n</h3>")
except:
print("<h3>\nAVI Config not found\n</h3>")
#except Exception as e:
#print(e)
print ("""
</p>
</div>
<!-- END MAIN -->
</div>
</body>
</html>
""")
- References
{{#widget:DISQUS
|id=networkm
|uniqid=Python CGI
|url=https://aman.awiki.org/wiki/Python_CGI
}}