Running Python in Browser

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chittaranjan
    New Member
    • Sep 2006
    • 51

    Running Python in Browser

    Hi All,

    I am new to Python. I am not able to get the python script run in the browser. I am using IIS as my webserver and my python version is Python-2.4. I am under Windows-XP OS. I have done one my directories to be a virtual directory to run in my web browser. I have written some sample codes as follows

    - form.html
    Code:
    <html><body>
    <form method="get" action="process_form.py">
    Name: <input type="text" name="name">
    <input type="submit" value="Submit">
    </form>
    </body></html>
    This is a simple HTML form from where I am posting the data to a python file. The python code as below:

    - process_form.py
    Code:
    #!c:\Python24\python.exe
    import cgi
    form = cgi.FieldStorage() # instantiate only once!
    name = form.getfirst('name', 'empty')
    
    # Avoid script injection escaping the user input
    name = cgi.escape(name)
    
    print """\
    Content-Type: text/html\n
    <html><body>
    <p>The submitted name was "%s"</p>
    </body></html>
    """ % name
    When I am trying to run in the browser it show me the form but aftre entering the data in the form field and submitting it just shows up the python code in the next page like below:

    #!c:\Python24\p ython.exe import cgi form = cgi.FieldStorag e() # instantiate only once! name = form.getfirst(' name', 'empty') # Avoid script injection escaping the user input name = cgi.escape(name ) print """\ Content-Type: text/html\n
    The submitted name was "%s"

    """ % name


    So please suggest me what I need to do to execute a python code from a browser what is the mistake I have done here?

    Thanks and Regards,
    Chittarajan :)
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Looking at the book, Learning Python, page 386, it looks OK to me. Ensure that the hash-bang (#!) is on the first line of the module.

    Comment

    • Chittaranjan
      New Member
      • Sep 2006
      • 51

      #3
      Thanks Sir,

      I do not have the book with me and I have ordered the book. But if any kind of other help will be highly appreciated.

      Regards,
      Chittaranjan :)

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by Chittaranjan
        Thanks Sir,

        I do not have the book with me and I have ordered the book. But if any kind of other help will be highly appreciated.

        Regards,
        Chittaranjan :)
        Ensure that the hash-bang (#!) is on the very first line of the module.

        Comment

        • Motoma
          Recognized Expert Specialist
          • Jan 2007
          • 3236

          #5
          It sounds like the python file is not set up to be executed by your webserver. Read through Microsoft's documentation on getting Python scripts to run with IIS.

          Comment

          • Chittaranjan
            New Member
            • Sep 2006
            • 51

            #6
            Originally posted by Motoma
            It sounds like the python file is not set up to be executed by your webserver. Read through Microsoft's documentation on getting Python scripts to run with IIS.
            Thanks Motoma,

            This was really helpful. I have tried and able to setup and browse the python files in browser. But can you please let me know some helpful sites for pymssql installation and examples by which I can move forward to link with database in my coding.

            Regards,
            Chittaranjan :)

            Comment

            • Motoma
              Recognized Expert Specialist
              • Jan 2007
              • 3236

              #7
              Originally posted by Chittaranjan
              Thanks Motoma,

              This was really helpful. I have tried and able to setup and browse the python files in browser. But can you please let me know some helpful sites for pymssql installation and examples by which I can move forward to link with database in my coding.

              Regards,
              Chittaranjan :)
              Forgive me if this sounds obtuse, but have you read pymssql's documentation?

              Comment

              • ravikonam
                New Member
                • Jan 2009
                • 2

                #8
                run test.py on the browser

                Chittaranjan,

                Is your problem solved?
                If so, can you please let me know how to run a python file (for eg, test.py) on the browser?
                I tried running the file in Django, like http://localhost:8000/demo/test.py but getting File Not Found (404) error.

                Please help me

                Thanks
                Ravi K

                Comment

                Working...