No

G

Gaz

Check this piece of code:

#now add an image part
part = writer.nextpart()
part.addheader('Content-Transfer-Encoding', 'base64')
body = part.startbody('image/jpeg')
base64.encode(open('c:\check.jpg', 'rb'), body)

I get the following error:

IOError: [Errno 2] No such file or directory: 'c:\\check.jpg'
args = (2, 'No such file or directory')
errno = 2
filename = r'c:\check.jpg'
strerror = 'No such file or directory'

Dont know why im getting such error, the file is there. Perhaps wrong
filetype? Wrong read mode?
 
P

Peter Hansen

Gaz said:
Check this piece of code:

#now add an image part
part = writer.nextpart()
part.addheader('Content-Transfer-Encoding', 'base64')
body = part.startbody('image/jpeg')
base64.encode(open('c:\check.jpg', 'rb'), body)

I get the following error:

IOError: [Errno 2] No such file or directory: 'c:\\check.jpg'
args = (2, 'No such file or directory')
errno = 2
filename = r'c:\check.jpg'
strerror = 'No such file or directory'

Dont know why im getting such error, the file is there. Perhaps wrong
filetype? Wrong read mode?

What does it show if you put "print os.listdir('c:/')" on the line
before the open? (You'll obviously need to import os first.)

-Peter
 
G

Gaz

OSError: [Errno 2] No such file or directory: 'c:/'
args = (2, 'No such file or directory')
errno = 2
filename = 'c:/'
strerror = 'No such file or directory'

Crazy... and if i use c:\, i get a 500 error.
 
F

Fredrik Lundh

Gaz said:
OSError: [Errno 2] No such file or directory: 'c:/'
args = (2, 'No such file or directory')
errno = 2
filename = 'c:/'
strerror = 'No such file or directory'

Crazy... and if i use c:\, i get a 500 error.

that's probably because "c:\" is a syntax error:
File "<stdin>", line 1
"c:\"
^
SyntaxError: EOL while scanning single-quoted string

but since you're getting a "500 error" instead, it sure sounds like you
forgot to tell us *how* you're running your scripts... (server, platform,
python version, etc).

</F>
 
G

Gaz

Im running it in my test site at F2o.org. Perhaps its server's fault
and not script's fault. I'll deal this issue at its forums.

:)
 
F

Fredrik Lundh

Gaz said:
Im running it in my test site at F2o.org. Perhaps its server's fault
and not script's fault. I'll deal this issue at its forums.

according to their FAQ, they run Red Hat Enterprise Linux. I'm pretty
sure RHEL doesn't come with a C: drive...

</F>
 
G

Gaz

Aye, but the file is in MY drive, not the server. Perhaps thats the
issue? I bet it's so... how should i deal with it? Perhaps should be a
script that uploads the files to the server and then emails it... i
believed this was handled using the root in my PC, but now i see this
is not correct.

Now i need a script for uploading the files from MY pc and then use the
sendmail thingie. Any tips about that script?
 
S

Steve Holden

Fredrik said:
:

OSError: [Errno 2] No such file or directory: 'c:/'
args = (2, 'No such file or directory')
errno = 2
filename = 'c:/'
strerror = 'No such file or directory'

Crazy... and if i use c:\, i get a 500 error.


that's probably because "c:\" is a syntax error:


File "<stdin>", line 1
"c:\"
^
SyntaxError: EOL while scanning single-quoted string

but since you're getting a "500 error" instead, it sure sounds like you
forgot to tell us *how* you're running your scripts... (server, platform,
python version, etc).
The 500 error's almost certainly because the script is failing before it
emits anything on standard output (I'm presuming that the syntax error
will appear on standard error, without having checked ...).

regards
Steve
 
F

Fredrik Lundh

Gaz said:
Aye, but the file is in MY drive, not the server. Perhaps thats the
issue? I bet it's so... how should i deal with it? Perhaps should be a
script that uploads the files to the server and then emails it...

assuming that the web server can read files on your local disk is perhaps
a bit too optimistic.

how did you upload your program to the server in the first place ?

any reason you cannot use the same approach to upload the image ?

</F>
 
S

Steven D'Aprano

assuming that the web server can read files on your local disk is perhaps
a bit too optimistic.

Come on, Gaz is running Windows. Chances are *everyone* can read files on
his local disk.

*wink*
 
G

Gaz

@ Steven: LOL =P

@ Frederik: aye, i uploaded the script by FTP, but the idea of this
little program is to allow a technician on field to upload photos from
their laptops when they get into a hotel, along with some text, anywere
in the world. We need the reports to be daily. Email is not an option,
because due my experience, they always lack of info, with a form, i can
put some conditions for sending the report.

So, they can not upload the photos by FTP because its too "geek" for
them. And i need to have a standarized form to handle the reports,
because otherwise its a organizational mess.

The program should handle the upload thingie in the backstage. I'll
look for a script like this in these forums...
 
