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