from fastapi import APIRouter, Path, Request import os, sys, platform, html, socket from fastapi.responses import HTMLResponse router = APIRouter() @router.get('/pyinfo', response_class=HTMLResponse) async def read_items(): output = '\n' output += '' output += '' output += 'pyinfo()' output += '' output += styles() output += '' output += '' output += '
' output += section_title() output += '

System

' output += section_system() output += '

Python Internals

' output += section_py_internals() output += '

OS Internals

' output += section_os_internals() output += '

WSGI Environment

' output += section_environ() output += '

Database support

' output += section_database() output += '

Compression and archiving

' output += section_compression() if 'ldap' in sys.modules: output += '

LDAP support

' output += section_ldap() if 'socket' in sys.modules: output += '

Socket

' output += section_socket() output += '

Multimedia support

' output += section_multimedia() output += '

Copyright

' output += section_copyright() output += '
' output += '' output += '' return HTMLResponse(content=output, status_code=200) def styles(): css = '' return css def table(html): return '%s

' % html def makecells(data): html = '' while data: html += '%s%s' % (data.pop(0), data.pop(0)) return table(html) def imported(module): if module in sys.modules: return 'enabled' return 'disabled' def section_title(): html = '' html += '' html += '

Python %s

' % platform.python_version() html += '' return table(html) def section_system(): data = [] if hasattr(sys, 'subversion'): data += 'Python Subversion', ', '.join(sys.subversion) #if platform.linux_distribution ()[0] != '' and platform.linux_distribution ()[1] != '': # data += 'OS Version', '%s %s (%s %s)' % (platform.system(), platform.release(), platform.linux_distribution ()[0].capitalize(), platform.dist()[1]) #else: # data += 'OS Version', '%s %s' % (platform.system(), platform.release()) data += 'OS Version', '%s %s' % (platform.system(), platform.release()) if hasattr(sys, 'executable'): data += 'Executable', sys.executable data += 'Build Date', platform.python_build()[1] data += 'Compiler', platform.python_compiler() if hasattr(sys, 'api_version'): data += 'Python API', sys.api_version return makecells(data) def section_py_internals(): data = [] if hasattr(sys, 'builtin_module_names'): data += 'Built-in Modules', ', '.join(sys.builtin_module_names) data += 'Byte Order', sys.byteorder + ' endian' if hasattr(sys, 'getcheckinterval'): data += 'Check Interval', sys.getcheckinterval() if hasattr(sys, 'getfilesystemencoding'): data += 'File System Encoding', sys.getfilesystemencoding() if hasattr(sys, 'getrecursionlimit'): data += 'Maximum Recursion Depth', sys.getrecursionlimit() if hasattr(sys, 'tracebacklimit'): tabdatale += 'Maximum Traceback Limit', sys.tracebacklimit else: data += 'Maximum Traceback Limit', '1000' data += 'Maximum Unicode Code Point', sys.maxunicode return makecells(data) def section_os_internals(): data = [] if hasattr(os, 'getcwd'): data += 'Current Working Directory', os.getcwd() if hasattr(os, 'getegid'): data += 'Effective Group ID', os.getegid() if hasattr(os, 'geteuid'): data += 'Effective User ID', os.geteuid() if hasattr(os, 'getgid'): data += 'Group ID', os.getgid() if hasattr(os, 'getgroups'): data += 'Group Membership', ', '.join(map(str, os.getgroups())) if hasattr(os, 'linesep'): data += 'Line Seperator', repr(os.linesep)[1:-1] if hasattr(os, 'getloadavg'): data += 'Load Average', ', '.join(map(str, map(lambda x: round(x, 2), os.getloadavg()))) if hasattr(os, 'pathsep'): data += 'Path Seperator', os.pathsep try: if hasattr(os, 'getpid') and hasattr(os, 'getppid'): data += 'Process ID', ('%s (parent: %s)' % (os.getpid(), os.getppid())) except: pass if hasattr(os, 'getuid'): data += 'User ID', os.getuid() return makecells(data) def section_environ(): envvars = os.environ.keys() sorted(envvars) data = [] for envvar in envvars: if 'DB_PASSWORD' == envvar: data += 'DB_PASSWORD', html.escape('*****') else: if 'SESSION_SECRET_KEY' == envvar: data += 'SESSION_SECRET_KEY', html.escape('*****') else: data += envvar, html.escape(str(os.environ[envvar])) return makecells(data) def section_database(): data = [] data += 'DB2/Informix (ibm_db)', imported('ibm_db') data += 'MSSQL (adodbapi)', imported('adodbapi') data += 'MySQL (MySQL-Python)', imported('MySQLdb') data += 'ODBC (mxODBC)', imported('mxODBC') data += 'Oracle (cx_Oracle)', imported('cx_Oracle') data += 'PostgreSQL (PyGreSQL)', imported('pgdb') data += 'Python Data Objects (PyDO)', imported('PyDO') data += 'SAP DB (sapdbapi)', imported('sapdbapi') data += 'SQLite3', imported('sqlite3') return makecells(data) def section_compression(): data = [] data += 'Bzip2 Support', imported('bz2') data += 'Gzip Support', imported('gzip') data += 'Tar Support', imported('tarfile') data += 'Zip Support', imported('zipfile') data += 'Zlib Support', imported('zlib') return makecells(data) def section_ldap(): data = [] data += 'Python-LDAP Version' % urls['Python-LDAP'], ldap.__version__ data += 'API Version', ldap.API_VERSION data += 'Default Protocol Version', ldap.VERSION data += 'Minimum Protocol Version', ldap.VERSION_MIN data += 'Maximum Protocol Version', ldap.VERSION_MAX data += 'SASL Support (Cyrus-SASL)', ldap.SASL_AVAIL data += 'TLS Support (OpenSSL)', ldap.TLS_AVAIL data += 'Vendor Version', ldap.VENDOR_VERSION return makecells(data) def section_socket(): data = [] data += 'Hostname', socket.gethostname() data += 'Hostname (fully qualified)', socket.gethostbyaddr(socket.gethostname())[0] try: data += 'IP Address', socket.gethostbyname(socket.gethostname()) except: pass data += 'IPv6 Support', getattr(socket, 'has_ipv6', False) data += 'SSL Support', hasattr(socket, 'ssl') return makecells(data) def section_multimedia(): data = [] data += 'AIFF Support', imported('aifc') data += 'Color System Conversion Support', imported('colorsys') data += 'curses Support', imported('curses') data += 'IFF Chunk Support', imported('chunk') data += 'Image Header Support', imported('imghdr') data += 'OSS Audio Device Support', imported('ossaudiodev') data += 'Raw Audio Support', imported('audioop') data += 'Raw Image Support', imported('imageop') data += 'SGI RGB Support', imported('rgbimg') data += 'Sound Header Support', imported('sndhdr') data += 'Sun Audio Device Support', imported('sunaudiodev') data += 'Sun AU Support', imported('sunau') data += 'Wave Support', imported('wave') return makecells(data) def section_copyright(): html = '%s' % sys.copyright.replace('\n\n', '
').replace('\r\n', '
').replace('(c)', '©') return table(html) optional_modules_list = [ 'Cookie', 'zlib', 'gzip', 'bz2', 'zipfile', 'tarfile', 'ldap', 'socket', 'audioop', 'curses', 'imageop', 'aifc', 'sunau', 'wave', 'chunk', 'colorsys', 'rgbimg', 'imghdr', 'sndhdr', 'ossaudiodev', 'sunaudiodev', 'adodbapi', 'cx_Oracle', 'ibm_db', 'mxODBC', 'MySQLdb', 'pgdb', 'PyDO', 'sapdbapi', 'sqlite3' ] for i in optional_modules_list: try: module = __import__(i) sys.modules[i] = module globals()[i] = module except: pass