Run a python scripts in web browser using PythonCGI
Hello everyone 😀 today I want to share another interesting topic which is python CGI. CGI stands for the Common Gateway Interface. This interface allows a web browser to pass input to Python scripts and pass the output of Python scripts to a web browser. Building a web interface is similar to building a graphical user interface.
We will run our demonstrations on a Mac OS X computer.
# They are maybe in different lines
Require all granted<Directory "/Users/<user>/Sites/">
We will run our demonstrations on a Mac OS X computer.
- Apache is already installed on Mac OS X, launch Safari with http://localhost/ to verify.
- To enable web sharing, select Sharing from the System Preferences.
- Instead of public_html, the Sites the directory is where Mac users store their web pages.
- Instead of /var/www/cgi-bin, CGI scripts are in /Library/WebServer/CGI-Executables. CGI = Common Gateway Interface is a set of protocols through which applications interact with web servers.
Apache Configuration
To check the version of Apache, we can type the following at the command prompt in a terminal window:
To launch Apache, type sudo /usr/sbin/apachectl graceful in a Terminal window.
httpd -v
Pointing the browser to localhost (or 127.0.0.1) shows It works! if Apache was configured correctly. To check the configuration of Apache, type apachectl configtest at the command prompt.
CGI Configuration
1. To serve web pages from user directories, in the file /etc/apache2/extra/httpd-userdir.conf uncomment the line that containsInclude /private/etc/apache2/users/*.conf
2. In /etc/apache2/httpd.conf uncomment the lines
LoadModule userdir_module libexec/apache2/mod_userdir.so
Include /private/etc/apache2/extra/httpd-userdir.conf
LoadModule cgi_module libexec/apache2/mod_cgi.so
3. Finally, in the folder /etc/apache2/users, if <user> is your user name, in the file <user>.conf, then add the line in the file <user>.conf.
Options Indexes MultiViews
AllowOverride None
Require all granted
</Directory>
4. Now go to /Library/WebServer/CGI-Executables Python script python works.py
5. Now add this code in works.py file
#!/usr/bin/python
"""
Place this in /Library/WebServer/CGI-Executables.
"""
print("Content-Type: text/plain\n\n")
print("cgi working fine")
Comments
Post a Comment
Please give us your valuable feedback