User:Cristinac/CGI: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
Line 1: Line 1:
<div style="width:1000px;">
<div style="width:1000px;">
16.06.2015
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:  
  to run from terminal:  
Line 9: Line 31:
  python -m CGIHTTPServer
  python -m CGIHTTPServer
  then include lineno in the URL
  then include lineno in the URL
making it responsive:


<source lang="python">
<source lang="python">
Line 29: Line 53:
print
print
print lines[lineno-1]
print lines[lineno-1]
 
print '<a href=\"?line='+str(lineno+1)'\">&hellip;</a>'
</source>
</source>




 
^called CGI with response
 





Revision as of 12:05, 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: 
chmod +x cgi-bin/read_textfile.py
python -m CGIHTTPServer
then include lineno in the URL

making it responsive:

#!/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]
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