[CGI] Why is HTML not rendered?

Discussion in 'Python' started by Gilles, Aug 17, 2012.

  1. Gilles

    Gilles Guest

    Hello

    I'm learning how to call Python scripts through the different
    solutions available.

    For some reason, this CGI script that I found on Google displays the
    contents of the variable but the HTML surrounding it is displayed
    as-is by the browser instead of being rendered:

    --------------
    #!/usr/bin/env python
    # -*- coding: UTF-8 -*-

    # enable debugging
    import cgitb
    cgitb.enable()

    import cgi
    form = cgi.FieldStorage()

    # get a value from the form
    value = form.getvalue("dummy")

    print "Content-Type: text/plain;charset=utf-8"
    print

    # print a document
    print "<P>You typed: <TT>%s</TT></P>" % (
    cgi.escape(value),
    )
    --------------

    Here's the output:
    --------------
    <P>You typed: <TT>test</TT></P>
    --------------

    Could this be due to the script itself, or some server configuration?

    Thank you.
     
    Gilles, Aug 17, 2012
    #1
    1. Advertising

  2. On 17.08.2012 15:27, Gilles wrote:
    > For some reason, this CGI script that I found on Google displays the
    > contents of the variable but the HTML surrounding it is displayed
    > as-is by the browser instead of being rendered:


    > print "Content-Type: text/plain;charset=utf-8"


    With this line you tell the browser to expect a simple plain text file
    and no html. Change the line to

    print "Content-Type: text/html;charset=utf-8"

    and it should work.
     
    Alexander Blinne, Aug 17, 2012
    #2
    1. Advertising

  3. Gilles

    Dan Sommers Guest

    On 2012-08-17 at 15:27:59 +0200,
    Regarding "[CGI] Why is HTML not rendered?,"
    Gilles <> wrote:

    > For some reason, this CGI script that I found on Google displays the
    > contents of the variable but the HTML surrounding it is displayed
    > as-is by the browser instead of being rendered:


    .... [with all due respect and apologies to another thread on this list!]

    > print "Content-Type: text/plain;charset=utf-8"


    That line tells the browser that the response is plain text.

    Do this instead to have the browser render the HTML:

    print "Content-Type: text/html;charset=utf-8"

    HTH,
    Dan

    --
    Μὴ μοῦ τοὺς κÏκλους τάÏαττε -- ΑÏχιμηδησ
    Do not disturb my circles. -- Archimedes

    Dan Sommers, http://www.tombstonezero.net/dan
     
    Dan Sommers, Aug 17, 2012
    #3
  4. Gilles

    Robert Kern Guest

    On 8/17/12 2:27 PM, Gilles wrote:
    > Hello
    >
    > I'm learning how to call Python scripts through the different
    > solutions available.
    >
    > For some reason, this CGI script that I found on Google displays the
    > contents of the variable but the HTML surrounding it is displayed
    > as-is by the browser instead of being rendered:
    >
    > --------------
    > #!/usr/bin/env python
    > # -*- coding: UTF-8 -*-
    >
    > # enable debugging
    > import cgitb
    > cgitb.enable()
    >
    > import cgi
    > form = cgi.FieldStorage()
    >
    > # get a value from the form
    > value = form.getvalue("dummy")
    >
    > print "Content-Type: text/plain;charset=utf-8"
    > print
    >
    > # print a document
    > print "<P>You typed: <TT>%s</TT></P>" % (
    > cgi.escape(value),
    > )
    > --------------
    >
    > Here's the output:
    > --------------
    > <P>You typed: <TT>test</TT></P>
    > --------------
    >
    > Could this be due to the script itself, or some server configuration?


    By using "Content-Type: text/plain", you told the browser to treat it like plain
    text instead of HTML. Use text/html instead.

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
     
    Robert Kern, Aug 17, 2012
    #4
  5. Gilles

    Gilles Guest

    On Fri, 17 Aug 2012 14:44:37 +0100, Robert Kern
    <> wrote:
    >> For some reason, this CGI script that I found on Google displays the
    >> contents of the variable but the HTML surrounding it is displayed
    >> as-is by the browser instead of being rendered


    Thanks all. I (obviously) combined two scripts but didn't notice that
    I had to change the "Content-Type" line to output HTML.
     
    Gilles, Aug 17, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timo
    Replies:
    4
    Views:
    500
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    977
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,216
    Smokey Grindel
    Dec 2, 2006
  4. Jeff
    Replies:
    1
    Views:
    1,073
    bruce barker
    Apr 20, 2009
  5. Ron Vecchi
    Replies:
    7
    Views:
    160
    Ron Vecchi
    Nov 26, 2003
Loading...

Share This Page