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.
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.
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')
I have a simple script that is failing. I am running the service and the firewall is off. Anyone have any ideas on what's wrong? I can't netcat to it either with cmd : nc -vz 127.0.0.1 40400
Python 2.7
import telnetlib
tn = telnetlib.Telnet("127.0.0.1", 40400)
Traceback (most recent call last): File
"/Users/mhegglin/assignments/telemetry/Basis.py", line 3, in
tn = telnetlib.Telnet("127.0.0.1", 40400)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/telnetlib.py",
line 211, in init
self.open(host, port, timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/telnetlib.py",
line 227, in open
self.sock = socket.create_connection((host, port), timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py",
line 575, in create_connection
raise err socket.error: [Errno 61] Connection refused
My telnet server wasn't staying online so when I tried to telnet it was failing
I've been trying to connect to my Gmail account using python. imap is enabled.
import imaplib
imap_server = imaplib.IMAP4_SSL("imap.gmail.com",993)
# also tried imap_server = imaplib.IMAP4_SSL("imap.gmail.com"), doesnt work.
Traceback is :
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
imap_server = imaplib.IMAP4_SSL("imap.gmail.com",993)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 1202, in __init__
IMAP4.__init__(self, host, port)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 172, in __init__
self.open(host, port)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 1217, in open
IMAP4.open(self, host, port)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 248, in open
self.sock = self._create_socket()
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 1205, in _create_socket
sock = IMAP4._create_socket(self)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 238, in _create_socket
return socket.create_connection((self.host, self.port))
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/socket.py", line 435, in create_connection
raise err
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/socket.py", line 426, in create_connection
sock.connect(sa)
OSError: [Errno 65] No route to host
What OSError: [Errno 65] No route to host means is what it say: you can't get to that machine from your machine.
You can test that from outside of Python by opening up a terminal/DOS prompt and typing this:
ping imap.gmail.com
It's possible that this is actually a name lookup error, and you're somehow getting a bad address for imap.gmail.com. So, just to be sure, check by IP address too:
ping 74.125.129.108
ping 74.125.129.109
If ping works, you can check whether your router is for some reason just blocking TCP access to the host, e.g., with:
telnet imap.gmail.com
If it's working, this should either hang for a long time, or give you a connection-refused error; if it gives you a no-route-to-host error, it's the same problem you're seeing.
It's also possible that your router is specifically blocking port 993. You can test this too:
telnet imap.gmail.com 993
If it doesn't come back with something like "Connected to gmail-imap.l.google.com", same problem here too.
At any rate, once you've verified that this is a system or network configuration problem, not a programming problem, go ask for help with your system on the appropriate site.
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)