This question already has an answer here:
Accessing FTP server with Python fails with "getaddrinfo" error
(1 answer)
Closed 2 years ago.
I am trying to download all the files present in a directory using a simple python code, I used the ftplib but I am unable to get proper results.
from ftplib import FTP
import os
folder='D:\\New folder'
os.chdir(folder)
ftp = FTP('http://mtb.dobzhanskycenter.org/VCF/')
ftp.login()
vcffiles = ftp.nlst()
for files in vcffiles:
local_filename = os.path.join('D:\\New folder', files)
file = open(local_filename, 'wb')
ftp.retrbinary('RETR '+ files, file.write)
file.close()
ftp.quit()
Error:-
Traceback (most recent call last):
File "D:/Python Class/url.py", line 5, in <module>
ftp = FTP('http://mtb.dobzhanskycenter.org/VCF/')
File "C:\Program Files\Python37\lib\ftplib.py", line 117, in __init__
self.connect(host)
File "C:\Program Files\Python37\lib\ftplib.py", line 152, in connect
source_address=self.source_address)
File "C:\Program Files\Python37\lib\socket.py", line 707, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Program Files\Python37\lib\socket.py", line 748, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
The first argument of FTP constructor is host – a hostname or an IP address – not an URL (let alone HTTP URL – It's FTP client, not HTTP client).
So it should be:
ftp = FTP('mtb.dobzhanskycenter.org')
If you want o list files in /VCF subfolder, either do:
ftp.cwd('VCF')
vcffiles = ftp.nlst()
or
vcffiles = ftp.nlst('VCF')
Related
This question already has an answer here:
Python 3 ftplib error "Name or service not known"
(1 answer)
Closed 1 year ago.
I have a local FTP server set up on my machine. It's set up on 127.0.0.1 and port 21. When I'm trying to access it using the python-based library ftplib, the program is throwing an error. This is the code that I'm running.
from ftplib import FTP
ftp = FTP("ftp://127.0.0.1", user = "VAIBHAV", passwd = "12345")
ftp.dir()
ftp.retrlines("LIST")
ftp.quit()
And, this is the error.
Traceback (most recent call last):
File ".\main.py", line 5, in <module>
ftp = FTP("ftp://127.0.0.1", user = "VAIBHAV", passwd = "12345")
File "C:\Users\VAIBHAV\AppData\Local\Programs\Python\Python38\lib\ftplib.py", line 117, in __init__
self.connect(host)
File "C:\Users\VAIBHAV\AppData\Local\Programs\Python\Python38\lib\ftplib.py", line 152, in connect
self.sock = socket.create_connection((self.host, self.port), self.timeout,
File "C:\Users\VAIBHAV\AppData\Local\Programs\Python\Python38\lib\socket.py", line 787, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Users\VAIBHAV\AppData\Local\Programs\Python\Python38\lib\socket.py", line 918, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
How should I fix this error?
I'm using python 3.8.7 and I have Windows 10 on my machine.
Looking at the documentation, I don't see any "ftp://" before the hostname, so I think if you change it to
ftp = FTP("127.0.0.1", user="VAIBHAV", passwd="12345")
# or try "localhost" as well
ftp = FTP("localhost", user="VAIBHAV", passwd="12345")
it should work, or at least give you a different error.
I am trying to access the open DLP Test FTP server as a practice. I keep getting a getaddrinfo error but I am unsure of where I'm going wrong. I am using Python 2 on a Windows 10, and have already checked that I am not behind a proxy.
Code:
from ftplib import FTP
ftp = FTP('ftp://ftp.dlptest.com/')
...
Error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\ftplib.py", line 120, in __init__
self.connect(host)
File "C:\Python27\lib\ftplib.py", line 135, in connect
self.sock = socket.create_connection((self.host, self.port), self.timeout)
File "C:\Python27\lib\socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11001] getaddrinfo failed
Any help is appreciated!
Use
ftp = FTP('ftp.dlptest.com')
instead.
The first argument of FTP constructor is host – a hostname or an IP address – not a URL.
import paramiko
from socket import error as socket_error
import os
server =['10.10.0.1','10.10.0.2']
path='/home/test/'
for hostname in server:
try:
ssh_remote =paramiko.SSHClient()
ssh_remote.set_missing_host_key_policy(paramiko.AutoAddPolicy())
privatekeyfile = os.path.expanduser('~/.ssh/id')
mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile, password='test123')
ssh_remote.connect(hostname, username = 'test1', pkey = mykey)
sftp=ssh_remote.open_sftp()
for i in sftp.listdir(path):
info = sftp.stat(i)
print info.st_size
except paramiko.SSHException as sshException:
print "Unable to establish SSH connection:{0}".format(hostname)
except socket_error as socket_err:
print "Unable to connect connection refused"
This is my code. I tried to get file size of remote server files. But below error was throwing. Can some please guide on this?
Error
Traceback (most recent call last):
File "<stdin>", line 15, in <module>
File "/usr/lib/python2.6/site-packages/paramiko/sftp_client.py", line 337, in stat
t, msg = self._request(CMD_STAT, path)
File "/usr/lib/python2.6/site-packages/paramiko/sftp_client.py", line 624, in _request
return self._read_response(num)
File "/usr/lib/python2.6/site-packages/paramiko/sftp_client.py", line 671, in _read_response
self._convert_status(msg)
File "/usr/lib/python2.6/site-packages/paramiko/sftp_client.py", line 697, in _convert_status
raise IOError(errno.ENOENT, text)
IOError: [Errno 2] No such file
SFTPClient.listdir returns file names only, not a full path. So to use the filename in another API, you have to add a path:
for i in sftp.listdir(path):
info = sftp.stat(path + "/" + i)
print info.st_size
Though that's inefficient. Paramiko knows the size already, you are just throwing the information away by using SFTPClient.listdir instead of SFTPClient.listdir_attr (listdir calls listdir_attr internally).
for i in sftp.listdir_attr(path):
print i.st_size
I'm trying to access objects from my S3 bucket with s3cmd with path style urls. This is no problem with the Java SDK like.
s3Client.setS3ClientOptions(S3ClientOptions.builder()
.setPathStyleAccess(true).build());
I want to do the same with s3cmd. I have set this up in my s3conf file:
host_base = s3.eu-central-1.amazonaws.com
host_bucket = s3.eu-central-1.amazonaws.com/%(bucket)s
This works for bucket listing with:
$ s3cmd ls
2016-08-24 12:36 s3://test
When trying to list all objects of a bucket I get the following error:
Traceback (most recent call last):
File "/usr/local/bin/s3cmd", line 2919, in <module>
rc = main()
File "/usr/local/bin/s3cmd", line 2841, in main
rc = cmd_func(args)
File "/usr/local/bin/s3cmd", line 120, in cmd_ls
subcmd_bucket_list(s3, uri)
File "/usr/local/bin/s3cmd", line 153, in subcmd_bucket_list
response = s3.bucket_list(bucket, prefix = prefix)
File "/usr/local/lib/python2.7/site-packages/S3/S3.py", line 297, in bucket_list
for dirs, objects in self.bucket_list_streaming(bucket, prefix, recursive, uri_params):
File "/usr/local/lib/python2.7/site-packages/S3/S3.py", line 324, in bucket_list_streaming
response = self.bucket_list_noparse(bucket, prefix, recursive, uri_params)
File "/usr/local/lib/python2.7/site-packages/S3/S3.py", line 343, in bucket_list_noparse
response = self.send_request(request)
File "/usr/local/lib/python2.7/site-packages/S3/S3.py", line 1081, in send_request
conn = ConnMan.get(self.get_hostname(resource['bucket']))
File "/usr/local/lib/python2.7/site-packages/S3/ConnMan.py", line 192, in get
conn.c.connect()
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 836, in connect
self.timeout, self.source_address)
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 557, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
gaierror: [Errno 8] nodename nor servname provided, or not known
Assuming that there is no other issue with your configuration, the value that you used for "host_bucket" is wrong.
It should be:
host_bucket = %(bucket)s.s3.eu-central-1.amazonaws.com
or
host_bucket = s3.eu-central-1.amazonaws.com
The second one will for "path style" to be used. But, if you are using amazon s3 and the first host_bucket value that I propose, s3cmd will automatically use dns-based or path-based buckets depending of what characters you are using in your bucket name.
Is it a particular reason why you would want to only use path-based style?
I am doing the following
>> from ftplib import FTP
>> s = FTP('host','user','password') # Connect
and it fails giving the following
Traceback (most recent call last): File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 117, in __init__
self.connect(host)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 132, in connect
self.sock = socket.create_connection((self.host, self.port), self.timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 571, in create_connection
raise err socket.error: [Errno 60] Operation timed out
I know that host, user, passwd are correct
How do I debug/fix this error?
WinSCP (which you've otherwise been using to connect to the same server) supports SFTP and SCP, not FTP.
To write a Python program using SFTP, you should be using the Paramiko library.
Try doing it like this:
try:
s = FTP(host)
s.login(user, password)
except Exception, e:
print "The error was:", str(e)