|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
Quote:
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 " " |
|
#5
|
|||
|
|||
|
Quote:
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> |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
Quote:
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 >< |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > putting .py onto an xhtml |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|