【问题标题】:How do I serve and log my current directory with a python web server?如何使用 python Web 服务器服务和记录我的当前目录?
【发布时间】:2010-09-01 02:52:47
【问题描述】:

我需要创建一个网络服务器,它将通过从指定文件夹提供页面来响应 GET 请求,并记录用户正在获取的页面以及用户的 IP。

主要的麻烦来自我不知道如何在覆盖 do_GET 方法时向用户提供目录列表。到目前为止,这是我的代码:

#!/usr/bin/env python
import logging
import SimpleHTTPServer
import SocketServer
import SimpleHTTPServer
import BaseHTTPServer
import os

PORT = 8001
LOG_FILENAME = 'log.txt'
logging.basicConfig(filename=LOG_FILENAME, level=logging.DEBUG)

class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        try:
            #self.send_response(200)
            #self.send_header('Content-type', 'text/html')
            #self.end_headers();
            #self.list_directory(self.path)
            #os.listdir()
            logging.debug('Test text')            
        except IOError:
            print "nothing"

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

httpd = SocketServer.TCPServer(("", PORT), MyHandler)

print "serving at port", PORT
httpd.serve_forever()

【问题讨论】:

    标签: python logging get webserver


    【解决方案1】:

    您需要使用 dir_listing() 来列出目录。 与其在这里写,我建议你查看 python 食谱/食谱以获得详细的指导和理解。

    1. http://code.activestate.com/recipes/392879-my-first-application-server/

    【讨论】:

    • 感谢您的快速回复,我去看看!
    • 您应该尝试在此处粘贴至少几行代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多