User:Emily/Prototyping/Trimester 03/04: Difference between revisions
(2 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
:command line -- go to "http://localhost:8000/" | :command line -- go to "http://localhost:8000/" | ||
python -m CGIHTTPServer | python -m CGIHTTPServer | ||
chmod +x filename.cgi (make your .cgi script executable) | |||
<div style='width:90%;border:1px solid #ccc;padding:11px'> | |||
cp python script to CGI script | |||
---- | |||
<source lang="python"> | |||
#!/usr/bin/python | |||
import datetime | |||
import cgitb; cgitb.enable() | |||
import cgi | |||
i = cgi.FieldStorage() | |||
year = (i.getvalue("year", "")) | |||
month = (i.getvalue("month", "")) | |||
day = (i.getvalue("day", "")) | |||
#header announce doc type | |||
print "Content-type:text/html;charset=utf-8" | |||
print | |||
print "<h2>what day is it</h2>" | |||
print ''' | |||
<style> | |||
input, form { color: green } | |||
input, form { font-family: verdana; font-size: 12px } | |||
</style> | |||
''' | |||
print ''' | |||
<form action=""> | |||
what year were you born?<input type="text" name="year" value="{0}"> <br> | |||
what month were you born?<input type="text" name="month" value="{1}"> <br> | |||
what day were you born?<input type="text" name="day" value="{2}"> <br> | |||
<input type="submit"> | |||
</form> | |||
'''.format(year, month, day) #to store/keep the last input there | |||
try: | |||
d = datetime.datetime(int(year), int(month), int(day)) | |||
print d.strftime("You were born on a %A") | |||
except ValueError: | |||
print "Hey please type only numbers!" | |||
</source> | |||
</div> | |||
====birthday.py (datetime module)==== | ====birthday.py (datetime module)==== | ||
Line 21: | Line 65: | ||
d = datetime.datetime(int(year), int(month), int(day)) | d = datetime.datetime(int(year), int(month), int(day)) | ||
print d.strftime("You were born on a %A") | print d.strftime("You were born on a %A") | ||
# | # Locale’s full weekday name --%A | ||
break | break | ||
except ValueError: | except ValueError: |
Latest revision as of 14:10, 21 June 2015
birthday.cgi
- command line -- go to "http://localhost:8000/"
python -m CGIHTTPServer chmod +x filename.cgi (make your .cgi script executable)
cp python script to CGI script
#!/usr/bin/python
import datetime
import cgitb; cgitb.enable()
import cgi
i = cgi.FieldStorage()
year = (i.getvalue("year", ""))
month = (i.getvalue("month", ""))
day = (i.getvalue("day", ""))
#header announce doc type
print "Content-type:text/html;charset=utf-8"
print
print "<h2>what day is it</h2>"
print '''
<style>
input, form { color: green }
input, form { font-family: verdana; font-size: 12px }
</style>
'''
print '''
<form action="">
what year were you born?<input type="text" name="year" value="{0}"> <br>
what month were you born?<input type="text" name="month" value="{1}"> <br>
what day were you born?<input type="text" name="day" value="{2}"> <br>
<input type="submit">
</form>
'''.format(year, month, day) #to store/keep the last input there
try:
d = datetime.datetime(int(year), int(month), int(day))
print d.strftime("You were born on a %A")
except ValueError:
print "Hey please type only numbers!"
birthday.py (datetime module)
Version I
import datetime
while True:
year = input("What year were you born?")
# or we can use --> year = raw_input("What year were you born?")
month = input("What month were you born?")
day = input("What day were you born?")
# print type(year), type(month)
try:
d = datetime.datetime(int(year), int(month), int(day))
print d.strftime("You were born on a %A")
# Locale’s full weekday name --%A
break
except ValueError:
print "Hey please only type only numbers!"
# pass
Version II (better)
import datetime
while True:
try:
year = int(raw_input("What year were you born?"))
# "input" is not working here, use "raw_iput"
month = int(raw_input("What month were you born?"))
day = int(raw_input("What day were you born?"))
# print type(year), type(month)
d = datetime.datetime(year, month, day)
print d.strftime("You were born on a %A")
break
except ValueError:
print "Hey please only type only numbers!"
# pass