#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    9
    Rep Power
    0

    Exclamation putting .py onto an xhtml


    Hi...I've been trying to figure out how to put the program in python onto a webpage.

    The program just basically tells you the current time and when you go to the webpage, you're supposed to see the current time.

    So how do you apply the python program onto a webpage?

    Thanks.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Do you have any code we could look at. basically all you have to do is give you're program any required headers (Conent-Type) and give it a go. Everything that is printed by Python will end up as part of teh Page.

    Note: you will obviously need a server to run the program.

    There are alot of threads on this kind of thing and even an article here on devshed that should tell you everything you need to know!

    Happy huntingm have fun!

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    543
    Rep Power
    24
    Do you have Apache? If so, get http://httpd.apache.org/modules/python-download.cgi

    Install mod python (you shouldn't have any problems). Don't forget to restart Apache, afterwards!

    Use this as a test, to see if mod python is working:

    Code:
    #!/usr/bin/env python
    print 'Content-Type: text/html\n'
    print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">'
    print '<html lang="en">'
    print ' <head>'
    print '  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">'
    print '  <link rel="stylesheet" href="/projects/python/python.css" type="text/css" media="screen" title="Python">'
    print '  <title>Python on the Web</title>'
    print ' </head>'
    print ''
    print ' <body>'
    print '<p>This page is used for testing Python on the Web.</p>'
    print '<p><b>Test One (swapcase):</b></p>'
    import string
    sww = 'Spencer Warren Wilson'
    print string.swapcase(sww)
    print '<p><b>Test Two (len):</b></p>'
    print 'Spencer Warren Wilson = '
    print len(sww)
    print '<br />Spencer = '
    s = 'Spencer'
    print len(s)
    print '<br />Warren = '
    w1 = 'Warren'
    print len(w1)
    print '<br />Wilson = '
    w2 = 'Wilson'
    print len(w2)
    print '<br /><i>spaces</i> = '
    space = '  '
    print len(space)
    print ' </body>'
    print '</html>'
    Put

    Code:
    #!/usr/bin/env python
    print 'Content-Type: text/html\n'
    at the very top of your script. Use the print statement to print the HTML stuff as a string (put quotes around the stuff).

    Tell us how it goes.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    9
    Rep Power
    0
    Originally Posted by MasterChief
    Do you have Apache? If so, get http://httpd.apache.org/modules/python-download.cgi

    Install mod python (you shouldn't have any problems). Don't forget to restart Apache, afterwards!

    Use this as a test, to see if mod python is working:

    Code:
    #!/usr/bin/env python
    print 'Content-Type: text/html\n'
    print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">'
    print '<html lang="en">'
    print ' <head>'
    print '  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">'
    print '  <link rel="stylesheet" href="/projects/python/python.css" type="text/css" media="screen" title="Python">'
    print '  <title>Python on the Web</title>'
    print ' </head>'
    print ''
    print ' <body>'
    print '<p>This page is used for testing Python on the Web.</p>'
    print '<p><b>Test One (swapcase):</b></p>'
    import string
    sww = 'Spencer Warren Wilson'
    print string.swapcase(sww)
    print '<p><b>Test Two (len):</b></p>'
    print 'Spencer Warren Wilson = '
    print len(sww)
    print '<br />Spencer = '
    s = 'Spencer'
    print len(s)
    print '<br />Warren = '
    w1 = 'Warren'
    print len(w1)
    print '<br />Wilson = '
    w2 = 'Wilson'
    print len(w2)
    print '<br /><i>spaces</i> = '
    space = '  '
    print len(space)
    print ' </body>'
    print '</html>'
    Put

    Code:
    #!/usr/bin/env python
    print 'Content-Type: text/html\n'
    at the very top of your script. Use the print statement to print the HTML stuff as a string (put quotes around the stuff).

    Tell us how it goes.
    I tried putting them in quotes but it turned out funny looking...

    this is what it looks like...maybe you can spot the error?

    #!/usr/bin/env python
    print "Content-Type: text/html\n"
    print "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">"
    print "<html xmlns="http://www.w3.org/1999/xhtml">"
    print "<head>"
    print "<title>Current Time</title>"
    print "</head>"
    print "<body>"
    print "<h1>Current Time</h1>"
    print "import time'
    print ""Content-type: text/html""
    print ""Right now, it is ", time.asctime()"
    print "</body>"
    print "</html>"


    I even tried it as ' '
    instead of " "
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    9
    Rep Power
    0
    Originally Posted by netytan
    Do you have any code we could look at. basically all you have to do is give you're program any required headers (Conent-Type) and give it a go. Everything that is printed by Python will end up as part of teh Page.

    Note: you will obviously need a server to run the program.

    There are alot of threads on this kind of thing and even an article here on devshed that should tell you everything you need to know!

    Happy huntingm have fun!

    Mark.

    This is the python source

    import time

    print "Content-type: text/html"

    print "Right now, it is ", time.asctime()

    and this is the html

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Current Time</title>

    </head>
    <body>
    <h1>Current Time</h1>

    </body>
    </html>
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    543
    Rep Power
    24
    Code:
    #!/usr/bin/env python
    print "Content-Type: text/html\n"
    print "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">"
    print "<html xmlns="http://www.w3.org/1999/xhtml">"
    print "<head>"
    print "<title>Current Time</title>"
    print "</head>"
    print "<body>"
    print "<h1>Current Time</h1>"
    print "import time'
    print ""Content-type: text/html""
    print ""Right now, it is ", time.asctime()"
    print "</body>"
    print "</html>"
    Use single quotes (' ') around your strings.
    You don't print the import.

    This code should do what you want it to:
    Code:
    #!/usr/bin/env python
    print 'Content-Type: text/html\n'
    print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
    print '<html xmlns="http://www.w3.org/1999/xhtml">'
    print '<head>'
    print '<title>Current Time</title>'
    print '</head>'
    print '<body>'
    print '<h1>Current Time</h1>'
    import time
    time = time.asctime()
    print 'Right now, it is '
    print time
    print '</body>'
    print '</html>'
    See the script in action on my Apache Web server.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    9
    Rep Power
    0

    Thumbs up


    Originally Posted by MasterChief
    Code:
    #!/usr/bin/env python
    print "Content-Type: text/html\n"
    print "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">"
    print "<html xmlns="http://www.w3.org/1999/xhtml">"
    print "<head>"
    print "<title>Current Time</title>"
    print "</head>"
    print "<body>"
    print "<h1>Current Time</h1>"
    print "import time'
    print ""Content-type: text/html""
    print ""Right now, it is ", time.asctime()"
    print "</body>"
    print "</html>"
    Use single quotes (' ') around your strings.
    You don't print the import.

    This code should do what you want it to:
    Code:
    #!/usr/bin/env python
    print 'Content-Type: text/html\n'
    print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
    print '<html xmlns="http://www.w3.org/1999/xhtml">'
    print '<head>'
    print '<title>Current Time</title>'
    print '</head>'
    print '<body>'
    print '<h1>Current Time</h1>'
    import time
    time = time.asctime()
    print 'Right now, it is '
    print time
    print '</body>'
    print '</html>'
    See the script in action on my Apache Web server.
    ahhhhhhh ty soooooo much!!!!!!!!!!!!!!!!!! At first when I tried out your source, it didn't work till i had a closer look. you saved it has .py and I saved it as .html! You peeps are the greatest!!

    sometimes the obvious is unseen ><

IMN logo majestic logo threadwatch logo seochat tools logo