#! /usr/local/bin/python
# Store contents of dir() before we've mucked with it:
dir_list = dir()
dir_contents = {}
for m in dir_list:
dir_contents[m] = repr(locals()[m])
import os, sys, types
import string
import HTMLgen
import Ns
import ns_python # for ns_tclvar
def literal(t):
return HTMLgen.TT(HTMLgen.Text(t))
def dict_contents(d, name):
retval = HTMLgen.Container()
retval.append(HTMLgen.Heading(2, 'Contents of %s:' % (name,)))
t = HTMLgen.TableLite(cellpadding=0,)
ms = d.keys()
ms.sort()
for m in ms:
t.append(HTMLgen.TR(
HTMLgen.TD(HTMLgen.Text(m), valign='top'),
HTMLgen.TD(':', valign='top'),
HTMLgen.TD(literal(repr(d[m]))),
))
retval.append(t)
return retval
conn = Ns.GetConn()
doc = HTMLgen.SimpleDocument(title='Sample PyWX document')
evars = [
'SERVER_SOFTWARE',
'SERVER_NAME',
'GATEWAY_INTERFACE',
'SERVER_PROTOCOL',
'SERVER_PORT',
'REQUEST_METHOD',
'HTTP_ACCEPT',
'PATH_INFO',
'PATH_TRANSLATED',
'SCRIPT_NAME',
'QUERY_STRING',
'REMOTE_HOST',
'REMOTE_ADDR',
'REMOTE_USER',
'AUTH_TYPE',
'CONTENT_TYPE',
'CONTENT_LENGTH',
]
# For some reason, the following line doesn't break the connection to
# os.environ, leading to environment variables being cleared below.
#allevars = os.environ.copy()
# So instead, copy explicitly:
allevars = {}
for (k,v) in os.environ.items():
allevars[k] = v
doc.append(HTMLgen.Heading(1, 'Sample PyWX document'))
doc.append(HTMLgen.Heading(2, 'Connection information:'))
t = HTMLgen.TableLite(cellpadding=0,)
connection_info = [
('Server name:', '"%s"', conn.Server(),),
('Driver name:', '"%s"', conn.DriverName(),),
('Authorized user (if any):', '"%s"', conn.AuthUser(),),
('Authorized password (if any):', '"%s"', conn.AuthPasswd(),),
('Content length:', '%d', conn.ContentLength(),),
('Peer:', '"%s"', conn.Peer(),),
('Peer port:', '%d', conn.PeerPort(),),
('Location:', '"%s"', conn.Location(),),
('HTTP version:', '%.1f', conn.request.version,),
('Request line:', '"%s"', conn.request.line,),
('Request method:', '"%s"', conn.request.method,),
('Request protocol:', '"%s"', conn.request.protocol,),
('Request host:', '"%s"', conn.request.host,),
('Request port:', '%d', conn.request.port,),
('Request URL:', '"%s"', conn.request.url,),
('Request query:', '"%s"', conn.request.query,),
]
for (label, format, value,) in connection_info:
if value is None:
value = HTMLgen.Font('None', color='red')
else:
value = HTMLgen.Text(literal(format % (value,)))
t.append(HTMLgen.TR(
HTMLgen.TD(label),
HTMLgen.TD(value),
))
if conn.request.query:
t2 = HTMLgen.TableLite(cellpadding=0, border=2)
t2.append(HTMLgen.TR(
HTMLgen.TH('Key'),
HTMLgen.TH('Value'),
))
for (key,value,) in conn.GetQuery().items():
t2.append(HTMLgen.TR(
HTMLgen.TD(key),
HTMLgen.TD(value),
))
else:
t2 = HTMLgen.Font('N/A', color='red')
t.append(HTMLgen.TR(
HTMLgen.TD('Parsed request query:'),
HTMLgen.TD(t2, valign='center'),
))
doc.append(t)
doc.append(HTMLgen.Heading(2, 'Headers:'))
t = HTMLgen.TableLite(cellpadding=0, border=2)
t.append(HTMLgen.TR(
HTMLgen.TH('Header'),
HTMLgen.TH('Value'),
))
for (header,value,) in conn.Headers().items():
t.append(HTMLgen.TR(
HTMLgen.TD(literal(header)),
HTMLgen.TD(literal('"%s"' % value)),
))
doc.append(t)
doc.append(HTMLgen.Heading(2, 'Standard environment variables set:'))
t = HTMLgen.TableLite(cellpadding=0,)
for evar in evars:
if allevars.has_key(evar):
t.append(HTMLgen.TR(
HTMLgen.TD(HTMLgen.Text(evar), valign='top'),
HTMLgen.TD('=', valign='top'),
HTMLgen.TD(literal('"' + allevars[evar] + '"')),
))
del allevars[evar]
else:
t.append(HTMLgen.TR(
HTMLgen.TD(HTMLgen.Text(evar)),
HTMLgen.TD(HTMLgen.Font('NOT SET', color='red'), colspan=2),
))
doc.append(t)
doc.append(HTMLgen.Heading(2, 'Other environment variables set:'))
t = HTMLgen.TableLite(cellpadding=0,)
evars = allevars.keys()
evars.sort()
for evar in evars:
t.append(HTMLgen.TR(
HTMLgen.TD(HTMLgen.Text(evar), valign='top'),
HTMLgen.TD('=', valign='top'),
HTMLgen.TD(literal('"' + allevars[evar] + '"')),
))
doc.append(t)
doc.append(HTMLgen.Heading(2, 'pid of process:'))
doc.append(
HTMLgen.UL(
['pid = %s' % (os.getpid(),)],
),
)
doc.append(HTMLgen.Heading(2, 'sys.path:'))
t = HTMLgen.OL()
for p in sys.path:
t.append('"%s"' % (p,))
doc.append(t)
doc.append(HTMLgen.Heading(2, 'sys.argv:'))
if hasattr(sys, 'argv'):
t = HTMLgen.OL()
for p in sys.argv:
t.append('"%s"' % (p,))
doc.append(t)
else:
doc.append(HTMLgen.Font('NOT SET', color='red'))
doc.append(HTMLgen.Heading(2, 'Available builtin modules:'))
t = HTMLgen.TableLite(cellpadding=0,)
ms = list(sys.builtin_module_names)
ms.sort()
for m in ms:
t.append(HTMLgen.TR(
HTMLgen.TD(HTMLgen.Text(m)),
))
doc.append(t)
doc.append(HTMLgen.Heading(2, 'Loaded modules:'))
t = HTMLgen.TableLite(cellpadding=0,)
ms = sys.modules.keys()
ms.sort()
for m in ms:
t.append(HTMLgen.TR(
HTMLgen.TD(HTMLgen.Text(m), valign='top'),
HTMLgen.TD(':', valign='top'),
HTMLgen.TD(literal(repr(sys.modules[m]))),
))
doc.append(t)
# __builtins__ can apparently be sometimes a dictionary, sometimes a module.
if type(__builtins__) is types.ModuleType:
doc.append(dict_contents(__builtins__.__dict__,
'__builtins__(module)'))
elif type(__builtins__) is types.DictType:
doc.append(dict_contents(__builtins__,
'__builtins__(dict)'))
doc.append(dict_contents(__persistdict__, '__persistdict__'))
modules_to_show = [
'Nsc',
'Ns',
'ns_python',
'ns_setup',
'pywx',
'ns_codecache',
'PyWX_buffer',
]
for m in modules_to_show:
if sys.modules.has_key(m):
doc.append(dict_contents(sys.modules[m].__dict__, m))
doc.append(HTMLgen.Heading(2, 'Contents of dir() at start of script:'))
t = HTMLgen.TableLite(cellpadding=0,)
ms = dir_contents.keys()
ms.sort()
for m in ms:
t.append(HTMLgen.TR(
HTMLgen.TD(HTMLgen.Text(m), valign='top'),
HTMLgen.TD(':', valign='top'),
HTMLgen.TD(literal(dir_contents[m])),
))
doc.append(t)
doc.append(HTMLgen.Heading(2, 'Defined tcl variables:'))
t = HTMLgen.OL()
ms = conn.TclEval('info vars')
ms = string.split(ms)
ms.sort()
for m in ms:
v = ns_python.ns_tclvar(m)
if v is None:
t.append(HTMLgen.Container(
HTMLgen.Text('%s = ' % (m,)),
HTMLgen.Font('None', color='red'),
))
else:
t.append(HTMLgen.Text('%s = "%s"' % (m, v,)))
doc.append(t)
doc.append(HTMLgen.Heading(2, 'Defined tcl commands and procedures:'))
t = HTMLgen.TableLite(cellpadding=0,)
t.append(HTMLgen.TR(
HTMLgen.TH(HTMLgen.RawText(' ')),
HTMLgen.TH('Name', align='LEFT'),
HTMLgen.TH('Command?', align='CENTER'),
))
ms1 = string.split(conn.TclEval('info procs'))
ms1d = {}
for m in ms1:
ms1d[m] = 'Yes'
ms2 = string.split(conn.TclEval('info commands'))
ms2d = {}
for m in ms2:
ms2d[m] = 'Yes'
msd = ms1d.copy()
msd.update(ms2d)
ms = msd.keys()
ms.sort()
for i in range(len(ms)):
m = ms[i]
if ms1d.has_key(m) and not ms2d.has_key(m):
# This doesn't seem to happen. I guess all commands are
# procedures.
cmd = HTMLgen.Font('Command but not procedure!', color='red')
elif ms1d.has_key(m):
cmd = HTMLgen.Font('Yes', color='green')
else:
cmd = HTMLgen.Font('No', color='black')
t.append(HTMLgen.TR(
HTMLgen.TD(`i`),
HTMLgen.TD(HTMLgen.Text(m)),
HTMLgen.TD(cmd, align='center'),
))
doc.append(t)
conn.ReturnHtml(200, str(doc))
|