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

SimpleHTTPServer, Detecting when file transfer is finished?

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

您可以覆盖 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()