snoop.py :  » Web-Server » Snakelets » Snakelets-1.50 » webapps » test » tsnakelets » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Server » Snakelets 
Snakelets » Snakelets 1.50 » webapps » test » tsnakelets » snoop.py
import cgi,os,sys,time
import types

from snakeserver.snakelet import Snakelet
from snakeserver.webform import FormUploadedFile,FormFileUploadError


class Snoop(Snakelet):

    # no special init method...

    def getDescription(self):
        return "request snooper"
    def requiresSession(self):
        return self.SESSION_DONTCREATE

    def serve(self, request, response):
        # don't set this, just take the default...: request.setEncoding("UTF-8")
        try:
            request.setMaxPOSTsize(self.getWebApp().getConfigItem("maxPOSTsize"))
            form = request.getForm()
        except FormFileUploadError,x:
            response.sendError(413,"upload too large")
            return
            
        out = response.getOutput()
        print >>out, '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
        print >>out, '<html><head><title>Request Snoop</title>'
        print >>out, '<style type="text/css">'
        print >>out, 'body { font-size: 10pt; font-family: sans-serif; color: black; background-color: #fffff8;}'
        print >>out, 'table { border: none;  border-collapse: collapse; }'
        print >>out, 'td,th { text-align: left; vertical-align: top; border: solid #e0d8d0 1px; margin: 0px; padding: 2pt; }'
        print >>out, 'th { border-bottom: solid #808080 1px; }'
        print >>out, 'span.listsep { color: #00a0a0; padding-left: 1ex; padding-right: 1ex; }'
        print >>out, '</style>'
        print >>out, '</head><body>'
        print >>out, '<h1>Request Snoop Snakelet</h1>'
        print >>out, '<p><a href="#form">Form params</a><span class="listsep">|</span>'
        print >>out, '<a href="#req">Request info</a><span class="listsep">|</span>'
        print >>out, '<a href="#headers">HTTP headers</a>'
        self.print_form(form,out)
        print >>out, '<h3><a name="req">Request parameters</a></h3>'
        print >>out, '<table>'
        print >>out, '<tr><th>Item</th><th>Value</th></tr>'
        print >>out, '<tr><td>Method</td><td>', self.escape(request.getMethod()),'</td></tr>'
        print >>out, '<tr><td>Full request URL</td><td>',self.escape(request.getRequestURL()),'</td></tr>'
        print >>out, '<tr><td>Plain request URL</td><td>',request.getRequestURLplain(),'</td></tr>'
        print >>out, '<tr><td>Full URL of the page</td><td>',self.getFullURL(),'</td></tr>'
        print >>out, '<tr><td>Full Query args</td><td>', self.escape(request.getFullQueryArgs()),'</td></tr>'
        print >>out, '<tr><td>Arg</td><td>', self.escape(request.getArg()),'</td></tr>'
        print >>out, '<tr><td>PathInfo</td><td>', self.escape(request.getPathInfo()),'</td></tr>'
        print >>out, '<tr><td>Query</td><td>', self.escape(request.getQuery()),'</td></tr>'
        print >>out, '<tr><td>urlpattern</td><td>',self.getURL(),'</td></tr>'
        print >>out, '<tr><td>Remote host</td><td>', self.escape(request.getRemoteHost()),'</td></tr>'
        print >>out, '<tr><td>Remote addr</td><td>', self.escape(request.getRemoteAddr()),'</td></tr>'
        print >>out, '<tr><td>Real Remote addr</td><td>', self.escape(request.getRealRemoteAddr()),'</td></tr>'
        print >>out, '<tr><td>Auth</td><td>', self.escape(request.getAuth()),'</td></tr>'
        print >>out, '<tr><td>Vhost</td><td>', self.getWebApp().getVirtualHost(),'</td></tr>'
        print >>out, '<tr><td>Server base URL</td><td>', request.getBaseURL(),'</td></tr>'
        session=request.getSession()
        if session:
            print >>out,"<tr><td>Session ID</td><td>",session.getID()+"</td></tr>"
            print >>out,"<tr><td>Session New?</td><td>",session.isNew(),"</td></tr>"
        else:
            print >>out,"<tr><td>Session</td><td>not present</td></tr>"
        print >>out, '</table>'

        print >>out, '<h3><a name="headers">All HTTP request headers</a></h3>'
        print >>out, '<table>'
        print >>out, '<tr><th>Header</th><th>Value</th></tr>'
        
        k=request.getAllHeaders().keys()
        k.sort()
        for h in k:
            hv=request.getHeader(h)
            print >>out,"<tr><td>",h,"</td><td>",self.escape(hv),"</td></tr>"
        print >>out, "</table>"
        print >>out,'</body></html>\n'

    def print_form(self, form,outs):
        """Dump the contents of a form as HTML."""
        keys = form.keys()
        keys.sort()
        print >>outs, '<h3><a name="form">Form Contents (form-POST or GET params)</a></h3>'
        if not keys:
            print >>outs, "<p><em>No form fields are present.</em>"
        else:
            print >>outs, '<table>'
            print >>outs, '<tr><th>Form field</th><th>Type</th><th>Value</th></tr>'
            for key in keys:
                print >>outs, "<tr><td>" + self.escape(key) + "</td><td>",
                value = form[key]
                if type(value) in types.StringTypes:
                    print >>outs,"str</td><td>",self.escape(value)
                elif type(value)==type([]):
                    print >>outs,"list</td><td>",'<span class="listsep"> | </span> '.join( [ self.escape(v) for v in value] )
                elif isinstance(value, FormUploadedFile):
                    print >>outs,"uploaded file</td><td>"
                    print >>outs, "<table>"
                    print >>outs, "<tr><td>filename</td><td>",value.filename,"</td></tr>"
                    print >>outs, "<tr><td>mimeType</td><td>",value.mimeType,"</td></tr>"
                    print >>outs, "<tr><td>typeOptions</td><td>",value.typeOptions,"</td></tr>"
                    print >>outs, "<tr><td>disposition</td><td>",value.disposition,"</td></tr>"
                    print >>outs, "<tr><td>disposition_options</td><td>",value.dispositionOptions,"</td></tr>"
                    contents=value.file.read(2000)
                    print >>outs,"<tr><td>content size</td><td>",len(contents)
                    if value.file.read(1):
                        truncated=True
                        print >>outs,"(truncated to 2000!!)"
                    else:
                        truncated=False
                    print >>outs, "</td></tr>"
                    if truncated:
                        print >>outs, "<tr><td>partial content</td><td>",
                    else:
                        print >>outs, "<tr><td>content</td><td>",
                    if value.mimeType.startswith("text/"):
                        print >>outs,"<pre>"+self.escape(contents)
                        if truncated:
                            print >>outs," &hellip;"
                        print >>outs,"</pre></td></tr>"
                    else:
                        print >>outs,"<em>not shown (binary data)</em></td></tr>"
                    print >>outs,"</table>"
                print >>outs,"</td></tr>"
            print >>outs, "</table>"

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.