User:Cristinac/CGI: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
Line 10: Line 10:
print “command line arguments”, sys.argv[1]
print “command line arguments”, sys.argv[1]
args=cgi.FieldStorage()
args=cgi.FieldStorage()
lineno=int(args.getvalue(“line”,"1"))
lineno=int(args.getvalue("line","1"))


#lineno=int(sys.argv[1])
#lineno=int(sys.argv[1])
Line 18: Line 18:
#print len(lines)
#print len(lines)


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


  to run on a server:  
  to run on a server:  
  chmod +x cgi-bin/read_textfile.py
  make cgi-bin folder, move python file in new folder, rename python file to .cgi
  python -m CGIHTTPServer
run: chmod +x cgi-bin/read_textfile.py
  then include lineno in the URL
  run: python -m CGIHTTPServer
  go to localhost:8000


making it responsive:
 
making the CGI script responsive:


<source lang="python">
<source lang="python">
#!/usr/bin/python
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys, cgi
import sys, cgi
import cgitb; cgitb.enable()
import cgitb; cgitb.enable()


#explanation of the script
#explanation of the script
print “command line arguments”, sys.argv[1]
#print “command line arguments”, sys.argv[1]
args=cgi.FieldStorage()
args=cgi.FieldStorage()
lineno=int(args.getvalue(“line”,"1"))
lineno=int(args.getvalue("line", "1"))
 
word=args.getvalue("word","")
#lineno=int(sys.argv[1])
#lineno=int(sys.argv[1])
f=open(“pg18757.txt”)
f=open("text.txt")
lines=f.readlines()
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()


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


print “Content-type:text/html;charset=utf-8”
print '<a href="?line='+str(lineno-1)+'">&hellip;</a>'
print
print lines[lineno-1]
print lines[lineno-1]
print '<a href=\"?line='+str(lineno+1)'\">&hellip;</a>'
print '<a href="?line='+str(lineno+1)+'">&hellip;</a>'
</source>
</source>



Revision as of 12:59, 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


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

create a date time object

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



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