Python: nlst error. Empty directory - python

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.

Related

Can't open file error uploading file via FTPLIB and Python

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?

ftplib.error_perm: 502 Command not implemented (python)

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.

Google App Engine throws this InvalidURL("nonnumeric port: '%s'" % host[i+1:]) exception when I try to deploy. How do I fix it?

When I click the Deploy button for my project, it gives me the following error: File "C:\Python27\lib\httplib.py", line 721, in _set_hostport
raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
httplib.InvalidURL: nonnumeric port: 'port'.
I've successfully deployed this project before, so I tried reverting the files so that I would have the same files when it worked properly. No dice, it seems. That was just a jab in the dark, though -- the full trace shows this starting in Google App Engine's files, not mine.
Anyone know how I can deploy my project?
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\appcfg.py", line 171, in <module>
run_file(__file__, globals())
File "C:\Program Files (x86)\Google\google_appengine\appcfg.py", line 167, in run_file
execfile(script_path, globals_)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\appcfg.py", line 4191, in <module>
main(sys.argv)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\appcfg.py", line 4182, in main
result = AppCfgApp(argv).Run()
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\appcfg.py", line 2579, in Run
self.action(self)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\appcfg.py", line 3927, in __call__
return method()
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\appcfg.py", line 3040, in Update
updatecheck.CheckForUpdates()
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\appcfg.py", line 596, in CheckForUpdates
runtime=self.config.runtime)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\appengine_rpc.py", line 383, in Send
f = self.opener.open(req)
File "C:\Python27\lib\urllib2.py", line 400, in open
response = self._open(req, data)
File "C:\Python27\lib\urllib2.py", line 418, in _open
'_open', req)
File "C:\Python27\lib\urllib2.py", line 378, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 1215, in https_open
return self.do_open(httplib.HTTPSConnection, req)
File "C:\Program Files (x86)\Google\google_appengine\lib\fancy_urllib\fancy_urllib\__init__.py", line 370, in do_open
req)
File "C:\Python27\lib\urllib2.py", line 1146, in do_open
h = http_class(host, timeout=req.timeout) # will parse host:port
File "C:\Program Files (x86)\Google\google_appengine\lib\fancy_urllib\fancy_urllib\__init__.py", line 78, in __init__
httplib.HTTPSConnection.__init__(self, *args, **kwargs)
File "C:\Python27\lib\httplib.py", line 1149, in __init__
source_address)
File "C:\Python27\lib\httplib.py", line 693, in __init__
self._set_hostport(host, port)
File "C:\Python27\lib\httplib.py", line 721, in _set_hostport
raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
httplib.InvalidURL: nonnumeric port: 'port'
2012-10-22 16:55:29 (Process exited with code 1)
You can close this window now.
I'll preface this by saying I'm not familiar with the GUI, so feel free to completely disregard :).
The error seems to indicate that you are trying to specify a particular port for deployment, and furthermore, the value of that setting is the word 'port'. When using the commnand line with the dev server, port is an argument you can use to specify a particular port to use; my best guess is that this keyword parameter is still defined (as port) when you are trying to deploy. Is there a section where you can specify command-line flags? Is it possible that port is defined there when deploying?
I've resolved this by completely uninstalling and reinstalling both Python 2.7 and GAE. This complete wipe should include the deletion of any environment variables that have been set since your usage of GAE.
In my case, I think it was particularly the creation of environment proxy variables. A complete wipe of Python and GAE resolved this.

How to upload a file on FTPS server using m2crypto

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()

FancyURLOpener failing since moving to python 3.1.2

I had an application that was downloading a .CSV file from a password-protected website then processing it futher.
I was using FancyURLOpener, and simply hardcoding the username and password. (Obviously, security is not a high priority in this particular instance).
Since downloading Python 3.1.2, this code has stopped working. After fixing the obvious issue of it now being in the "request" namespace, it's crashing in a less obvious way.
Does anyone know of the changes that have happened to the implementation, and how to use it now? The documentation seems to be short of examples.
Here is a cut down version of the code:
import urllib.request;
class TracOpener (urllib.request.FancyURLopener) :
def prompt_user_passwd(self, host, realm) :
return ('andrew_ee', '_my_unenctryped_password')
csvUrl='http://mysite/report/19?format=csv#USER=fred_nukre'
opener = TracOpener();
f = opener.open(csvUrl); # This is failing!
s = f.read();
f.close();
s;
For the sake of completeness, here's the entire call stack:
Traceback (most recent call last):
File "C:\reporting\download_csv_file.py", line 12, in <module>
f = opener.open(csvUrl);
File "C:\Program Files\Python31\lib\urllib\request.py", line 1454, in open
return getattr(self, name)(url)
File "C:\Program Files\Python31\lib\urllib\request.py", line 1628, in open_http
return self._open_generic_http(http.client.HTTPConnection, url, data)
File "C:\Program Files\Python31\lib\urllib\request.py", line 1624, in _open_generic_http
response.status, response.reason, response.msg, data)
File "C:\Program Files\Python31\lib\urllib\request.py", line 1640, in http_error
result = method(url, fp, errcode, errmsg, headers)
File "C:\Program Files\Python31\lib\urllib\request.py", line 1878, in http_error_401
return getattr(self,name)(url, realm)
File "C:\Program Files\Python31\lib\urllib\request.py", line 1950, in retry_http_basic_auth
return self.open(newurl)
File "C:\Program Files\Python31\lib\urllib\request.py", line 1454, in open
return getattr(self, name)(url)
File "C:\Program Files\Python31\lib\urllib\request.py", line 1628, in open_http
return self._open_generic_http(http.client.HTTPConnection, url, data)
File "C:\Program Files\Python31\lib\urllib\request.py", line 1590, in _open_generic_http
auth = base64.b64encode(user_passwd).strip()
File "C:\Program Files\Python31\lib\base64.py", line 56, in b64encode
raise TypeError("expected bytes, not %s" % s.__class__.__name__)
TypeError: expected bytes, not str
It's a known bug: http://bugs.python.org/issue8123

Categories