473,500 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help: Uploading .zip to Python CGI

I am uploading a .zip file to a Python CGI program, using a form on
a HTML page with

<input name="yourfile" type="file">...

In the Python CGI program I do:

import cgi
fStorage = cgi.FieldStorage()
zipdata = fStorage['yourfile'].value
print "Content-type: text/plain"
print
print len(zipdata)

Now the length of the zipdata is 100, where it should be about
2635...and unzipping it with zipfile of course gives the "not a zip
file" error.

The last part of the data that is received by the CGI script is:

\xf2\xf1!0\xdbS\xa9

and the next one *should* be \x1a

It seems that the .zip data is being truncated, but I don't know where
in my tool chain.
The strange thing is that the Python CGI script *does* work on a
Apache 1.3.27 server at work (unix), but gives the error above when
run on
my laptop with Windows XP and Apache 1.3.27 and also with the Apache
version 2.0.48 I tried later.

Does anybody have a clue what is going on?

Maybe the error is with the Windows version of Apache? Or is it a
Python problem (the unix server has Python 2.1.1).
Jul 18 '05 #1
6 3827
On Sat, Dec 06, 2003 at 12:40:43PM -0800, Will Stuyvesant wrote:
I am uploading a .zip file to a Python CGI program, using a form on
a HTML page with

<snip>

Without being able to see the form, I wonder if you're certain you set the enctype on the form to "multipart/form-data"?

You're working across multiple servers and if you're retyping the script each time its easy to forget the enctype of the form.

HTH
--
Jay Dorsey
jay at jaydorsey dot com

Jul 18 '05 #2
The problem I described in this thread is with Apache, not with
Python! And the unix Apache at my work has no problems, its only the
Windows Apache versions. So the Apache peeps will probably say it's a
*Windows* problem 0-)

I found out with the following: I can now avoid the first HTML page
with the .zip upload, instead I upload the .zip to my Python CGI
program with this little program:
import urllib
import webbrowser

webserviceURI = r'http://localhost/cgi-bin/mycgiprogram.py'
startpageName = 'start.xml'
# instead of a HTML page with INPUT type=file just read the file
fp = open(fname, 'rb')
data = fp.read()
fp.close()

# all CGI parameters in a dict, and encoded
params = urllib.urlencode({ 'yourfile': data })

# call the CGI program and read what it returns
f = urllib.urlopen(webserviceURI, params)
webpage = f.read()

# save it locally in a file
wpfp = open(startpageName, 'w')
wpfp.write(webpage)
wpfp.close()

# show it in your browser
webbrowser.open(startpageName)
--
If pro is the opposite of con, what is the opposite of progress?
Jul 18 '05 #3
hw***@hotmail.com (Will Stuyvesant) wrote in message news:<cb**************************@posting.google. com>...
I am uploading a .zip file to a Python CGI program, using a form on
a HTML page with

<input name="yourfile" type="file">...

In the Python CGI program I do:

import cgi
fStorage = cgi.FieldStorage()
zipdata = fStorage['yourfile'].value
print "Content-type: text/plain"
print
print len(zipdata)

Now the length of the zipdata is 100, where it should be about
2635...and unzipping it with zipfile of course gives the "not a zip
file" error.

The last part of the data that is received by the CGI script is:

\xf2\xf1!0\xdbS\xa9

and the next one *should* be \x1a

It seems that the .zip data is being truncated, but I don't know where
in my tool chain.
.... Does anybody have a clue what is going on?

Maybe the error is with the Windows version of Apache? Or is it a
Python problem (the unix server has Python 2.1.1).


Had a similare problem with *.jpg uploads

uploading files with a shebang such as:
#! c:/python23/python -u
reolved it for me
the -u part telling Windows to get data "unbuffered", so I read somewhere...

Good weekend,

JM
Jul 18 '05 #4
> [jm*********@cvm.qc.ca]
Had a similare problem with *.jpg uploads

