views:

237

answers:

2

I really like how I can easily share files on a network using the SimpleHTTPServer, but I wish there was an option like "download entire directory". Is there an easy (one liner) way to implement this?

Thanks

+2  A: 

There is no one liner which would do it, also what do you mean by "download whole dir" as tar or zip?

Anyway you can follow these steps

  1. Derive a class from SimpleHTTPRequestHandler or may be just copy its code
  2. Change list_directory method to return a link to "download whole folder"
  3. Change copyfile method so that for your links you zip whole dir and return it
  4. You may cache zip so that you do not zip folder every time, instead see if any file is modified or not

Would be a fun exercise to do :)

Anurag Uniyal
Unless you also change `send_head`, you're not going to be zipping up directories which contain an `index.html`, so the change is a bit more pervasive than the one you describe (I gave the detail and line numbers for the current online version of the sources in my answer, which crossed yours on the wire by a few seconds).
Alex Martelli
+2  A: 

Look at the sources, e.g. online here. Right now, if you call the server with a URL that's a directory, its index.html file is served, or, missing that, the list_directory method is called. Presumably, you want instead to make a zip file with the directory's contents (recursively, I imagine), and serve that? Obviously there's no way to do it with a one-line change, since you want to replace what are now lines 68-80 (in method send_head) plus the whole of method list_directory, lines 98-137 -- that's already at least a change to over 50 lines;-).

If you're OK with a change of several dozen lines, not one, and the semantics I've described are what you want, you could of course build the required zipfile as a cStringIO.StringIO object with the ZipFile class, and populate it with an os.walk on the directory in question (assuming you want, recursively, to get all subdirectories as well). But it's most definitely not going to be a one-liner;-).

Alex Martelli