#!/usr/bin/python -u
# 18-08-04
# v1.1.1
# http.py
# A simple CGI script, to explore http headers, cookies etc.
# Copyright Michael Foord
# Free to use, modify and relicense.
# No warranty express or implied for the accuracy, fitness to purpose or otherwise for this code....
# Use at your own risk !!!
# E-mail or michael AT foord DOT me DOT uk
# Maintained at www.voidspace.org.uk/atlantibots/pythonutils.html
"""
This CGI script allows you to specify a URL using an HTML form.
It will fetch the specified URL and print the headers from the server.
It will also handle cookies using ClientCookie - if it's available.
It is based on approx.py the CGI-proxy I'm building.
It includes authentication circuitry and I'm using it to understand http authentication.
This script shows using urllib2 to fetch a URL with a request object including User-Agent header and basic authentication.
It also shows the possible http errors - using a dictionary 'borrowed' from BaseHTTPServer
"""
################################################################
# Imports
try:
import cgitb; cgitb.enable()
except:
pass
import os, sys, cgi, pickle
from time import strftime
import urllib2
sys.stderr = sys.stdout
READSIZE = 4000
COOKIEFILE = 'cookies.lwp'
try:
import ClientCookie
openfun = ClientCookie.urlopen
reqfun = ClientCookie.Request
cj = ClientCookie.LWPCookieJar()
if os.path.isfile(COOKIEFILE):
cj.load(COOKIEFILE)
opener = ClientCookie.build_opener(ClientCookie.HTTPCookieProcessor(cj))
ClientCookie.install_opener(opener)
except:
ClientCookie = None
openfun = urllib2.urlopen
reqfun = urllib2.Request
###############################################################
# Nicked from BaseHTTPServer
# This is the basic table of HTTP errors
errorlist = { 400: ('Bad Request',
'The Server thinks your request was malformed.'),
401: ('Unauthorized',
'No permission -- see authorization schemes'),
402: ('Payment required',
'No payment -- see charging schemes'),
403: ('Forbidden',
'Request forbidden -- authorization will not help'),
404: ('Not Found', 'Nothing matches the given URI'),
405: ('Method Not Allowed',
'Specified method is invalid for this server.'),
406: ('Not Acceptable', 'URI not available in preferred format.'),
407: ('Proxy Authentication Required', 'You must authenticate with '
'this proxy before proceeding.'),
408: ('Request Time-out', 'Request timed out; try again later.'),
409: ('Conflict', 'Request conflict.'),
410: ('Gone',
'URI no longer exists and has been permanently removed.'),
411: ('Length Required', 'Client must specify Content-Length.'),
412: ('Precondition Failed', 'Precondition in headers is false.'),
413: ('Request Entity Too Large', 'Entity is too large.'),
414: ('Request-URI Too Long', 'URI is too long.'),
415: ('Unsupported Media Type', 'Entity body in unsupported format.'),
416: ('Requested Range Not Satisfiable',
'Cannot satisfy request range.'),
417: ('Expectation Failed',
'Expect condition could not be satisfied.'),
500: ('Internal error', 'Server got itself in trouble'),
501: ('Not Implemented',
'Server does not support this operation'),
502: ('Bad Gateway', 'Invalid responses from another server/proxy.'),
503: ('Service temporarily overloaded',
'The server cannot process the request due to a high load'),
504: ('Gateway timeout',
'The gateway server did not receive a timely response'),
505: ('HTTP Version not supported', 'Cannot fulfill request.')
}
################################################################
# Private functions and variables
SCRIPTNAME = os.environ.get('SCRIPT_NAME', '') # the name of the script
versionstring = '1.1.1 18th August, 2004.'
fontline = ''
METHOD = 'GET'
METHOD2 = 'POST'
def getform(valuelist, theform, notpresent=''):
"""This function, given a CGI form, extracts the data from it, based on
valuelist passed in. Any non-present values are set to '' - although this can be changed.
(e.g. to return None so you can test for missing keywords - where '' is a valid answer but to have the field missing isn't.)"""
data = {}
for field in valuelist:
if not theform.has_key(field):
data[field] = notpresent
else:
if type(theform[field]) != type([]):
data[field] = theform[field].value
else:
values = map(lambda x: x.value, theform[field]) # allows for list type values
data[field] = values
return data
errormess = "An Error Has Occurred
"
theformhead = """
Welcome to http.py -
By Fuzzyman
a Python CGI
"""+fontline +"Version : " + versionstring + """, Running on : """ + strftime('%I:%M %p, %A %d %B, %Y')+'''.
'''
HR = '
'
theform = """This CGI script allows you to specify a URL using the form below.
It will take a look at the specified URL and print the headers from the server.
It will also print the cookies which ought to be managed by the ClientCookie module.
Enter the Location
Voidspace Pythonutils Page
"""
authmess = """