2012-06-16 4 views
1

pydoc을 사용하면 브라우저의 내 PYTHONPATH에 추가 된 디렉토리의 Python 모듈 및 패키지에 대한 문서를 볼 수 있습니다. 해당 파일의 전체 코드를 탐색 할 수있는 방법이 있습니까? localhost:8080에가는브라우저에서 Python 파일의 내용/코드를 탐색하는 프로그램과 유사한 pydoc?

#!/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() 

no data received 메시지를 보여줍니다

나는 3to2를 사용 에도아르도 Ivanec의 코드를 변환. 어떻게 데이터를 피드해야합니까?

답변

2

pygments의 도움으로 표준 라이브러리에서 HTTP 서버를 확장하여 디렉토리에 강조 표시되고 행 번호가 매겨진 Python 소스 파일을 제공 할 수 있습니다.

#!/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() 

파이썬 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

+0

헤이 에두아르도

파이썬 3에 대한 예입니다 ! '3to2'를 사용하여 코드를 변환했습니다. (편집 된 원본 게시물을 참조하십시오.) 그러나 'no data received'메시지가 나타나면 어떻게 데이터를 전달해야합니까? – Bentley4

+0

너무 나빠요. 몇 분 후에 집에 가면 시험해 보겠습니다. –

+0

SimpleHTTPRequestHandler에 대한'super()'호출이 실패한 것처럼 보입니다. 이전 스타일의 클래스 일 수도 있습니다. 이상하게 보이지만 실제로 확인하지는 않았을 수도 있습니다. 편집에서 새 버전을 확인하십시오. 부모 클래스에 대한 명시 적 호출과 함께 작동합니다. –