# A simple script for displaying the response from an HTTP server. import cgi,httplib,urlparse,string,sys # utilities def bottom(): print "
" print "
HTTPTest.py
" print "" print "" def error(msg): print "

ERROR: %s

" % msg bottom() sys.exit(1) def get(form,key,alt=None): if form.has_key(key): return form[key].value else: return alt # print top of page try: print "Content-type: text/html\n" print \ """ HTTPTest results

HTTPTest results

""" sys.stdout.flush() # interpret request form=cgi.FieldStorage() form.__class__.get=get url = form.get("URL") server = form.get("SERVER") port = form.get("PORT") request = form.get("REQ") reqtype = form.get("REQTYPE") # check parameters (scheme,netloc,path,parameters,query,fragment) = urlparse.urlparse(url) req_uri = urlparse.urlunparse(("","",path,parameters,query,fragment)) if scheme!="http": error("You must specify an 'http://'-URL.") elif reqtype!="GET" and reqtype!="HEAD": error("You must specify a GET or HEAD request.") # print connection info print \ """
URL: %s
Method: %s
""" % (url,reqtype) print "

Response

" sys.stdout.flush() # send request hostname=string.split(netloc,":")[0] serv=httplib.HTTP(netloc) serv.putrequest(reqtype, req_uri) serv.putheader("Host",hostname) serv.endheaders() (code,msg,headers)=serv.getreply() print "
"
    sys.stdout.flush()
    print "%s %s HTTP/1.0" % (code,msg)
    sys.stdout.flush()
    for item in headers.items():
        print "%s: %s" % item
        sys.stdout.flush()
    print
    sys.stdout.flush()
    print string.replace(string.replace(serv.getfile().read(), "&", "&"), "<", "<")
    sys.stdout.flush()
    print "
" sys.stdout.flush() # finish off page bottom() sys.stdout.flush() except: sys.stderr=sys.stdout print "
"
    raise