Last active — forked from branneman/example-mod-wsgi.py

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Python (2&3 compatible) Equivalent to phpinfo()

View example-mod-wsgi.py
1 2 3 4 5 6 7 8
def application(environ, start_response):
 
from pyinfo import pyinfo
output = pyinfo()
# or pyinfo([('custom key', 'custom value'), ...])
 
start_response('200 OK', [('Content-type', 'text/html')])
return [output]
View example-mod-wsgi.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
try:
from html import escape
except ImportError:
from cgi import escape
import os
from string import Template
import sys
import platform
import socket
from datetime import datetime
 
 
output_template = Template('''<!DOCTYPE html>
<html>
<head>
<title>pyinfo()</title>
<meta name="robots" content="noindex,nofollow,noarchive,nosnippet">
<style>
body{width: 700px; margin: 10px auto; font:12px sans-serif;}
table{border-collapse:collapse; width:100%;}
td,th{border:1px solid #999999; padding: 3px}
.e{width:35%;background-color:#ffffcc;font-weight:bold;color:#000}
.h{background:url('http://python.org/images/header-bg2.png') repeat-x;}
.v{background:#f2f2f2;}
img{float:right;border:0;}
</style>
</head>
<body>
<table><tr class="h"><td>
<img src="http://python.org/images/python-logo.gif">
<h1 class="p">Python $py_version</h1>
</td></tr></table>
$output
</body>
</html>
''')
 
 
def make_table(title, data):
return '<h2>{}</h2><table>{}</table><br>'.format(title, ''.join(
'<tr><td class="e">{}</td><td class="v">{}</td></tr>'.format(*row) for row in data
))
 
 
def pyinfo(data=None, allow_import=True, template=output_template, make_table_fn=make_table):
"""
@param data: None or a list of additional (key, values) to include in report,
like [('Application version', '2.1')]
@param allow_import: allow importing (many) new modules or not, defaults to True
@return: complete rendered html
"""
_process_out = lambda out: '' if not out else make_table_fn(*out)
output = ''
output += _process_out(section_server_info(data))
output += _process_out(section_system())
output += _process_out(section_py_internals())
output += _process_out(section_os_internals())
output += _process_out(section_environ())
if allow_import:
output += _process_out(section_compression())
output += _process_out(section_ldap(allow_import))
output += _process_out(section_socket())
if allow_import:
output += _process_out(section_multimedia())
output += _process_out(section_packages())
 
