skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    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:-

    <form action="server_action.cgi">
    <input name="Message" type="input">
    </form>

    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:

      STEP-1:[Download Python]
    
      Download & install the latest version of python from www.python.org Download 
      Python & click on the windows installer of any version [ex. python-3.6.2]
    
      STEP 2: [Install Python] Install in any directory of your harddrive [ex. 
      D:python-3.6.2]
      Click on config button just right to the Start Button and open `httpd.conf` 
      file and make the changes as said in the linked post BELOW  ⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇ 
      [RUN PYTHON SCRIPTS WITH XAMPP][1]
    

    Running Python scripts with Xampp
    The code for server_action.cgi which i was talking about before the Installation of XAMPP Note 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)

    #!C:Python39python.exe
    import cgi
    import cgitb
    import cgitb
    cgitb.enable()
    import cgitb
    cgitb.enable(display=0, logdir="/path/to/logdir")
    print("<TITLE>CGI script output</TITLE>")
    print("<H1>This is my first CGI script</H1>")
    print("Hello, world!")
    print("""<form action="server_action.cgi">
    <input name="Message" type="input">
    </form>""")
    form = cgi.FieldStorage()
    text=form["Message"].value 
    #'Message' is the value of my 'name' attribute used in my <input name=""> tag
    
    Use 
    print("""
    this 
    format
    """) to print multiple lines in one print output. 
    

    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:-
      import cgitb
      cgitb.enable(display=0, logdir="/path/to/logdir")
    
    
      This was used 
      to prevent displaying error on the clients browser if any occured.
      ```/path/to/logdir``` is the path where you want to store the error that was 
      prevented directly on the browser window ```display=0```
    

  2. Try using django which is a popular python web framework. For more info visit https://www.djangoproject.com/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search