uploading files with a shebang such as:
#! c:/python23/python -u
reolved it for me
the -u part telling Windows to get data "unbuffered", so I read somewhere...


Great! Solved it for me too! Thank you!
Jul 18 '05 #5
Will Stuyvesant <hw***@hotmail.com> wrote:
I am uploading a .zip file to a Python CGI program, using a form on
a HTML page with

<input name="yourfile" type="file">...

In the Python CGI program I do:

import cgi
fStorage = cgi.FieldStorage()
zipdata = fStorage['yourfile'].value
print "Content-type: text/plain"
print
print len(zipdata)

Now the length of the zipdata is 100, where it should be about
2635...and unzipping it with zipfile of course gives the "not a zip
file" error.

The last part of the data that is received by the CGI script is:

\xf2\xf1!0\xdbS\xa9

and the next one *should* be \x1a
\x1a is ASCII for Ctrl-Z, which is used in Windows as an EOF (End Of
File) marker.
It seems that the .zip data is being truncated, but I don't know where
in my tool chain.


Somewhere in your tool chain, something is opening a file in text mode
instead of in binary mode.

The fact that your CGI script works on Unix but fails on Windows is
further proof that the Ctrl-Z is being treated as EOF on Windows, since
Unix doesn't give that character any special meaning.

--
Robin Munn
rm***@pobox.com
Jul 18 '05 #6
On Sun, 6 Dec 2003, Will Stuyvesant wrote:
The last part of the data that is received by the CGI script is:

\xf2\xf1!0\xdbS\xa9

and the next one *should* be \x1a


ISTR that \x1A is control-Z.

Which is the EOF character on CP/M derived systems, and is still
interpreted thusly in the most surprising places in software from Redmond.

As another poster suggested, the -u option is the usual solution.

--
Andrew I MacIntyre "These thoughts are mine alone..."
E-mail: an*****@bullseye.apana.org.au (pref) | Snail: PO Box 370
an*****@pcug.org.au (alt) | Belconnen ACT 2616
Web: http://www.andymac.org/ | Australia

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4452
by: Ralph Freshour | last post by:
I'm trying to code the ability for my users to upload up to photo's to mysql database - can someone point me in the right direction as to how this might be done in php? Perhaps a tutorial or some...
1
4233
by: Vasil Slavov | last post by:
I apologize for the long email. I hope somebody will have time to read it and give some suggestions. I am working on a school project written in Python (using mod_python) and I need to upload a...
2
4950
by: rbt | last post by:
Has anyone used pure python to upload files to a webdav server over SSL? I have no control over the server. I can access it with all of the various webdav GUIs such as Konqueror, Cadaver, etc. by...
4
2645
by: Chris Dewin | last post by:
How do I go about writing a cgi script, that will enable the client to upload things to a directory on my website? I would also like to write a script that enables the client to delete items in...
5
1480
by: hb | last post by:
Hi, In my ASP.Net application 'MyWebApp' , the mode="StateServer" in <sessionState> of Web.config file, and the ASP.NET State Service is set to start automatically on the server. But every...
3
2330
by: Carlos | last post by:
Hello Forum, I would appreciate it if you could recommend settings to use auto-vacuum in my version 7.4 database. I am uploading several thousands records in the database at a rate of ~1 second...
2
1535
by: Velhari | last post by:
Hi all, I developed the following codes to implement the concept of Multiple file Uploading Process. Actually my goal in this implementation is to uploads the files onto the server and i have...
7
3879
by: Lad | last post by:
If a user will upload large files via FTP protocol, must the user have an FTP client on his computer or is it possible to use a similar way to "http form" comunication? Or is there another way(...
1
1230
by: Lad | last post by:
On my website I allow users to upload files. I would like a user to see how much time is left before a file is uploaded. So, I would like to have a progress bar during a file uploading. Can Python...
0
7136
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7182
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7232
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6906
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7397
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5490
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.