How to run a .cgi files on a server ?
I have a major problem when running python scripts along with html form because the browser was showing the source code on the browser window.
How to print output by running html on the server side and not exposing it to the browser on the client side?
Question posted in PhpMyAdmin
The official documentation can be found here.
The official documentation can be found here.
2
Answers
I found the answer to my question: Step 1: Name your python files as
.cgi
instead of.py
..py
files only run inside python terminal or if you could edit the server configuration file to show.py
files extensions as if they are.cgi
files.If you don't have that priviledge you can use.cgi
as your file extension to run python on your server and print the output on the client side.NOTE:You can use your python code on the server side it is not possible for you to run it on client side so always know what you are doing
.cgi gives you that power
Step-2: Linking you.html
code with the python code you can do that by simply giving the link of your.cgi
file into the action attribute of form element. See Below:-This is just an example I have not linked a real
.cgi
file above.Note : To print your output in the same html page just use this code
print('Content-Type:text/html')
print()
print(
AFTER THE ABOVE TWO LINES just copy and paste you html code inside this brackets)
function. Just copy and paste the code above TO SEE HOW IT WORKS.
NOTE THE
"
should be written as"
inside the the brackets or else python will close its print"
befor the whole code is rendered. Also it will produce an error on the client side.Step-3:(Most Important)PROCESSING : This step is where python functions play their role to understand what the user is saying in its input and carry output as you wish. I have written an example below DO NOT RUN THIS CODE HERE IT WILL NOT PRODUCE OUTPUT BECAUSE PYTHON LIBRARIES ARE NOT Installed ON STACKOVERFLOWS EDITOR.[
SKIP THIS CODE BELOW IF YOU ALREADY HAVE XAMPP INSTALLED AND KNOW HOW TO RUN CGI FILES ON IT
]Install https://www.apachefriends.org/index.html XAMPP if you do not have a hosted server. Run Python in xampp for windows below is the explanation how to setup xampp for running
.cgi
files:Running Python scripts with Xampp
The code for
server_action.cgi
which i was talking about before the Installation of XAMPPNote that #!C:Python39python.exe
is use at the beginning of your code. Which is the location of your python interpreter (Although it is not needed if your server provides its own libraries to interpret cgi files)How to hide python errors from displaying on the client side/browser?
In the above snippet i have used a line which will prevent displaying errors on the browser window of your user.
You can turn it off if you think it will be easy for you to see the errors on the browser window instead of opening another directory.
See Below to understand which line am I talking about:-
Try using django which is a popular python web framework. For more info visit https://www.djangoproject.com/