1

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消息。我应该如何为其提供数据?

4

1 回答 1

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()

示例结果:

示例结果

于 2012-06-16T20:54:55.163 回答