Serving a file
Revision as of 22:20, 27 May 2009 by Michael Murtaugh (talk | contribs) (New page: Example of a Python script to serve a file as a "download" link. <source lang="python"> #!/usr/bin/env python import os filepath = "/path/to/the/file" filelen = os.path.getsize(filepath)...)
Example of a Python script to serve a file as a "download" link.
#!/usr/bin/env python
import os
filepath = "/path/to/the/file"
filelen = os.path.getsize(filepath)
print "Content-type: application/download"
print "Content-length: %d" % filelen
print "Content-transfer-encoding: binary"
print "Content-disposition: attachment; filename=%s" % os.path.basename(filepath)
print
f = open(filepath, "rb")
bytesserved = 0
while True:
data = f.read(1024)
if not data:
break
bytesserved += len(data)
sys.stdout.write(data)
f.close()