how do i do this - stream file

A

Ajay

hi!

How do i create and stream a file?
On a webpage i have a link to a file. Next to it i would like link, which,
when pressed, runs a script that calculates the signature of the file,
pickles the signature to a file and then gives a dialog box asking the
user whether they would like to save the file or open it.

I have the script to do the signing and pickle the signature into a file,
but how do i allow the user to download it?

thanks

cheers
 
D

Diez B. Roggisch

How do i create and stream a file?
On a webpage i have a link to a file. Next to it i would like link, which,
when pressed, runs a script that calculates the signature of the file,
pickles the signature to a file and then gives a dialog box asking the
user whether they would like to save the file or open it.

I have the script to do the signing and pickle the signature into a file,
but how do i allow the user to download it?

You'll need a webserver - there exist simple http-servers in the standard
modules, you can use twisted or one of the plethorea of
web-serving-solutions that exist for python, like (on particular order nor
a complete list) quixote, zope, mod_python and so on....
 
C

Christopher T King

I have the script to do the signing and pickle the signature into a file,
but how do i allow the user to download it?

Instead of pickling it to a file, pickle it to a string (using dumps).
Then you can change your script to a CGI script like the following:

#!/usr/local/bin/python <-- replace this with the real location of Python
on your server
import sys
from pickle import dumps

<do stuff>

mydata = dumps(<something>)

sys.stdout.write('Content-type: application/x-pickle\n\n')
sys.stdout.write(mydata)

That first sys.stdout.write is all that's needed to make a basic CGI
script: the Content-type line tells the web browser what type of
information to expect (the MIME type). Then you can just send whatever
data you like (this works with print statements too; but remember that
they append a \n to your data).

For normal web pages, the MIME type is text/html. The type
application/x-pickle is just something I made up, since there is no MIME
type corresponding to pickled data. You may even consider just sending
the signature as text and use the standard MIME type text/plain.

You should put your script in the cgi-bin/ directory on your web
server, and set its executable bit if it's a Unix server.
 
L

Larry Bates

Actually this can happen automatically. If the
user's machine doesn't have a file association for
the file extension of the file that they click on
the browser automatically displays a open/save
dialog for them when they click on the hyperlink.
If they have an association defined on their machine,
for the extension it launches the application when
they click the hyperlink. On Windows this behavior
is configurable. They can always right click on
hyperlink and choose save instead.

HTH,
Larry Bates
Syscon, Inc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top