Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

cgi and mime type

14 views
Skip to first unread message

A M

unread,
Feb 12, 2002, 3:25:47 PM2/12/02
to
Hi,
I have a simple cgi script that sends back an exe when a button is
pressed..

I am using the following code to do that :

print 'Content-type: application/octet-stream'
print 'Content-disposition: application; filename="app.exe";
print;

after which I open the file and print it:

fp = open('fp.exe')
print fp.read();

Now when testing it in the browsers NS and IE, because of the content
disposition header IE prompts you to download it with the name in the
header.

However in NS it refuses to recognize the MIME type and prompts that
you are attempting to download "text/plain" script.py

However if i save it to disk: bacuase of the content disposition it
saves the file as app.exe

why doesnt NS recognize my mime type. its a predefined one as far as I
know. Am I doing something wrong in python.

The test URL is : http://66.121.5.70/links.html

Thanks in advance,
A

Chris Liechti

unread,
Feb 12, 2002, 3:58:21 PM2/12/02
to
as...@neurobs.com (A M) wrote in news:ed6d1c1a.0202121225.295ca950
@posting.google.com:

> fp = open('fp.exe')
> print fp.read();

use open(name, 'rb') here. binary mode is important when the cgi runs
on win32. not sure if this fixes your other problem though...

chris

--
Chris <clie...@gmx.net>

Michal Wallace

unread,
Feb 12, 2002, 5:46:57 PM2/12/02
to
On 12 Feb 2002, Chris Liechti wrote:

> as...@neurobs.com (A M) wrote in news:ed6d1c1a.0202121225.295ca950
> @posting.google.com:
>
> > fp = open('fp.exe')
> > print fp.read();
>
> use open(name, 'rb') here. binary mode is important when the cgi runs
> on win32. not sure if this fixes your other problem though...


I missed the start of this thread, but this might help
someone: when printing binary files on stdout on win32, you
also have to make stdout binary:

if sys.platform=="win32":
import msvcrt
msvcrt.setmode(sys.__stdin__.fileno(), os.O_BINARY)
msvcrt.setmode(sys.__stdout__.fileno(), os.O_BINARY)

And you'd probably also want to say:

sys.stdout.write(fp.read())

instead of "print" in the code above...

Cheers,

- Michal http://www.sabren.net/ sab...@manifestation.com
------------------------------------------------------------
Give your ideas the perfect home: http://www.cornerhost.com/
cvs - weblogs - php - linux shell - perl/python/cgi - java
------------------------------------------------------------


Richie

unread,
Feb 13, 2002, 8:06:14 AM2/13/02
to
Hi,

> in NS it refuses to recognize the MIME type and prompts that
> you are attempting to download "text/plain" script.py

I've seen browsers use the filename part of the URL ('script.py' in your
case) to determine the MIME type. One workaround is to code the name of
the download into the URL, so that instead of:

<form action="/cgi-bin/script.py" method="POST">

you have:

<form action="/cgi-bin/script.py/app.exe" method="POST">

The web server will run your script as normal (with '/app.exe' in the
PATH_INFO environment variable) and the browser will see 'app.exe' as the
filename part of the URL.

--
Richie Hindle
ric...@entrian.com

A M

unread,
Feb 13, 2002, 11:15:32 AM2/13/02
to
Hi all,
Thanks for the replies... didnt help as far as my script goes but
learnt something new... thanks again.
A
0 new messages