- 1). Configure the Apache web server to run CGI scripts by inserting the following lines of code in the main httpd.conf file:
<Directory /usr/local/apache2/htdocs/somedir>
Options +ExecCGI
</Directory>
You will also need to add the following line to the httpd.conf file:
AddHandler cgi-script .cgi .py
This tells the web server that files appended with .cgi and .py must be run as CGI scripts. - 2). Open a text editor and type the following:
#!/usr/bin/python
print('Content-type: text/html\n')print('<TITLE>Python_In_HTML</TITLE>')print('<H1>Python would like to say</H1>')print('<P>Hello, HTML!</P>')
The first line with its "shebang" call invokes the Python interpreter; the print command calls the html method so that the text between the tags will be properly formatted in the browser. Save this file as PyScript1.py in root/Library/Apache/cgi-bin. - 3). Test your script by opening a web browser and typing in the following URL:
http://localhost.cgi-bin.PyScript1.py
Your browser will display the following: Python would like to say Hello, HTML!
SHARE