I am trying to use ftps to upload file to our FTP server. Login is trivial and works:
from M2Crypto import ftpslib
ftp = ftpslib.FTP_TLS()
ftp.connect(host)
ftp.login(username, password)
as well as descending into directory
for dir in directory:
ftp.cwd(dir)
However, when trying to retrieve directory content:
if directory_name not in ftp.nlst():
ftp.mkd(directory_name)
I get 522 error:
File "/usr/lib/python2.5/ftplib.py", line 459, in nlst
self.retrlines(cmd, files.append)
File "/usr/lib/python2.5/ftplib.py", line 407, in retrlines
conn = self.transfercmd(cmd)
File "/usr/lib/python2.5/ftplib.py", line 356, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "/var/lib/python-support/python2.5/M2Crypto/ftpslib.py", line 86, in ntransfercmd
conn, size = FTP.ntransfercmd(self, cmd, rest)
File "/usr/lib/python2.5/ftplib.py", line 327, in ntransfercmd
resp = self.sendcmd(cmd)
File "/usr/lib/python2.5/ftplib.py", line 241, in sendcmd
return self.getresp()
File "/usr/lib/python2.5/ftplib.py", line 216, in getresp
raise error_perm, resp
ftplib.error_perm: 522 Data connections must be encrypted.
It seems TLS is used only for handshake, not for transfers.
It there a way to secure the transfer (I'd like to upload files using storbinary()) using M2Crypto? If not, what are other alternatives?
Solution is to explicitly call for protected transfer after login():
ftp.prot_p()
Related
I am trying to upload a file using ftplib and python. I continue getting this error and don't know why...help?
Traceback (most recent call last):
File "C:\Users\212392169\Desktop\ftp_trial.py", line 15, in <module>
ftp.storbinary("STOR samplee.bin", f)
File "C:\Python27\lib\ftplib.py", line 471, in storbinary
conn = self.transfercmd(cmd, rest)
File "C:\Python27\lib\ftplib.py", line 376, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "C:\Python27\lib\ftplib.py", line 339, in ntransfercmd
resp = self.sendcmd(cmd)
File "C:\Python27\lib\ftplib.py", line 249, in sendcmd
return self.getresp()
File "C:\Python27\lib\ftplib.py", line 224, in getresp
raise error_perm, resp
ftplib.error_perm: 550 Can't open file "samplee.bin"
Source Code:
from ftplib import FTP
ftp = FTP(host)
ftp.login(host,pw)
ftp.cwd("/dflts")
f = open('samplee.bin', 'rb')
ftp.storbinary("STOR samplee.bin", f)
f.close()
ftp.quit()
That error means that you don't have permission to upload the file.
ftp.login(host, pw)
^^^^
Is the host really what you meant?
I have a code which downloads zip files from a ftp server. When I try to catch files on an empty directory, the code fails.
It works fine when I have files in the directory.
These errors appears when the directory is empty.
File "/usr/lib/python2.5/ftplib.py", line 459, in nlst
self.retrlines(cmd, files.append)
File "/usr/lib/python2.5/ftplib.py", line 421, in retrlines
return self.voidresp()
File "/usr/lib/python2.5/ftplib.py", line 221, in voidresp
resp = self.getresp()
File "/usr/lib/python2.5/ftplib.py", line 216, in getresp
raise error_perm, resp
ftplib.error_perm: 550 No files found.
I tried to fix this error with the next code, but it doesn't work. It keeps happening.
If somebody can help me with a solution, I'd appreciate it
CODE
try:
fileList = s.nlst()
except ftplib.error_perm, resp:
if str(resp) == '550 No files found':
print 'Directory is empty.'
else:
raise
which still results in
File "/root/folder/ftp.py", line 151, in download fileList = s.nlst()
File "/usr/lib/python2.5/ftplib.py", line 459, in nlst self.retrlines(cmd, files.append)
File "/usr/lib/python2.5/ftplib.py", line 421, in retrlines return self.voidresp()
File "/usr/lib/python2.5/ftplib.py", line 221, in voidresp resp = self.getresp()
File "/usr/lib/python2.5/ftplib.py", line 216, in getresp raise error_perm, resp
ftplib.error_perm: 550 No files found.
I know this is dead but I found this because I had the same problem. Here is the workaround I used:
def handle_folders(item):
global fldrs
temp = item.split()
if item.startswith('d') and temp[4] != "512":
fldrs.append(myFldr + item[54:] + "/")
ftp.cwd(myFldr)
ftp.retrlines('LIST', handle_folders)
setupDir = fldrs
where myFldr is the path and you filter out anything that isnt a folder and any empty ones. Elsewhere in my code I iterate through setupDir and use ftp.nlst() to get a list of files, though you could also use retrlines to get that list.
I want to get a list of folders in a ftp directory, but I'm getting the following error:
directory list of folder /myfolder
Traceback (most recent call last):
File "./run.py", line 12, in <module>
folderList = connection.nlst()
File "/usr/local/lib/python2.7/ftplib.py", line 506, in nlst
self.retrlines(cmd, files.append)
File "/usr/local/lib/python2.7/ftplib.py", line 429, in retrlines
conn = self.transfercmd(cmd)
File "/usr/local/lib/python2.7/ftplib.py", line 368, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "/usr/local/lib/python2.7/ftplib.py", line 331, in ntransfercmd
resp = self.sendcmd(cmd)
File "/usr/local/lib/python2.7/ftplib.py", line 244, in sendcmd
return self.getresp()
File "/usr/local/lib/python2.7/ftplib.py", line 219, in getresp
raise error_perm, resp
ftplib.error_perm: 502 Command not implemented
And here's the sorcecode:
#!/usr/bin/python
import ftplib
connection = ftplib.FTP("10.0.99.11")
#connection.set_pasv(False)
connection.login(user='abc', passwd='1234')
rootDir = "/myfolder"
connection.cwd(rootDir)
print "directory list of folder ",rootDir
#connection.retrlines('LIST')
folderList = connection.nlst()
for folderEntry in folderList:
print folderEntry
print "end of list"
# close connection
connection.quit()
Thanks for your help and any information or idea you have.
Update: see the comments for my solution.
My python code uses urllib2 to access a FTP server through a proxy with user and password. I use both a urllib2.ProxyHandler and a urllib2.ProxyBasicAuthHandler to implement this by following urllib2 examples:
1 import urllib2
2 proxy_host = 'host.proxy.org:3128' # only host name, no scheme (http/ftp)
3 proxy_handler = urllib2.ProxyHandler({'ftp': proxy_host})
4 proxy_auth_handler = urllib2.ProxyBasicAuthHandler()
5 proxy_auth_handler.add_password(None, proxy_host, proxy_user, proxy_passwd)
6 opener_thru_proxy = urllib2.build_opener(proxy_handler, proxy_auth_handler)
7 conn = opener_thru_proxy.open('ftp://ftp.ftpserver.org/targetfile.txt')
8 print conn.read()
The code stops at line 7.
error messages are:
conn = urllib2.urlopen(remote_fn)
File "/usr/lib/python2.6/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.6/urllib2.py", line 391, in open
response = self._open(req, data)
File "/usr/lib/python2.6/urllib2.py", line 409, in _open
'_open', req)
File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain
result = func(*args)
File "/usr/lib/python2.6/urllib2.py", line 1344, in ftp_open
fw = self.connect_ftp(user, passwd, host, port, dirs, req.timeout)
File "/usr/lib/python2.6/urllib2.py", line 1365, in connect_ftp
fw = ftpwrapper(user, passwd, host, port, dirs, timeout)
File "/usr/lib/python2.6/urllib.py", line 856, in __init__
self.init()
File "/usr/lib/python2.6/urllib.py", line 862, in init
self.ftp.connect(self.host, self.port, self.timeout)
File "/usr/lib/python2.6/ftplib.py", line 134, in connect
self.welcome = self.getresp()
File "/usr/lib/python2.6/ftplib.py", line 209, in getresp
resp = self.getmultiline()
File "/usr/lib/python2.6/ftplib.py", line 195, in getmultiline
line = self.getline()
File "/usr/lib/python2.6/ftplib.py", line 185, in getline
if not line: raise EOFError
urllib2.URLError: <urlopen error ftp error: >
I have tested the proxy server by defining env variable ftp_proxy and access ftp file using
wget --proxy-user=proxy_user --proxy-password=proxy_passwd ftp://ftp.ftpserver.org/targetfile.txt
and it successfully downloaded file from the ftp server.
How can I improve my code to access the ftp site using proxy with authentication?
Thanks for your help in advance!
I want to get contents of a website, so I use urllib.urlopen(url).
set url='http://localhost:8080'(tomcat page)
If I use Google App Engine Launcher, run the application, browse http://localhost:8082 , it works well.
But if I specify the address and port for the application:
python `"D:\Program Files\Google\google_appengine\dev_appserver.py" -p 8082 -a 10.96.72.213 D:\pagedemon\videoareademo`
there's something wrong:
Traceback (most recent call last):
File "D:\Program Files\Google\google_appengine\google\appengine\ext\webapp\_webapp25.py", line 701, in __call__
handler.get(*groups)
File "D:\pagedemon\videoareademo\home.py", line 76, in get
wp = urllib.urlopen(url)
File "C:\Python27\lib\urllib.py", line 84, in urlopen
return opener.open(url)
File "C:\Python27\lib\urllib.py", line 205, in open
return getattr(self, name)(url)
File "C:\Python27\lib\urllib.py", line 343, in open_http
errcode, errmsg, headers = h.getreply()
File "D:\Program Files\Google\google_appengine\google\appengine\dist\httplib.py", line 334, in getreply
response = self._conn.getresponse()
File "D:\Program Files\Google\google_appengine\google\appengine\dist\httplib.py", line 222, in getresponse
deadline=self.timeout)
File "D:\Program Files\Google\google_appengine\google\appengine\api\urlfetch.py", line 263, in fetch
return rpc.get_result()
File "D:\Program Files\Google\google_appengine\google\appengine\api\apiproxy_stub_map.py", line 592, in get_result
return self.__get_result_hook(self)
File "D:\Program Files\Google\google_appengine\google\appengine\api\urlfetch.py", line 365, in _get_fetch_result
raise DownloadError(str(err))
DownloadError: ApplicationError: 2 [Errno 11003] getaddrinfo failed
The strangest thing is when I change the url form "http://localhost:8080" to "http://127.0.0.1:8080", it works well!
I googled a lot, but I didn't find any good solutions.Hoping for some help!
Also, I didn't configure any proxy.IE works well.
Your system doesn't necessarily know that localhost should resolve to 127.0.0.1. You might need to put an entry into your hosts file. On Windows, it's located at C:\Windows\System32\drivers\etc\hosts