python paramiko module error with callback - python

I'm trying to use the paramiko module to copy a (big) file in my local network, and get the output to display a GtkProgressBar.
A part of my code is:
...
NetworkCopy.pbar.set_text("Copy of the file in the Pi...")
while gtk.events_pending(): # refresh the progress bar
gtk.main_iteration()
self.connection(transferred, toBeTransferred)
def connection(self, transferred, toBeTransferred):
sftp = self.sftp
fichier_pc = self.fichier_pc
chemin_pi = self.chemin_pi # var names are in french !
fichier = self.fichier
transferred = self.transferred
toBeTransferred = self.toBeTransferred
print "Transferred: {0}\tStill to send: {1}".format(transferred, toBeTransferred)
sftp.put(fichier_pc, chemin_pi + fichier, callback=self.connection)
In the terminal, I can see
Transferred: 0 Still to send: 3762398252
for a while, but after 10s I have this error:
File "network_copier.py", line 158, in connection
sftp.put(fichier_pc, chemin_pi + fichier, callback=self.connection)
File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 615, in put
return self.putfo(fl, remotepath, os.stat(localpath).st_size, callback, confirm)
File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 577, in putfo
fr.close()
File "/usr/lib/python2.7/dist-packages/paramiko/sftp_file.py", line 67, in close
self._close(async=False)
File "/usr/lib/python2.7/dist-packages/paramiko/sftp_file.py", line 88, in _close
self.sftp._request(CMD_CLOSE, self.handle)
File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 689, in _request
return self._read_response(num)
File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 721, in _read_response
raise SSHException('Server connection dropped: %s' % (str(e),))
paramiko.SSHException: Server connection dropped:
I have the 1.12.2 version of paramiko, from this ppa
Thanks for your help
Edit: The solution is to use pexpect instead of paramiko. It's working with big files.
See here

Related

TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time Email-Listener

I'm trying to create a Python code for automatically listen any incoming email then download attachment of that email. I found Email-Listener library and practice the documentation. I'm using the exact code from the documentation. This is the code:
import email_listener
# Set your email, password, what folder you want to listen to, and where to save attachments
email = "example#gmail.com"
app_password = "password"
folder = "Inbox"
attachment_dir = "/path/to/attachments"
el = email_listener.EmailListener(email, app_password, folder, attachment_dir)
# Log into the IMAP server
el.login()
# Get the emails currently unread in the inbox
messages = el.scrape()
print(messages)
# Start listening to the inbox and timeout after an hour
timeout = 60
el.listen(timeout)
Well it works, when program running it will listen and download any attachment from unread email, either already exist on inbox or not. But after about 15+ minutes the program will stopped and display this error message:
Traceback (most recent call last):
File "app.py", line 20, in <module>
el.listen(timeout)
File "D:\PreProject\email_auto\emailauto_env\lib\site-packages\email_listener\__init__.py", line 333, in listen
self.__idle(process_func=process_func, **kwargs)
File "D:\PreProject\email_auto\emailauto_env\lib\site-packages\email_listener\__init__.py", line 385, in __idle
self.server.idle_done()
File "D:\PreProject\email_auto\emailauto_env\lib\site-packages\imapclient\imapclient.py", line 175, in wrapper
return func(client, *args, **kwargs)
File "D:\PreProject\email_auto\emailauto_env\lib\site-packages\imapclient\imapclient.py", line 975, in idle_done
return self._consume_until_tagged_response(self._idle_tag, "IDLE")
File "D:\PreProject\email_auto\emailauto_env\lib\site-packages\imapclient\imapclient.py", line 1596, in _consume_until_tagged_response
line = self._imap._get_response()
File "c:\users\0974\appdata\local\programs\python\python37\lib\imaplib.py", line 1047, in _get_response
resp = self._get_line()
File "c:\users\0974\appdata\local\programs\python\python37\lib\imaplib.py", line 1149, in _get_line
line = self.readline()
File "D:\PreProject\email_auto\emailauto_env\lib\site-packages\imapclient\tls.py", line 59, in readline
return self.file.readline()
File "c:\users\0974\appdata\local\programs\python\python37\lib\socket.py", line 589, in readinto
return self._sock.recv_into(b)
File "c:\users\0974\appdata\local\programs\python\python37\lib\ssl.py", line 1071, in recv_into
return self.read(nbytes, buffer)
File "c:\users\0974\appdata\local\programs\python\python37\lib\ssl.py", line 929, in read
return self._sslobj.read(len, buffer)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Is there any solutions? And I don't mind if the solutions use other methods, like not using Email-Listener library

