Arboreal Shark
Arboreal Shark

Reputation: 2501

SimpleHTTPServer, Detecting when file transfer is finished?

I'm making simple file sharing utility using SimpleHTTPServer. I would like to be able to catch when a http-transfer completes, i.e. when a clients file download is finished. I'd like to perform some actions at that point. Is that possible?

Upvotes: 2

Views: 178

Answers (1)

Ozan
Ozan

Reputation: 1074

You can override the request handler in SimpleHTTPServer module. I put a snippet below

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()

Upvotes: 3

Related Questions