User:Cristinac/CGI: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "<div style="width:1000px;"> <pre> SelfPostingForms (look up on wiki) look up Simple chat.cgi and How am I being served? and How this wiki was set up? on wiki CGI script, Pira...")
 
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
<div style="width:1000px;">
<div style="width:1000px;">
16.06.2015
<source lang="python">
#!/usr/bin/python
import sys, cgi
import cgitb; cgitb.enable()
#explanation of the script
print “command line arguments”, sys.argv[1]
args=cgi.FieldStorage()
lineno=int(args.getvalue("line","1"))
#lineno=int(sys.argv[1])
f=open(“pg18757.txt”)
lines=f.readlines()
#print len(lines)
print "Content-type:text/html;charset=utf-8"
print
print lines[lineno-1]
</source>
to run from terminal:
python read_textfile.py 1003
to run on a server:
make cgi-bin folder, move python file in new folder, rename python file to .cgi
run: chmod +x cgi-bin/read_textfile.py
run: python -m CGIHTTPServer
go to localhost:8000
making the CGI script responsive:
<source lang="python">
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys, cgi
import cgitb; cgitb.enable()
#explanation of the script
#print “command line arguments”, sys.argv[1]
args=cgi.FieldStorage()
lineno=int(args.getvalue("line", "1"))
word=args.getvalue("word","")
#lineno=int(sys.argv[1])
f=open("text.txt")
lines=f.readlines()
#print len(lines)
print "Content-type:text/html;charset=utf-8"
print
if lineno>len(lines):
print "Line number is too big, there are"+str(len(lines))
sys.exit()
if word:
for line in lines:
if word in line:
print line
print "<br></br>"
sys.exit()
print '<a href="?line='+str(lineno-1)+'">&hellip;</a>'
print lines[lineno-1]
print '<a href="?line='+str(lineno+1)+'">&hellip;</a>'
</source>
^called CGI with response
previous CGI script:
<source lang="python">
#!/usr/bin/env python
import datetime
import cgitb; cgitb.enable()
import cgi
i=cgi.FieldStorage()
year=i.getvalue("year", "")
month=i.getvalue("month", "")
day=i.getvalue("day", "")
print "Content-type:text/html"
print
print "<h1>Hello</h1>"
print '''
<link rel="stylesheet" href="/birthday.css">
<style>
input, form {color: purple}
</style>
<form action="">
'''
print '''
What year were you born? <input type="text" name="year" value="{0}" autofocus></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)
#when using the script on the server, we cannot have while
# while True:
# try:
# year=int(raw_input("What year were you born?"))
# month=int(raw_input("What month were you born?"))
# day=int(raw_input("What day were you born?"))
# print type(year), type(month), type(day)
try:
d=datetime.datetime(int(year), int(month), int(day))
print d.strftime("You were born on a %A")
# break
except ValueError:
# pass
print "Only numbers please"
# if you want to import text from a source and reply from there:
# open("movie.srt")
# for word in message.spli():
# print "You saud{0}</br>".format(word)
</source>
<pre>
<pre>
SelfPostingForms (look up on wiki)
SelfPostingForms (look up on wiki)
look up Simple chat.cgi and How am I being served?  and How this wiki was set up? on wiki
look up Simple chat.cgi and How am I being served?  and How this wiki was set up? on wiki
CGI script, Pirate Box  
CGI script, Pirate Box  
what does it mean to share a git on ssh


CGI script
CGI script
Line 19: Line 160:
datetime=both name of module and name of class
datetime=both name of module and name of class


create a date time object


1972,11,10 -M’s birthday :)




Line 58: Line 197:


raw_input vs input
raw_input vs input


exceptions are used so that the program doesn’t stop
exceptions are used so that the program doesn’t stop
Line 97: Line 234:
if you are using CSS sheets, you need to put them outside the cgi-bin
if you are using CSS sheets, you need to put them outside the cgi-bin


static python library GEOip
 
http://d3js.org/
http://d3js.org/



Latest revision as of 19:13, 16 June 2015

16.06.2015

#!/usr/bin/python
import sys, cgi
import cgitb; cgitb.enable()

#explanation of the script
print command line arguments, sys.argv[1]
args=cgi.FieldStorage()
lineno=int(args.getvalue("line","1"))

#lineno=int(sys.argv[1])
f=open(pg18757.txt)
lines=f.readlines()

#print len(lines)

print "Content-type:text/html;charset=utf-8"
print
print lines[lineno-1]


to run from terminal: 
python read_textfile.py 1003
to run on a server: 
make cgi-bin folder, move python file in new folder, rename python file to .cgi
run: chmod +x cgi-bin/read_textfile.py
run: python -m CGIHTTPServer
go to localhost:8000


making the CGI script responsive:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys, cgi
import cgitb; cgitb.enable()

 
#explanation of the script
#print “command line arguments”, sys.argv[1]
args=cgi.FieldStorage()
lineno=int(args.getvalue("line", "1"))
word=args.getvalue("word","")
 
#lineno=int(sys.argv[1])
f=open("text.txt")
lines=f.readlines()
 
#print len(lines)
 
print "Content-type:text/html;charset=utf-8"
print

if lineno>len(lines):
	print "Line number is too big, there are"+str(len(lines))
	sys.exit()

if word:
	for line in lines:
		if word in line:
			print line
			print "<br></br>"
			sys.exit()

print '<a href="?line='+str(lineno-1)+'">&hellip;</a>'
print lines[lineno-1]
print '<a href="?line='+str(lineno+1)+'">&hellip;</a>'


^called CGI with response


previous CGI script:

#!/usr/bin/env python
import datetime
import cgitb; cgitb.enable()
import cgi


i=cgi.FieldStorage()
year=i.getvalue("year", "")
month=i.getvalue("month", "")
day=i.getvalue("day", "")


print "Content-type:text/html"
print
print "<h1>Hello</h1>"
print '''
<link rel="stylesheet" href="/birthday.css">
<style>
input, form {color: purple}
</style>