K

Kent Johnson

Gaz said:
Aye, but the file is in MY drive, not the server. Perhaps thats the
issue? I bet it's so... how should i deal with it? Perhaps should be a
script that uploads the files to the server and then emails it... i
believed this was handled using the root in my PC, but now i see this
is not correct.

Now i need a script for uploading the files from MY pc and then use the
sendmail thingie. Any tips about that script?
google 'python cgi file upload' for many examples.

Kent
 
S

Sion Arrowsmith

Gaz said:
So, they can not upload the photos by FTP because its too "geek" for
them. And i need to have a standarized form to handle the reports,
because otherwise its a organizational mess.

Am I missing something here, or is all you need a
<INPUT TYPE="FILE" ... >
on your form? Then you don't worry about filenames, you just get
their data supplied to your cgi script.
 
G

Gaz

(the Input type = FILE is in the field)

please check this out

import ftplib
import os

....

def upload(ftp, file):
ext = os.path.splitext(file)[1]
if ext in (".txt", ".htm", ".html"):
ftp.storlines("STOR " + file, open(file))
else:
ftp.storbinary("STOR " + file, open(file, "rb"), 1024)

ftp = ftplib.FTP("ftp.XXXX.f2o.org")
ftp.login("XXXXX", "XXXXX")

upload(ftp, "c:\check.jpg")

I'm still getting the file not found error:

http://img153.imageshack.us/img153/2720/error3ps.jpg

I believe the path is kinda wrong or something. But this time, the
script is supposed to fetch the file from my pc.
 
G

Gerard Flanagan

Gaz said:
(the Input type = FILE is in the field)

please check this out

import ftplib
import os

...

def upload(ftp, file):
ext = os.path.splitext(file)[1]
if ext in (".txt", ".htm", ".html"):
ftp.storlines("STOR " + file, open(file))
else:
ftp.storbinary("STOR " + file, open(file, "rb"), 1024)

ftp = ftplib.FTP("ftp.XXXX.f2o.org")
ftp.login("XXXXX", "XXXXX")

upload(ftp, "c:\check.jpg")

I'm still getting the file not found error:

http://img153.imageshack.us/img153/2720/error3ps.jpg

I believe the path is kinda wrong or something. But this time, the
script is supposed to fetch the file from my pc.

Gaz

Google "file upload cgi".

Gerard
 
D

Dennis Lee Bieber

I'm still getting the file not found error:

http://img153.imageshack.us/img153/2720/error3ps.jpg

I believe the path is kinda wrong or something. But this time, the
script is supposed to fetch the file from my pc.

If the script is supposed to run ON THE SERVER (which is what it
looks like), and FETCH the file, you need to:

1) run an FTPd ON YOUR MACHINE
2) connect to your machine FROM THE SERVER
2a) possibly need to use FTP CD commands to get to where the file is
on YOUR machine
3) Use an FTP RETR, writing the data to the server-local file.


The following is untested, pseudo-code, based upon your listing

import ftplib
import os

local_file = None

# ftp.retrlines strips the line ending, so we need a "command"
# to put a line ending back onto the line when writing
def text_writer(ln):
local_file.write(ln + "\n")

# just for symmetry, we could just supply "local_file.write" as the
# command for binary retrieve
def bin_writer(blk):
local_file.write(blk)

ftp = ftplib.FTP("Your.machine.hostname")
ftp.login("whatever", "you_set_up")

# get remote end path, and plain file name
pname, fname = os.path.split(file)

# direct ftp to move to remote end directory
ftp.cwd(pname)

if os.path.splitext(fname.lower())[-1] in (".txt", ".htm", ".html"):
# create LOCAL file (no path), text mode, using text_writer to
# output the retrieved lines
local_file = open(fname, "wt")
ftp.retrlines("RETR %s" % fname, text_writer)
local_file.close()
else:
# Same, but binary mode, and bin_writer
local_file = open(fname, "wb")
ftp.retrbinary("RETR %s" % fname, bin_writer)
local_file.close()




--
 
G

Gaz

Aye, but i dont need to run an FTP daemon every time i want to upload
an image to Imageshack or any other website.

About the topic name, yes, it's meaningless and its a shame. I fucked
it up when typing the title =P
 
K

Kent Johnson

Gaz said:
Aye, but i dont need to run an FTP daemon every time i want to upload
an image to Imageshack or any other website.

Did you google 'python cgi file upload' yet? Several people have pointed
you to an answer that doesn't use FTP.
 
D

Dennis Lee Bieber

Aye, but i dont need to run an FTP daemon every time i want to upload
an image to Imageshack or any other website.
Imageshack is the end that is running the daemon while the client is
running on your machine.

But if I understand your needs, your script is running on the server
end (processing some web form data that was sent to it)... You are
trying to have the /server/ PULL a file from the client, rather than the
/client/ running a script to PUSH the file to the server.
--
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top