ftp a tar file using python - python-2.7

I am trying to copy a file from remote server to my local machine using FTP and i have written a python script for it.:
import os
from ftplib import FTP
btsIp=raw_input("Enter the IP:")
try:
ftp=FTP(btsIp,user='',passwd='',timeout=20)
except :
print("FTP Connection failed")
sys.exit(1)
ftp.cwd('/store/slv_imt')
filenames=ftp.nlst()
for eachFile in filenames:
localName=os.path.join('D:\Users\shaik-s\Documents\SLV_logs',eachFile)
file=open(localName,'w')
ftp.retrbinary('RETR' + eachFile,file.write)
file.close()
ftp.quit()
However, i am getting error, as command not understood:
slv_imt_2016-07-13__00-00-34.tar
Traceback (most recent call last):
File "C:/Python27/Scripts/ftp.py", line 16, in <module>
ftp.retrbinary('RETR' + eachFile,file.write)
File "C:\Python27\lib\ftplib.py", line 414, in retrbinary
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
error_perm: 500 'RETRSLV_IMT_2016-07-13__00-00-34.TAR': command not understood.
Kindly help..

To fix the error, you need to add a space between "RETR" and "eachFile".
ftp.retrbinary('RETR %s'%eachFile, file.write)
But your code shows that you may encounter another problem, try to use the following cmd.
file = open(localName, 'wb')

Related

urllib IOError: [Errno socket error] [Errno 54] Connection reset by peer

I'm trying to run my python code and it shows the following:
IOError: [Errno socket error] [Errno 54] Connection reset by peer
Here is my code:
import urllib
def dl_jpg(url, file_path, file_name):
full_path = file_path + file_name + '.jpg'
urllib.urlretrieve(url, full_path)
url = 'https://xxxxx'
file_name = '1'
dl_jpg(url, 'image/', file_name)
It works fine before but doesn't work now. I'm not sure about the cause, the only difference I can think of is I updated my macOS. I am new to coding and still very confused after googled. I guess there are some issues in the ssl.
I now have
macOS 10.14.3
LibreSSL 2.6.5
Python 2.7.10
Any idea? Thank you so much!
Traceback (most recent call last):
File "img3.py", line 10, in <module>
dl_jpg(url, 'image3/', file_name)
File "img3.py", line 5, in dl_jpg
urllib.urlretrieve(url, full_path)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 98, in urlretrieve
return opener.retrieve(url, filename, reporthook, data)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 245, in retrieve
fp = self.open(url, data)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 213, in open
return getattr(self, name)(url)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 443, in open_https
h.endheaders(data)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1049, in endheaders
self._send_output(message_body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 893, in _send_output
self.send(msg)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 855, in send
self.connect()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1274, in connect
server_hostname=server_hostname)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 352, in wrap_socket
_context=self)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 579, in __init__
self.do_handshake()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 808, in do_handshake
self._sslobj.do_handshake()
Unfortunately, this is a known issue with python 2.7.10 which comes pre-installed with Mac OS. If you can upgrade your version of python and use it to run your script, you will find that it will work again.

Selenium doesn't open the browser in Python