Can't upload file to sftp server with Paramiko Python [duplicate]

This question already has an answer here:
Uploading file via Paramiko SFTP not working
(1 answer)
Closed 2 years ago.
I want to upload a file to a SFTP server using Paramiko, but I don't seem to get it right.
I thought I got the respective error cause of the file path '/' witch is the root on the server but I didn't get anywhare.
I keep getting this error:
File "c:\python\whitelistBot\whitelistBot.py", line 63, in on_message
sftp_client.put('C:/python/whitelistBot/whitelist1.json', '/')
File "C:\python\whitelistBot\lib\site-packages\paramiko\sftp_client.py", line 759, in put
return self.putfo(fl, remotepath, file_size, callback, confirm)
File "C:\python\whitelistBot\lib\site-packages\paramiko\sftp_client.py", line 714, in putfo
with self.file(remotepath, "wb") as fr:
File "C:\python\whitelistBot\lib\site-packages\paramiko\sftp_client.py", line 372, in open
t, msg = self._request(CMD_OPEN, filename, imode, attrblock)
File "C:\python\whitelistBot\lib\site-packages\paramiko\sftp_client.py", line 813, in _request
return self._read_response(num)
File "C:\python\whitelistBot\lib\site-packages\paramiko\sftp_client.py", line 865, in _read_response
self._convert_status(msg)
File "C:\python\whitelistBot\lib\site-packages\paramiko\sftp_client.py", line 898, in _convert_status
raise IOError(text)
OSError: Operation Unsupported
The code I'm running:
import paramiko
FTP_HOST = "********"
FTP_USER = "********"
FTP_PASS = "********"
FTP_PORT = ****
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname= FTP_HOST, username=FTP_USER, password=FTP_PASS, port=FTP_PORT)
sftp_client = ssh_client.open_sftp()
sftp_client.put('C:/python/whitelistBot/whitelist1.json', '/')
sftp_client.close()
ssh_client.close()
EDIT: I changed the directory where to put the file to "/home/container" cause that's what they told me in the site but except for the fact that I don't get that error message, the file does not appear on the server
Looks like I had to specify the name of the file '/whitelist1.json' like so:
sftp_client.put('C:/python/whitelistBot/whitelist1.json', '/whitelist1.json') # don't know why but yeah

Downloading second file from ftp fails

I want to download multiple files from FTP in python. the my code works when I just download 1 file, but not works for more than one!
import urllib
urllib.urlretrieve('ftp://ftp.ncbi.nlm.nih.gov/pub/pmc/oa_package/00/00/PMC1790863.tar.gz', 'file1.tar.gz')
urllib.urlretrieve('ftp://ftp.ncbi.nlm.nih.gov/pub/pmc/oa_package/00/00/PMC2329613.tar.gz', 'file2.tar.gz')
An error say:
Traceback (most recent call last):
File "/home/ehsan/dev_center/bigADEVS-bknd/daemons/crawler/ftp_oa_crawler.py", line 3, in <module>
urllib.urlretrieve('ftp://ftp.ncbi.nlm.nih.gov/pub/pmc/oa_package/00/00/PMC2329613.tar.gz', 'file2.tar.gz')
File "/usr/lib/python2.7/urllib.py", line 98, in urlretrieve
return opener.retrieve(url, filename, reporthook, data)
File "/usr/lib/python2.7/urllib.py", line 245, in retrieve
fp = self.open(url, data)
File "/usr/lib/python2.7/urllib.py", line 213, in open
return getattr(self, name)(url)
File "/usr/lib/python2.7/urllib.py", line 558, in open_ftp
(fp, retrlen) = self.ftpcache[key].retrfile(file, type)
File "/usr/lib/python2.7/urllib.py", line 906, in retrfile
conn, retrlen = self.ftp.ntransfercmd(cmd)
File "/usr/lib/python2.7/ftplib.py", line 334, in ntransfercmd
host, port = self.makepasv()
File "/usr/lib/python2.7/ftplib.py", line 312, in makepasv
host, port = parse227(self.sendcmd('PASV'))
File "/usr/lib/python2.7/ftplib.py", line 830, in parse227
raise error_reply, resp
IOError: [Errno ftp error] 200 Type set to I
What should I do?
It is a bug in urllib in python 2.7. Reported here. The reason behind the same is explained here
Now, when a user tries to download the same file or another file from
same directory, the key (host, port, dirs) remains the same so
open_ftp() skips ftp initialization. Because of this skipping,
previous FTP connection is reused and when new commands are sent to
the server, server first sends the previous ACK. This causes a domino
effect and each response gets delayed by one and we get an exception
from parse227()
A possible solution is to clear the cache that may have been built up by previous calls. You may use the urllib.urlcleanup() method calls between your urlretrieve calls for the same, as mentioned here.
Hope this helps!

