Simple CGI Script in Python: Difference between revisions
(Created page with "The Python documentation is a pretty good place to start: http://docs.python.org/library/cgi.html <source lang="python"> print "Content-Type: text/html" # HTML is following ...") |
mNo edit summary |
||
(11 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
The Python documentation is a pretty good place to start: | The Python documentation is a pretty good place to start: | ||
http://docs.python.org/library/cgi.html | http://docs.python.org/library/cgi.html | ||
== Check List == | |||
# the scripts starts with: | |||
<source lang="python"> | |||
#!/usr/bin/env python | |||
#-*- coding:utf-8 -*- | |||
</source> | |||
# also import and enable cgi traceback | |||
<source lang="python"> | |||
import cgitb; | |||
cgitb.enable() | |||
</source> | |||
# the filename ends with .cgi | |||
# chmod 755 the_file.cgi | |||
# print the headers | |||
<source lang="python"> | |||
print "Content-Type: text/html; charset=utf-8" # HTML is following | |||
print # blank line, end of headers | |||
</source> | |||
# the script is here http://pzwart3.wdka.hro.nl/~yourname/cgi-bin/the_file.cgi | |||
== Hello world == | |||
<source lang="python"> | <source lang="python"> | ||
#!/usr/bin/env python | |||
#-*- coding:utf-8 -*- | |||
print "Content-Type: text/html" # HTML is following | print "Content-Type: text/html" # HTML is following | ||
print # blank line, end of headers | print # blank line, end of headers | ||
print "Hello!" | |||
</source> | |||
== Show Headers == | |||
HTTP headers are simply environment variables. Use the os.environ dictionary to get at them. | |||
<source lang="python"> | |||
#!/usr/bin/env python | |||
#-*- coding:utf-8 -*- | |||
import os | |||
print "Content-type: text/html" | |||
print | |||
# show all headers as an HTML definition list | |||
print "<dl>" | |||
for key in os.environ: | |||
print "<dt>" + key + "</dt>" | |||
print "<dd>" + os.environ[key] + "</dd>" | |||
print "</dl>" | |||
</source> | |||
== "Discriminating" Response based on IP Address == | |||
Interesting headers: | |||
* HTTP_USER_AGENT (string identifying the user's browser, also often includes information about the computer / operating system as well) | |||
* REMOTE_ADDR (the IP address of the user's machine *or* gateway) | |||
* REQUEST_METHOD (either "GET" or "POST") | |||
* QUERY_STRING (the stuff after the '?' in the users's request URL) | |||
* HTTP_ACCEPT_LANGUAGE (language code set by user's browser) | |||
<source lang="python"> | |||
#!/usr/bin/env python | |||
#-*- coding:utf-8 -*- | |||
import os | |||
print "Content-type: text/html" | |||
print | |||
addr = os.environ['REMOTE_ADDR'] | |||
last = addr.split(".")[-1] | |||
agent = os.environ.get("HTTP_USER_AGENT") | |||
if agent == None: | |||
print "Go away, bot!" | |||
if (last == "188") or (last=="18") or (last=="67"): | |||
print '<img src="http://www.kuliniewicz.org/hotlink/hotlink.png" />' | |||
else: | |||
print 'Hello "%s"' % agent | |||
</source> | </source> | ||
[[Category:Cookbook]] |
Latest revision as of 16:53, 12 December 2011
The Python documentation is a pretty good place to start: http://docs.python.org/library/cgi.html
Check List
- the scripts starts with:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
- also import and enable cgi traceback
import cgitb;
cgitb.enable()
- the filename ends with .cgi
- chmod 755 the_file.cgi
- print the headers
print "Content-Type: text/html; charset=utf-8" # HTML is following
print # blank line, end of headers
- the script is here http://pzwart3.wdka.hro.nl/~yourname/cgi-bin/the_file.cgi
Hello world
#!/usr/bin/env python
#-*- coding:utf-8 -*-
print "Content-Type: text/html" # HTML is following
print # blank line, end of headers
print "Hello!"
Show Headers
HTTP headers are simply environment variables. Use the os.environ dictionary to get at them.
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import os
print "Content-type: text/html"
print
# show all headers as an HTML definition list
print "<dl>"
for key in os.environ:
print "<dt>" + key + "</dt>"
print "<dd>" + os.environ[key] + "</dd>"
print "</dl>"
"Discriminating" Response based on IP Address
Interesting headers:
- HTTP_USER_AGENT (string identifying the user's browser, also often includes information about the computer / operating system as well)
- REMOTE_ADDR (the IP address of the user's machine *or* gateway)
- REQUEST_METHOD (either "GET" or "POST")
- QUERY_STRING (the stuff after the '?' in the users's request URL)
- HTTP_ACCEPT_LANGUAGE (language code set by user's browser)
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import os
print "Content-type: text/html"
print
addr = os.environ['REMOTE_ADDR']
last = addr.split(".")[-1]
agent = os.environ.get("HTTP_USER_AGENT")
if agent == None:
print "Go away, bot!"
if (last == "188") or (last=="18") or (last=="67"):
print '<img src="http://www.kuliniewicz.org/hotlink/hotlink.png" />'
else:
print 'Hello "%s"' % agent