I'm new in Python and I'm trying to use Selenium in Debian but it doesn't work, more concretely it seems to stay in a loop and nothing happens. The next script is the test that I've used:
#!/usr/bin/env python
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.python.org')
When I interrupt the script the following text appears:
Traceback (most recent call last):
File "prueba_parseo.py", line 7, in browser =
webdriver.Firefox() File
"/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py",
line 154, in init
keep_alive=True)
File
"/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py",
line 140, in init
self.start_session(desired_capabilities, browser_profile)
File
"/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py",
line 229, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File
"/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py",
line 295, in execute
response = self.command_executor.execute(driver_command, params)
File
"/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/remote_connection.py",
line 464, in execute
return self._request(command_info[0], url, body=data)
File
"/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/remote_connection.py",
line 488, in _request
resp = self._conn.getresponse()
File "/usr/lib/python2.7/httplib.py", line 1111, in getresponse
response.begin()
File "/usr/lib/python2.7/httplib.py", line 444, in begin
version, status, reason = self._read_status()
File "/usr/lib/python2.7/httplib.py", line 400, in _read_status
line = self.fp.readline(_MAXLINE + 1)
File "/usr/lib/python2.7/socket.py", line 476, in readline
data = self._sock.recv(self._rbufsize)
KeyboardInterrupt
I've been searching for an answer, but nothing works. I've changed the versions of the packages, export no_proxy="localhost,127.0.0.1"
OS: Debian 5
Python: 2.7
Selenium: 3.5
Geckodriver: 0.17.0
Firefox: 52.0
I don't know what else to do or what to change.
Thank you very much!
My guess is that it actually all goes well and the browser is started in background. The reason why it's staying open is probably because of the default option keep_alive=True I can see in your traceback.
Try closing the browser using browser.close() or browser.quit() when you are done with the tests.
From the documentation:
Finally, the browser window is closed. You can also call quit method
instead of close. The quit will exit entire browser whereas close`
will close one tab, but if just one tab was open, by default most
browser will exit entirely.
http://selenium-python.readthedocs.io/getting-started.html#simple-usage

IOError: [Errno socket error] [Errno 1] _ssl.c:507: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol

below is my code i am using to scrap some web page
i am using python in windows 8
import urllib
import re
htmlfile = urllib.urlopen("https://finance.yahoo.com/q?s=aapl&ql=1")
htmltext = htmlfile.read()
regex='<span id="yfs_l84_aapl">(.+?)</span>'
pattern = re.compile(regex)
price = re.findall(pattern,htmltext)
print price
error i am getting
Traceback (most recent call last):
File "C:\Python27\web.py", line 4, in
htmlfile = urllib.urlopen("https://finance.yahoo.com/q?s=aapl&ql=1")
File "C:\Python27\lib\urllib.py", line 87, in urlopen
return opener.open(url)
File "C:\Python27\lib\urllib.py", line 208, in open
return getattr(self, name)(url)
File "C:\Python27\lib\urllib.py", line 437, in open_https
h.endheaders(data)
File "C:\Python27\lib\httplib.py", line 969, in endheaders
self._send_output(message_body)
File "C:\Python27\lib\httplib.py", line 829, in _send_output
self.send(msg)
File "C:\Python27\lib\httplib.py", line 791, in send
self.connect()
File "C:\Python27\lib\httplib.py", line 1176, in connect
self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file)
File "C:\Python27\lib\ssl.py", line 387, in wrap_socket
ciphers=ciphers)
File "C:\Python27\lib\ssl.py", line 143, in init
self.do_handshake()
File "C:\Python27\lib\ssl.py", line 305, in do_handshake
self._sslobj.do_handshake()
IOError: [Errno socket error] [Errno 1] _ssl.c:507: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
how to debug this error
use inspect element > go to console > check if any errors in script > manage them > if problem still persists go to network option > now reload the page and check the flow of information > and rebuilt where info. flow is stopped or unexpected.
hope this will solve your problem.
i found an another answer my self
use inspect element > go to console > check if any errors in script > manage them > if problem still persists go to network option > now reload the page and check the flow of information > and rebuilt where info. flow is stopped or unexpected.

how to use envoy package in python?

I have installed package envoy. I ran the script but a windows error occured .I commented envoy.run then the full script runs but when I remove the comment, error occurs.
import envoy
# This data is checked-in to the repository and is a compressed
# version of the output from Example 3
F = 'resources/ch06-mailboxes/data/enron.mbox.json.bz2'
r = envoy.run("bunzip2 %s" % (F,))
print r.std_out
print r.std_err
traceback of script:
Exception in thread Thread-9:
Traceback (most recent call last):
File "C:\Users\sachin\Anaconda\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:\Users\sachin\Anaconda\lib\threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File "C:\Users\sachin\Anaconda\lib\site-packages\envoy\core.py", line 40, in target
bufsize=0,
File "C:\Users\sachin\Anaconda\lib\subprocess.py", line 709, in __init__
errread, errwrite)
File "C:\Users\sachin\Anaconda\lib\subprocess.py", line 957, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
Please try this:
F = os.path.join(os.getcwd(), 'resources/ch06-mailboxes/data/enron.mbox.json.bz2')

Python Transfering files to and from the remote machine

i want to transfer a file local to server machine.
import os
import paramiko
local_path = "/home/e100075/python/ss.txt"
remote_path = "/home/developers/screenshots/"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('hostname', username="username", password="password")
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('ls /tmp')
print "output", ssh_stdout.read()
#Reading output of the executed command
error = ssh_stderr.read()
#Reading the error stream of the executed command
print "err", error, len(error)
#Transfering files to and from the remote machine
sftp = ssh.open_sftp()
sftp.get(remote_path, local_path)
sftp.put(local_path, remote_path)
sftp.close()
ssh.close()
After run the program. I got the errors below
Traceback (most recent call last):
File "file_copy.py", line 21, in <module>
sftp.get(remote_path, local_path)
File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 606, in get
fr = self.file(remotepath, 'rb')
File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 245, in open
t, msg = self._request(CMD_OPEN, filename, imode, attrblock)
File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 635, in _request
return self._read_response(num)
File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 682, in
_read_response
self._convert_status(msg)
File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 708, in
_convert_status
raise IOError(errno.ENOENT, text)
IOError: [Errno 2] No such file
If you know the answer. please let me know.
Thanks for reading.
If you want to put a file to the server you probably will need
sftp.put(local_path, remote_path)
not
sftp.get(remote_path, local_path)
You need to give file name also, otherwise it will throw errors
sftp.put('filename.txt','/home/user/performance/destination.txt')
where 'filename.txt' must be in current directory or else give full path