Python FTP download not working

I am trying to download a file from FTP using python. I was able to successfully move into the directory but can't download the file.
The command I use is ftp.retrbinary('master.idx', open(fname,'wb').write)
And error is below. It looks like the command is looking for MASTER.IDX instead of master.idx
The full path to the file I want to download is ftp://ftp.sec.gov/edgar/full-index/2011/QTR2/master.idx
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/ftplib.py", line 406, in retrbinary
conn = self.transfercmd(cmd, rest)
File "/usr/lib/python2.7/ftplib.py", line 368, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "/usr/lib/python2.7/ftplib.py", line 331, in ntransfercmd
resp = self.sendcmd(cmd)
File "/usr/lib/python2.7/ftplib.py", line 244, in sendcmd
return self.getresp()
File "/usr/lib/python2.7/ftplib.py", line 219, in getresp
raise error_perm, resp
ftplib.error_perm: 500 MASTER.IDX not understood
I can't say why the name changes to uppercase. In any case, when using FTP, I make like this, it may help you:
server = "URL.of.server"
directory = "directory/where/the/file/is"
filename = "nameoffile.txt"
from ftplib import FTP
ftp = FTP(server) #Set server address
ftp.login() # Connect to server
ftp.cwd(directory) # Move to the desired folder in server
ftp.retrbinary('RETR ' + filename,open(filename, 'wb').write) # Download file from server
ftp.close() # Close connection
I think that it may be the 'RETR ', if you don't write, it the server may not understand what you want to do
use wget module of python instead. Here is an example snippet
import wget
fileloc = '/path/to/the/file/foo.txt'
wget.download(fileloc)

'put' in SFTP using Paramiko

I've installed and written the following Paramiko which is unable to put the file. It is easily able to 'get' a file and execute ls commands on it.
#set username & password
username='runaway'
password='runaway'
port=22
source= '/Unzip.sh'
destination ='/var/mpx/www/http'
#SFTP
client.load_system_host_keys()
print " hostname =%s \n username=%s \n password=%s \n" (hostname,username,password)
t = paramiko.Transport((hostname, port))
t.connect(username=username,password=password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put(source,destination)
#sftp.close()
#t.close()
Using a 'put' command gives the following error & stack trace -
File "upload_file.py", line 84, in ?
sftp.put(source,destination)
File "/usr/lib/python2.4/site-packages/paramiko/sftp_client.py", line 522, in put
fr = self.file(remotepath, 'wb')
File "/usr/lib/python2.4/site-packages/paramiko/sftp_client.py", line 221, in open
t, msg = self._request(CMD_OPEN, filename, imode, attrblock)
File "/usr/lib/python2.4/site-packages/paramiko/sftp_client.py", line 572, in _request
return self._read_response(num)
File "/usr/lib/python2.4/site-packages/paramiko/sftp_client.py", line 619, in _read_response
self._convert_status(msg)
File "/usr/lib/python2.4/site-packages/paramiko/sftp_client.py", line 649, in _convert_status
raise IOError(text)
IOError: Failure
How do I overcome this?
The solution seemed very funny to me!
source= '/Unzip.sh'
destination ='/var/mpx/www/http/Unzip.sh'
Just modified the destination path to include the file name as well.
Didn't expect some error like this coming from a Python package.
This also occurs in 2.0.2 when you try to sftp.mkdir('/exists'):
Traceback (most recent call last):
...
File "/usr/local/lib/python2.7/site-packages/paramiko/sftp_client.py", line 380, in mkdir
self._request(CMD_MKDIR, path, attr)
File "/usr/local/lib/python2.7/site-packages/paramiko/sftp_client.py", line 730, in _request
return self._read_response(num)
File "/usr/local/lib/python2.7/site-packages/paramiko/sftp_client.py", line 781, in _read_response
self._convert_status(msg)
File "/usr/local/lib/python2.7/site-packages/paramiko/sftp_client.py", line 811, in _convert_status
raise IOError(text)
IOError: Failure
This was my Python 2.7.9 fix:
try:
sftp.mkdir(remote_dir)
except IOError:
logging.debug('%s already exists.', remote_dir)

Categories