1
pydoc允許我查看在瀏覽器中添加到我的PYTHONPATH中的目錄中的Python模塊和包的文檔。有沒有辦法瀏覽這些文件的完整代碼?類似pydoc的程序在瀏覽器中瀏覽Python文件的內容/代碼?
我轉換使用3to2愛德華多伊萬內茨的代碼:
#!/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
消息。我應該如何提供數據?
嘿愛德華多!我使用'3to2'來轉換代碼(請參閱已編輯的原始文章)。但是,我收到了「無數據收到」消息,應如何傳遞數據? – Bentley4
這太糟糕了 - 我會在幾分鐘後回家時嘗試。 –
似乎對SimpleHTTPRequestHandler的'super()'調用失敗 - 這可能是因爲它是一箇舊式類,雖然這看起來很奇怪,但我沒有真正檢查。請參閱編輯中的新版本,它可以對顯示的父類進行調用。 –