SimpleHTTPServer,检测文件传输何时完成?

时间:2015-09-10 11:32:07

标签: python simplehttpserver

我正在使用SimpleHTTPServer制作简单的文件共享实用程序。我希望能够在http传输完成时捕获,即客户端文件下载完成时。那时我想做一些动作。这可能吗?

1 个答案:

答案 0 :(得分:3)

您可以在SimpleHTTPServer模块中覆盖请求处理程序。我在下面放了一个片段

from __future__ import print_function
import os
import SimpleHTTPServer
import SocketServer


class MySimpleHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):

    def do_GET(self):
        """Serve a GET request."""
        f = self.send_head()
        if f:
            try:
                self.copyfile(f, self.wfile)
            finally:
                if(os.path.isfile(self.translate_path(self.path))):
                    print("Log this download:", self.translate_path(self.path))
                f.close()

PORT = 8000

Handler = MySimpleHTTPRequestHandler

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

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