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.

  1. Apache is already installed on Mac OS X, launch Safari with http://localhost/ to verify.
  2. To enable web sharing, select Sharing from the System Preferences.
  3. Instead of public_html, the Sites the directory is where Mac users store their web pages.
  4. 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:
httpd -v

To launch Apache, type sudo /usr/sbin/apachectl graceful in a Terminal window. 

Pointing the browser to localhost (or 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 contains 

Include /private/etc/apache2/users/*.conf

2. In /etc/apache2/httpd.conf uncomment the lines

# They are maybe in different lines 
LoadModule userdir_module libexec/apache2/ 

Include /private/etc/apache2/extra/httpd-userdir.conf

LoadModule cgi_module libexec/apache2/

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.

Require all granted
<Directory "/Users/<user>/Sites/"> 
      Options Indexes MultiViews 
      AllowOverride None 
      Require all granted 

4. Now go to /Library/WebServer/CGI-Executables Python script python

5. Now add this code in file

    Place this in /Library/WebServer/CGI-Executables. 
print("Content-Type: text/plain\n\n") 
print("cgi working fine")

5. Now type URL  http://localhost/cgi-bin/


Popular posts from this blog

How to pass parameters in webhook?

Access and modify all the resources of our Wiki.js using WikiJS API

Fahrenheit to Celsius