类似于pydoc的程序在浏览器中浏览Python文件的内容/代码?

时间:2012-06-16 16:33:07

标签: python pydoc

pydoc允许我在浏览器中添加到我的PYTHONPATH的目录中查看Python模块和包的文档。有没有办法浏览这些文件的完整代码?

我使用3to2转换了Edoardo Ivanec的代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import with_statement
import CGIHTTPServer, SimpleHTTPServer, BaseHTTPServer
import os  
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
from io import open

class SourceViewer(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        path = os.path.join(os.getcwdu(), self.path[1:])
        if os.path.exists(path) and path.endswith(u'.py'):
            with open(path) as file:
                code = file.read()
                hl = highlight(code, PythonLexer(), HtmlFormatter(noclasses=True, linenos=u'table'))
                self.send_response(200)
                self.end_headers()
                self.wfile.write(str(hl).encode('UTF-8'))
                return
        else:       
            super(self.__class__, self).do_GET()


if __name__ == u"__main__":
    server = BaseHTTPServer.HTTPServer((u'localhost', 8080), SourceViewer)
    server.serve_forever()

转到localhost:8080会显示no data received条消息。我应该如何提供数据?

1 个答案:

答案 0 :(得分:2)

您可以在pygments的帮助下扩展标准库中的HTTP服务器,以便在目录中提供突出显示的行编号的Python源文件。

这是Python 3的一个例子:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import http.server
import os  
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter

class SourceViewer(http.server.SimpleHTTPRequestHandler):
    def do_GET(self):
        path = os.path.join(os.getcwd(), self.path[1:])
        if os.path.exists(path) and path.endswith('.py'):
            with open(path) as file:
                code = file.read()
                hl = highlight(code, PythonLexer(), HtmlFormatter(noclasses=True, linenos='table'))
                self.send_response(200)
                self.end_headers()
                self.wfile.write(bytes(hl, 'UTF-8'))
                return
        else:       
            super().do_GET()


if __name__ == "__main__":
    server = http.server.HTTPServer(('localhost', 8080), SourceViewer)
    server.serve_forever()

Python 2,基于3to2转换:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import with_statement
import SimpleHTTPServer, BaseHTTPServer
import os  
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter

class SourceViewer(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        path = os.path.join(os.getcwdu(), self.path[1:])
        if os.path.exists(path) and path.endswith(u'.py'):
            with open(path) as file:
                code = file.read()
                hl = highlight(code, PythonLexer(), HtmlFormatter(noclasses=True, linenos=u'table'))
                self.send_response(200)
                self.end_headers()
                self.wfile.write(hl)
                return
        else:    
            SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

if __name__ == u"__main__":
    server = BaseHTTPServer.HTTPServer((u'localhost', 8080), SourceViewer)
    server.serve_forever()

示例结果:

Example result