Problem getting shutil.copy() to transfer files to my Website - ?!

Randy Burgess rburgess1 at hvc.rr.com
Mon Dec 23 12:23:45 EST 2002


Aha!

I've figured out what the problem is and how to fix it. First, Martin is
correct (in his other post) - I am using shutil.copy in conjunction with the
standard file input function available to all browsers. Sorry if I didn't
make this clear.

Second, the problem is that I didn't include a necessary tag in the browser
form - namely, enctype="multipart/form-data". Without this tag, the browser
will submit the filepath but not the actual file when sending headers back
to the server. And obviously (as everyone has pointed out), the server can't
use that filepath locally. It has nothing to do with Windows versus Unix -
just that a filepath local to the client is meaningless to the server.

Thanks for all answers - I'm going to go ahead and recode now that I've
figured out the form problem.



"Cliff Wells" <clifford.wells at attbi.com> wrote in message
news:mailman.1040584110.15127.python-list at python.org...
> On Sun, 2002-12-22 at 09:44, Randy Burgess wrote:
> > Hi,
> >
> >  I tooled up a simple CGI script w/Python to transfer files from my
desktop
> > to my Web site (I'm actually going to use this with a project to help
others
> > transfer .DOC and .TXT files to my site w/out an FTP client.)
> >    It worked fine testing it on localhost (I run Xitami), but bombs out
on
> > my Web hosting service's unix box. Python gives me an error message like
> > "Can't copy C:\test\test.txt to /home/www/usable-thought/cgi-bin: (2,
'No
> > such file or directory') .
>
> I was very confused by this thread until it dawned on me what you are
> trying to do:  you are trying to have a cgi script on a remote server
> pull a file from the user's pc.  I'm willing to be corrected on this,
> but this doesn't seem possible.  Further, if it is, please let me know
> which browsers it works with so I can avoid them like the plague.
>
> Anyway, the simplest solution to what you are trying to do is this:
> have the clients ftp the files to a directory on the server and have a
> cgi pick them up.  Even Windows comes with an ftp client.  If they truly
> don't have one, put a link to somewhere they can download one (or write
> one in python).
>
> The bottom line is the user will have to send the file to you, you can't
> go out and get it.
>
>
>
>
> >    Not sure what could be wrong - code snippet below. If anyone has
ideas
> > I'd be grateful. I'm a novice at this but find Python very useful.
Thanks! -
> > Randy Burgess - rburgess at usable-thought.com
> >
> > if form.has_key('FILE'):
> >     from shutil import copy
> >     okay_filetypes = ["doc", "xls", "txt", "htm", "html"]
> >     import os
> >     upload_path = os.getcwd() # returns something like
> > /home/www/usable-thought/cgi-bin
> >     import string
> >     if string.lower(filepath.value[-3:]) in okay_filetypes:
> >         try:
> >             copy(filepath.value, upload_path)
> >             htmlPrintLine("Your document was saved under its own name to
" +
> > upload_path)
> >         except (IOError, os.error), why:
> >             print "Can't copy %s to %s: %s" % (filepath.value,
upload_path,
> > str(why)) # debugging
> >             htmlPrintLine("<b>Error:</b> Invalid filename or path - try
> > again!")
> >
> >     else:
> >         types = list_to_string(okay_filetypes)
> >         htmlPrintLine("<b>Error:</b> Document not uploaded - we only
accept
> > " + types + ".")
> --
> Cliff Wells <clifford.wells at attbi.com>
>
>





More information about the Python-list mailing list