Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python web服务器提供和记录当前目录?_Python_Logging_Get_Webserver - Fatal编程技术网

如何使用python web服务器提供和记录当前目录?

如何使用python web服务器提供和记录当前目录?,python,logging,get,webserver,Python,Logging,Get,Webserver,我需要创建一个Web服务器,它将通过为指定文件夹中的页面提供服务来响应GET请求,并记录用户正在获取的页面和用户的IP 主要的问题来自我不知道在重写do_GET方法时如何向用户提供目录列表。以下是我目前的代码: #!/usr/bin/env python import logging import SimpleHTTPServer import SocketServer import SimpleHTTPServer import BaseHTTPServer import os PORT =

我需要创建一个Web服务器,它将通过为指定文件夹中的页面提供服务来响应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()

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


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


  • 谢谢你的快速回复,我来看看!您应该尝试在这里粘贴至少几行代码。谢谢您的快速回复,我会检查它的!您应该尝试在此处粘贴至少几行代码。