Can't open file error uploading file via FTPLIB and Python - 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?

Related

how to solving error in uploading file via ftp in python

i have this code in python2.7 for upload photos.zip file on the server via ftp.
import ftplib
session = ftplib.FTP('server.address.com','USERNAME','PASSWORD')
file = open('c:\archived\photos.zip','rb') # file to send
session.storbinary('STOR photos.zip', file) # send the file
file.close() # close file and FTP
session.quit()
but i have this error :
raceback (most recent call last):
File "a.py", line 24, in <module>
file = open('c:\archived\photos.zip','rb')
IOError: [Errno 22] invalid mode ('rb') or filename: 'c:\archived\photos.zip'
also I used this solution :
file = open(os.path.join('c:/','archived','photos.zip'),'rb')
but I get this error :
Traceback (most recent call last):
File "s.py", line 28, in <module>
session.storbinary('s.zip', file)
File "C:\Users\0xuser\Anaconda2\lib\ftplib.py", line 479, in storbinary
conn = self.transfercmd(cmd, rest)
File "C:\Users\0xuser\Anaconda2\lib\ftplib.py", line 378, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "C:\Users\0xuser\Anaconda2\lib\ftplib.py", line 341, in ntransfercmd
resp = self.sendcmd(cmd)
File "C:\Users\0xuser\Anaconda2\lib\ftplib.py", line 251, in sendcmd
return self.getresp()
File "C:\Users\0xuser\Anaconda2\lib\ftplib.py", line 226, in getresp
raise error_perm, resp
ftplib.error_perm: 500 Unknown command.
Try using forward slashes :
file = open('c:/archived/photos.zip','rb') # file to send

Pyftpsync works only without TLS

Hi I'm using pyftpsync in order to upload new images (downloaded every day) in a online store skipping the existing ones.
From the author site I'm using this code (with my personal data of course):
from ftpsync.targets import FsTarget
from ftpsync.ftp_target import FtpTarget
from ftpsync.synchronizers import UploadSynchronizer
local = FsTarget("~/temp")
user ="joe"
passwd = "secret"
remote = FtpTarget("/temp", "example.com", username=user, password=passwd)
opts= {"force": False, "delete_unmatched": True, "verbose": 3, "dry_run" : False}
s = UploadSynchronizer(local, remote, opts,tls=True)
s.run()
This work for me correctly only if I delete the tls option, otherwise I obtain this error while connecting to my server:
Traceback (most recent call last):
File "sync.py", line 25, in <module>
s.run()
File "/media/luca/1E9C3E629C3E349D/luca/Documenti/attività/Cataloghi2/ftpsync/synchronizers.py", line 139, in run
res = self._sync_dir()
File "/media/luca/1E9C3E629C3E349D/luca/Documenti/attività/Cataloghi2/ftpsync/synchronizers.py", line 416, in _sync_dir
remote_entries = self.remote.get_dir()
File "/media/luca/1E9C3E629C3E349D/luca/Documenti/attività/Cataloghi2/ftpsync/ftp_target.py", line 310, in get_dir
self.ftp.retrlines("MLSD", _addline)
File "/usr/lib/python3.4/ftplib.py", line 466, in retrlines
resp = self.sendcmd('TYPE A')
File "/usr/lib/python3.4/ftplib.py", line 272, in sendcmd
return self.getresp()
File "/usr/lib/python3.4/ftplib.py", line 235, in getresp
resp = self.getmultiline()
File "/usr/lib/python3.4/ftplib.py", line 221, in getmultiline
line = self.getline()
File "/usr/lib/python3.4/ftplib.py", line 209, in getline
raise EOFError
EOFError
Could not remove lock file: Underlying socket connection gone (_ssl.c:1570)
Exception ignored in: <bound method FtpTarget.__del__ of <ftpsync.ftp_target.FtpTarget object at 0x7fe397ae7710>>
Traceback (most recent call last):
File "/media/luca/1E9C3E629C3E349D/luca/Documenti/attività/Cataloghi2/ftpsync/targets.py", line 70, in __del__
File "/media/luca/1E9C3E629C3E349D/luca/Documenti/attività/Cataloghi2/ftpsync/ftp_target.py", line 153, in close
File "/media/luca/1E9C3E629C3E349D/luca/Documenti/attività/Cataloghi2/ftpsync/ftp_target.py", line 187, in _unlock
File "/usr/lib/python3.4/ftplib.py", line 613, in delete
File "/usr/lib/python3.4/ftplib.py", line 271, in sendcmd
File "/usr/lib/python3.4/ftplib.py", line 198, in putcmd
File "/usr/lib/python3.4/ftplib.py", line 193, in putline
File "/usr/lib/python3.4/ssl.py", line 720, in sendall
File "/usr/lib/python3.4/ssl.py", line 681, in send
ssl.SSLError: Underlying socket connection gone (_ssl.c:1570)
It's obviously a problem related to the encryption, but I don't know how to fix it.

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.

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