if template is None:
return output
return template.substitute(output=output, py_version=platform.python_version())
 
 
def imported(module):
""" returns 'enabled' if a module is imported, '-' if it isn't"""
try:
if module not in sys.modules:
__import__(module)
return 'enabled'
except:
return '-'
 
 
def section_system():
data = []
if hasattr(sys, 'subversion'):
data.append(('Python Subversion', ', '.join(x for x in sys.subversion if x)))
if platform.dist()[0] != '' and platform.dist()[1] != '':
data.append(('OS Version', '%s %s (%s %s)' % (
platform.system(), platform.release(), platform.dist()[0].capitalize(), platform.dist()[1])))
else:
data.append(('OS Version', '%s %s' % (platform.system(), platform.release())))
if hasattr(sys, 'executable'):
data.append(('Executable', sys.executable))
data.append(('Build Date', platform.python_build()[1]))
data.append(('Compiler', platform.python_compiler()))
if hasattr(sys, 'api_version'):
data.append(('Python API', sys.api_version))
return 'System', data
 
 
def section_server_info(data):
data = list(data) if data else []
data.append(('Hostname', socket.gethostname()))
try:
data.append(('IP Address', socket.gethostbyname(socket.gethostname())))
except:
pass
data.append(('Local time', str(datetime.now())))
data.append(('UTC time', str(datetime.utcnow())))
return 'Server Info', data
 
 
def section_py_internals():
data = []
if hasattr(sys, 'builtin_module_names'):
data.append(('Built-in Modules', ', '.join(sys.builtin_module_names)))
data.append(('Byte Order', sys.byteorder + ' endian'))
if hasattr(sys, 'getcheckinterval'):
data.append(('Check Interval', sys.getcheckinterval()))
if hasattr(sys, 'getfilesystemencoding'):
data.append(('File System Encoding', sys.getfilesystemencoding()))
data.append(('Maximum Integer Size', str(sys.maxsize) + ' (%s)' % str(hex(sys.maxsize)).upper().replace("X", "x")))
if hasattr(sys, 'getrecursionlimit'):
data.append(('Maximum Recursion Depth', sys.getrecursionlimit()))
if hasattr(sys, 'tracebacklimit'):
data.append(('Maximum Traceback Limit', sys.tracebacklimit))
else:
data.append(('Maximum Traceback Limit', '1000'))
data.append(('Maximum Unicode Code Point', sys.maxunicode))
return 'Python Internals', data
 
 
def section_os_internals():
data = []
if hasattr(os, 'getcwd'):
data.append(('Current Working Directory', os.getcwd()))
if hasattr(os, 'getegid'):
data.append(('Effective Group ID', os.getegid()))
if hasattr(os, 'geteuid'):
data.append(('Effective User ID', os.geteuid()))
if hasattr(os, 'getgid'):
data.append(('Group ID', os.getgid()))
if hasattr(os, 'getgroups'):
data.append(('Group Membership', ', '.join(map(str, os.getgroups()))))
if hasattr(os, 'linesep'):
data.append(('Line Seperator', repr(os.linesep)[1:-1]))
if hasattr(os, 'getloadavg'):
data.append(('Load Average', ', '.join(str(round(x, 2)) for x in os.getloadavg())))
if hasattr(os, 'pathsep'):
data.append(('Path Seperator', os.pathsep))
try:
if hasattr(os, 'getpid') and hasattr(os, 'getppid'):
data.append(('Process ID', ('%s (parent: %s)' % (os.getpid(), os.getppid()))))
except:
pass
if hasattr(os, 'getuid'):
data.append(('User ID', os.getuid()))
return 'OS Internals', data
 
 
def section_environ():
envvars = list(os.environ.keys())
envvars.sort()
data = []
for envvar in envvars:
data.append((envvar, escape(str(os.environ[envvar]))))
return 'Environment', data
 
 
def section_compression():
return ('Compression and archiving', [
('SQLite3', imported('sqlite3')),
('Bzip2 Support', imported('bz2')),
('Gzip Support', imported('gzip')),
('Tar Support', imported('tarfile')),
('Zip Support', imported('zipfile')),
('Zlib Support', imported('zlib'))
])
 
 
def section_ldap(allow_import):
try:
if allow_import:
import ldap
else:
ldap = sys.modules['ldap']
except (KeyError, ImportError):
return ''
return ('LDAP support', [
('Python-LDAP Version', ldap.__version__),
('API Version', ldap.API_VERSION),
('Default Protocol Version', ldap.VERSION),
('Minimum Protocol Version', ldap.VERSION_MIN),
('Maximum Protocol Version', ldap.VERSION_MAX),
('SASL Support (Cyrus-SASL)', ldap.SASL_AVAIL),
('TLS Support (OpenSSL)', ldap.TLS_AVAIL),
('Vendor Version', ldap.VENDOR_VERSION)
])
 
 
def section_socket():
return ('Socket', [
('Hostname (fqdn)', socket.gethostbyaddr(socket.gethostname())[0]),
('IPv6 Support', getattr(socket, 'has_ipv6', False)),
('SSL Support', hasattr(socket, 'ssl')),
])
 
 
def section_multimedia():
return ('Multimedia support', [
('AIFF Support', imported('aifc')),
('Color System Conversion', imported('colorsys')),
('curses Support', imported('curses')),
('IFF Chunk Support', imported('chunk')),
('Image Header Support', imported('imghdr')),
('OSS Audio Device Support', imported('ossaudiodev')),
('Raw Audio Support', imported('audioop')),
('SGI RGB Support', imported('rgbimg')),
('Sound Header Support', imported('sndhdr')),
('Sun Audio Device Support', imported('sunaudiodev')),
('Sun AU Support', imported('sunau')), ('Wave Support', imported('wave'))])
 
 
def section_packages():
data = []
try:
import pkg_resources
except:
return ''
for pkg in pkg_resources.working_set:
assert isinstance(pkg, pkg_resources.Distribution)
data.append((pkg.project_name, pkg.version if pkg.has_version() else '[uknown]'))
return 'Installed Modules (Site Packages)', sorted(data, key=lambda a: a[0].lower())
 
 
if __name__ == '__main__':
def _text_table(title, data):
return '===== {}\n{}\n\n'.format(title, '\n'.join(
'{:30} {}'.format(*row) for row in data
))
print(pyinfo(template=None, make_table_fn=_text_table))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.