<form action="">
'''
print '''
What year were you born? <input type="text" name="year" value="{0}" autofocus></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)
#when using the script on the server, we cannot have while
# while True:
	# try:
	# 	year=int(raw_input("What year were you born?"))
	# 	month=int(raw_input("What month were you born?"))
	# 	day=int(raw_input("What day were you born?"))


# print type(year), type(month), type(day)

try:
	d=datetime.datetime(int(year), int(month), int(day))
	print d.strftime("You were born on a %A")
	# break

except ValueError:
		# pass
		print "Only numbers please"



# if you want to import text from a source and reply from there:
# open("movie.srt")

# for word in message.spli():
# 	print "You saud{0}</br>".format(word)


SelfPostingForms (look up on wiki)
look up Simple chat.cgi and How am I being served?  and How this wiki was set up? on wiki
CGI script, Pirate Box 

CGI script
Web 2.0 way of talking about interactive web

for example: Javascript plugin in browser app
giving a URL to your python script


https://pzwiki.wdka.nl/mediadesign/LetterWalk.cgi

http://www.greenteapress.com/thinkpython/swampy/

datetime=both name of module and name of class





d=datetime.datetime(1991,11,9)
d.day()
type(d.day)
d.weekday()

put things into a variable if you know it will take some time to calculate

strftime

d.strftime("i was bored on a %A”)
d.strftime("i was bored on a %A, the %d %b”)



d=datetime.now()

n=datetime.datetime.now()
n.strftime("%Y%M%D”)

example of calculations: n+(n-d)

difference between method and module (any .py file, can also be a folder, can also be a module)


class=create fusions that have a little bit of data, and functions
datetime is a bunch of structure


datetime.timedelta



raw_input vs input

exceptions are used so that the program doesn’t stop



CGI


python -m SimpleHTTPServer 
then in browser go to: localhost:8000


to add CGI:
python -m CGIHTTPServer



to use, you need to mkdir cgi-bin in the same folder
then use command:
cp birthday.py cgi-bin/birthday.cgi

in the new code document, add #!usr/bin/env python at the top, so that the script knows it’s python



now make it executable by running: 
chmod +x cgi-bin/birthday.cgi 



if it keeps the values there, it’s called sticky form
stateless http
cookies are a form of sticky data


if you are using CSS sheets, you need to put them outside the cgi-bin


http://d3js.org/


there is only one ssh master that everyone shares