downloading cgi advice
Ian Bicking
ianb at colorstudy.com
Thu May 9 21:00:13 EDT 2002
On Thu, 2002-05-09 at 17:38, Jeff Shipman wrote:
> I've got a cgi which handles downloading
> files to a person's computer. Files of
> type .tar.gz and .exe go through this CGI.
> These are some snippets I have from the
> program:
>
> # Determine size of file
> try:
> s = os.stat(BASEDIR+'/'+loc)
>
> size = int(s[6])
> except OSError:
> errmsg('Couldn\'t stat() file!')
> except:
> errmsg('Unexpected error while calling stat()!')
In case of errors, you should send proper error messages to the client.
Something like:
print 'Status: 404 Not Found'
or in the case of unknown errors:
print 'Status: 500 Internal Server Error'
Otherwise it looks fine. Though when you do
sys.stdout.write(file.read(size)), you'll have the entire file in memory
for a short time -- if you have the memory, and a low number of users,
it doesn't really matter. If not you should chunk it like:
while 1:
t = file.read(4096) # 4096 = random number
if not t: break
sys.stdout.write(t)
Ian
More information about the Python-list
mailing list