Python CGI: Difference between revisions

From Network Security Wiki
Content added Content deleted
Line 43: Line 43:
http://10.140.196.7/cgi-bin/cgi.py
http://10.140.196.7/cgi-bin/cgi.py


= Get Inputs from Form =
= Get inputs form URL =

Create a test CGI script in cgi-bin dir:
nano test.py


nano index.py
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
#!/usr/bin/python
#!/usr/bin/python


# Import modules for CGI handling
# Import modules for CGI handling
import cgi, cgitb
import cgi, cgitb


# Create instance of FieldStorage
# Create instance of FieldStorage
#form = cgi.FieldStorage()
form = cgi.FieldStorage()


# Get data from fields
# Get data from fields
#first_name = form.getvalue('first_name')
first_name = form.getvalue('first_name')
#last_name = form.getvalue('last_name')
last_name = form.getvalue('last_name')


print "Content-type:text/html\r\n\r\n"
print "Content-type:text/html\r\n\r\n"
Line 65: Line 67:
print "</head>"
print "</head>"
print "<body>"
print "<body>"
print "<h2>Hello %s %s</h2>" % (first_name, last_name)
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 "</body>"
print "</html>"
print "</html>"
</syntaxhighlight>
</syntaxhighlight>


Make it executable:
test@myserver:~/scripts/techsupp_analyzer$ cat test.py
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

<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
#!/usr/bin/python
#!/usr/bin/python
Line 81: Line 93:
# Import modules for CGI handling
# Import modules for CGI handling
import cgi, cgitb
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 "Content-type:text/html\r\n\r\n"
Line 95: Line 100:
print "</head>"
print "</head>"
print "<body>"
print "<body>"
print'<form action = "/cgi-bin/test.py" method = "get">'
print "<h2>Hello %s %s</h2>" % (first_name, last_name)
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 "</body>"
print "</html>"
print "</html>"
</syntaxhighlight>
</syntaxhighlight>

Make it executable:
sudo chmod +x index.py

Testing
http://10.140.196.7/cgi-bin/index.py

Revision as of 09:39, 13 December 2018

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/ /var/www/html/cgi-bin/

<Directory /var/www/html/cgi-bin>
        AllowOverride None
        Allow from all
        Require all granted
        Options +ExecCGI
        AddHandler cgi-script .py
</Directory>

Create Script:

sudo nano /var/www/html/cgi-bin/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 form 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