CGI script

Simple example

1) Webpage calling a CGI-script

python_CGI_test.html

<html>

<head>

<title>Python CGI test</title>

</head>

<body>

<a href="cgi-bin/python_example.cgi">Start CGI script</a>

</body>

</html>

adapt the path of your server CGI directory

2) Simple Python CGI script file

python_example.cgi

#!/usr/bin/python

print('Content-Type: text/html') # HTML is following

print('') # require blank line between CGI header and data

# content

print('<b>Hello World</b>')

a) copy script into the CGI directory on the webserver, the CGI directory can look like:

/var/www/cgi-bin/

b) make sure your script is readable and executable by the Web server, give permissions to execute the CGI script by:

chmod a+x python_example.cgi

c) test your script on the server

./python_example.cgi

Content-Type: text/html

<b>Hello World</b>

d) test pure CGI script in web browser

http://www.example.com/cgi-bin/python_example.cgi

e) finally, copy and test the above webpage which internally calls the CGI script

http://www.example.com/python_CGI_test.html