I am trying to ftp on a windows system using a file with the settings in it. This is what is in the file:

Address.to.device
username
password
"quote"
"site reboot"
quit

This will reboot a camera and it works. I even have it running on powershell script and it works. I can not get it work in powershell.

here is what I have:

subprocess.call(["C:/Windows/System32/ftp.exe", "-s:C:\test\ftp.txt"])

How do I get this to work and how do I get it to wait for its completetion before moving on?

I could figure out how to use ftplib to send the commands. I need it to send the below commands for it to be worth it:

"quote"
"site reboot"

How do I do that? If it helps, I am a python newbie and having fun learning it. I love this language and love its power

Edited 4 Years Ago by treyb

Here is what I got so far......

ftphost = fname + hostend  #device.address.edu
from ftplib import FTP
ftp = FTP(ftphost)
ftp.login('user', 'pass')
ftp.sendcmd("\"quote\"")
ftp.sendcmd("site reboot")
ftp.sendcmd("quit")

It throws this:

Traceback (most recent call last):
  File "C:\Users\treyb\Desktop\pythontest.py", line 57, in <module>
    ftp = FTP(ftphost)
  File "C:\Python27\lib\ftplib.py", line 117, in __init__
    self.connect(host)
  File "C:\Python27\lib\ftplib.py", line 132, in connect
    self.sock = socket.create_connection((self.host, self.port), self.timeout)
  File "C:\Python27\lib\socket.py", line 553, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
gaierror: [Errno 11004] getaddrinfo failed

How do I fix this?

Here the solution to this error was removing "0.0.0.0 localhost " from my hosts file located in "C:\Windows\System32\drivers\etc"

that did not work :(

I have updated the code to this:

    ftp = ftplib.FTP(ftphost, "user", "pass")
    ftp.sendcmd("\"quote\"")
    ftp.sendcmd("site reboot")
    ftp.sendcmd("quit")

Edited 4 Years Ago by treyb

nope

An example of one of the addresses is cse-211-cam01.circa.ufl.edu

It will take the name of a folder (cse-211-cam01) and then add the .circa.ufl.edu with

hostend = ".circa.ufl.edu"
ftphost = fname + hostend

Edited 4 Years Ago by treyb

Maybe you need FTP_TLS, or need to have acct not ''?

FTP.set_debuglevel(level) could help to spot problem cause.

Actually I get same error when trying the HELP example:

>>> from ftplib import FTP
>>> ftp = FTP('ftp.cwi.nl')

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    ftp = FTP('ftp.cwi.nl')
  File "I:\python27\lib\ftplib.py", line 117, in __init__
    self.connect(host)
  File "I:\python27\lib\ftplib.py", line 132, in connect
    self.sock = socket.create_connection((self.host, self.port), self.timeout)
  File "I:\python27\lib\socket.py", line 553, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
gaierror: [Errno 11001] getaddrinfo failed
>>> 

But this address does not exist anymore, this works:

>>> ftp = FTP('ftp.uwasa.fi')
>>> ftp.login()
"230-> Welcoming to Garbo archives.\n230->\n230-> Type 'ls -CF' or 'ls -lF' or 'ls -lF |more' for the directory contents.\n230-> Type 'cd /pc' to go to the main MsDos directory. May differ on mirrors.\n230->       ^^^^^^\n230-> No support. All the moderators have other, principal occupations.\n230-> For any questions please see http://lipas.uwasa.fi/~ts/garbo.html#faq\n230->\n230->     +-----------------------------------------------------------+\n230->     ! If you have files to submit please do *NOT* upload before !\n230->     ! carefully reading the /pc/UPLOAD.TXT instructions         !\n230->     ! Uploads without _*EMAILED*_ announcements are rejected.   !\n230->     ! First see http://www.uwasa.fi/~ts/garbinfo/garb1996.html  !\n230->     +-----------------------------------------------------------+\n230->\n230-> Your actions at Garbo are logged. If this bothers you, type 'quit' now.\n230 Login successful."
>>> ftp.retrlines('LIST')
drwxr-xr-x   63 ftp      ftp          4096 Dec 22 10:50 cs
drwxr-xr-x    2 ftp      ftp          4096 Mar 23  2004 garbo-gifs
drwx------    2 ftp      ftp         16384 Jan 21  2000 lost+found
-rw-r--r--    1 ftp      ftp      14116208 Apr 01 20:55 ls-lR
lrwxrwxrwx    1 ftp      ftp             8 Mar 30  2006 mirror -> mirrors/
drwxr-xr-x    3 ftp      ftp          4096 Aug 12  2008 mirrors
drwxr-xr-x  130 ftp      ftp          4096 Apr 01 20:55 pc
drwxrwxr-x    2 ftp      ftp          4096 Apr 12  2009 private-ts
lrwxrwxrwx    1 ftp      ftp             1 Mar 30  2006 pub -> .
drwxr-xr-x    7 ftp      ftp          4096 Feb 13  2005 ql
drwxr-xr-x   11 ftp      ftp          4096 Mar 23  2004 software
drwxr-xr-x   56 ftp      ftp          4096 Apr 01 20:55 unix
drwxr-xr-x    5 ftp      ftp          4096 Oct 16  2002 win32
drwxr-xr-x   66 ftp      ftp          4096 Apr 01 20:55 win95
drwxr-xr-x   36 ftp      ftp          4096 Apr 01 20:55 windows
'226 Directory send OK.'
>>> 

Edited 4 Years Ago by pyTony

have no idea what happened, but this morning

ftp = ftplib.FTP(ftphost, "user", "pass")
ftp.login()

worked. Now the issue is how do I send the text commands to the device.

ftp.sendcmd('\"quote\"')

The above does not work. Throws:

Traceback (most recent call last):
  File "<pyshell#46>", line 1, in <module>
    ftp.sendcmd('\"quote\"')
  File "C:\Python27\lib\ftplib.py", line 244, in sendcmd
    return self.getresp()
  File "C:\Python27\lib\ftplib.py", line 219, in getresp
    raise error_perm, resp
error_perm: 500 Syntax error.

Am I using the command correctly?

Edited 4 Years Ago by treyb

Simple

ftp.sendcmd("quote site reboot")

or without quote

ftp.sendcmd("site reboot")

did not work?

Edited 4 Years Ago by pyTony

 ftp.sendcmd("quote site reboot")

 Traceback (most recent call last):
   File "<pyshell#54>", line 1, in <module>
     ftp.sendcmd("quote site reboot")
   File "C:\Python27\lib\ftplib.py", line 244, in sendcmd
     return self.getresp()
   File "C:\Python27\lib\ftplib.py", line 219, in getresp
     raise error_perm, resp
 error_perm: 500 Syntax error.

That is what I got. The "quote" HAS to have the "'s around them.

Edited 4 Years Ago by treyb

I keep getting a syntax error when I do:

ftp.sendcmd("\"quote\"")

I need to have the quotes in it and I have tried everything I can think of, including creating a variable and nothing.

How about

   ftplib.sendcmd('"quote" site reboot')

or

   ftplib.sendcmd('"quote" "site reboot"')

Edited 4 Years Ago by pyTony

ftp.sendcmd("quote site reboot")

 Traceback (most recent call last):
   File "<pyshell#54>", line 1, in <module>
     ftp.sendcmd("quote site reboot")
   File "C:\Python27\lib\ftplib.py", line 244, in sendcmd
     return self.getresp()
   File "C:\Python27\lib\ftplib.py", line 219, in getresp
     raise error_perm, resp
 error_perm: 500 Syntax error.

That is what I got. The "quote" HAS to have the "'s around them.

Ok moved it to the actual server and the below works:

ftphost = fname + hostend
ftp = ftplib.FTP(ftphost, "user", "pass")
ftp.login
quote = "\"quote\""
ftp.sendcmd("site reboot")
ftp.sendcmd("quit")
ftp.quit

How do I get it to wait for the process to finish before moving on to the next one (it is located in a for loop)

And now I have an issue with smtp... I have searched it everywhere and it looks 100% correct... I have opened the port 587
and still get a fail.

Here is the Code:

SUBJECT = "Camera report:"
TO = "me@domain.com"
FROM = "server@domain.com"
BODY = "testing"
server = smtplib.SMTP('smtp.sever.com', 587)
server.ehlo()
server.login("username@domain.com", "pass")
server.sendmail(FROM, TO, BODY)
server.quit()

and I get the error code of:

File "C:\Python27\lib\smtplib.py", line 604, in login
    raise SMTPAuthenticationError(code, resp)
SMTPAuthenticationError: (535, '5.7.0 authentication failed')

any ideas what I need to add or change to fix this?

line 3 and 4 seems to do nothing in your first code (method name without call and unused variable).

Sorry about yesterday.. I was not at work. The ftp work correctly, although I cleaned up the part you talked about. Below is the corrected code.

ftphost = fname + hostend
ftp = ftplib.FTP(ftphost, "user", "pass")
ftp.login
ftp.sendcmd("site reboot")
ftp.sendcmd("quit")
ftp.quit

The problem I am having now is the email section now. I know the username and password is correct (I am using it successfully with powershell at the moment) so that just leave the syntax or some other issues.

Why does the blow not work? (I have created a test .py with just the email)

import smtplib

SUBJECT = "Camera report:"
TO = "me@domain.com"
FROM = "user@domain.com"
BODY = "testing"
server = smtplib.SMTP('smtp.ufl.edu')
server.ehlo()
server.login("user@domain.com", "pass")
server.sendmail(FROM, TO, BODY)
server.quit()

I just tried that snippet and I still get the authentication error. I retried the user name and pass in powershell and it worked just fine. There has to be something I am missing.

This article has been dead for over six months. Start a new discussion instead.