How can I connect to a FTP server in Python? [duplicate] - python

This question already has an answer here:
FTPLIB error socket.gaierror: [Errno 8] nodename nor servname provided, or not known [duplicate]
(1 answer)
Closed 1 year ago.
I am having problems when I try to connect to a ftp server, I am using a Mac with BigSur and Python3.
Here is my code:
import ftplib
FTP_HOST = "ftp://myserver"
FTP_USER = "user"
FTP_PASS = "pass"
# connect to the FTP server
ftp = ftplib.FTP(FTP_HOST, FTP_USER, FTP_PASS)
Here what it returns:
Traceback (most recent call last):
File "/Users/adrianrodriguezgalisteo/jonsuGo/test_upload.py", line 8, in <module>
ftp = ftplib.FTP(FTP_HOST, FTP_USER, FTP_PASS)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ftplib.py", line 119, in __init__
self.connect(host)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ftplib.py", line 156, in connect
self.sock = socket.create_connection((self.host, self.port), self.timeout,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 822, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 953, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
I just want to connect to the server and upload a file.
Thank you.

https://docs.python.org/3/library/ftplib.html
The host should only be myserver
FTP_HOST = "myserver"
You may need the FQDN of the servername

Related

python unable to connect to mqtt broker

When I was trying out a tutorial in the internet, I failed to connect to the mqtt broker - anyone can help me on that?
import paho.mqtt.client as mqtt
broker_url = "mqtt.eclipse.org"
broker_port = 1883
client = mqtt.Client()
client.connect(broker_url, broker_port)
print(client)
Traceback (most recent call last):
File "C:\Workspace\FI Systemintegration\Python\MitarbeiterVerwaltung\rpi\connect.py", line 6, in <module>
client.connect(broker_url, broker_port)
File "C:\Users\TorbenIT\AppData\Local\Programs\Python\Python310\lib\site-packages \paho\mqtt\client.py", line 914, in connect
return self.reconnect()
File "C:\Users\TorbenIT\AppData\Local\Programs\Python\Python310\lib\site-packages \paho\mqtt\client.py", line 1044, in reconnect
sock = self._create_socket_connection()
File "C:\Users\TorbenIT\AppData\Local\Programs\Python\Python310\lib\site-packages \paho\mqtt\client.py", line 3685, in _create_socket_connection
return socket.create_connection(addr, timeout=self._connect_timeout, source_address=source)
File "C:\Users\TorbenIT\AppData\Local\Programs\Python\Python310\lib\socket.py", line 824, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Users\TorbenIT\AppData\Local\Programs\Python\Python310\lib\socket.py", line 955, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
The service does not exist at mqtt.eclipse.org. It was either used only as an example or the service is no longer available.
You should use another MQTT instance, maybe run a local instance instead.

"getaddrinfo failed" when connecting to SFTP server using Paramiko

I am trying to connect to SFTP server but it returns error:
[Errno 11001] getaddrinfo failed
I am using Python 3.7.3 and Paramiko version is 2.6.0
import paramiko
host_name = "sftp://81.149.151.143"
user_name = "******"
password = "******"
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=host_name, port=220, username=user_name, password=password)
ftp_client=ssh_client.open_sftp()
ftp_client.put('***/issue_1.docx', '/issue_1.docx')
ftp_client.close()
This is the full error:
Traceback (most recent call last):
File "/sftp/paramiko_bot.py", line 10, in <module>
ssh_client.connect(hostname=host_name, port=22, username=user_name, password=password)
File "\Local\Programs\Python\Python37\lib\site-packages\paramiko\client.py", line 340, in connect
to_try = list(self._families_and_addresses(hostname, port))
File "\Local\Programs\Python\Python37\lib\site-packages\paramiko\client.py", line 204, in _families_and_addresses
hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM
File "\AppData\Local\Programs\Python\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 hostname parameter of SSHClient.connect should contain a hostname only (or in your case an IP address) – not any kind of URL.
ssh_client.connect(hostname="81.149.151.143", port=220, username=..., password=...)
Obligatory warning: Do not use AutoAddPolicy this way – You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".

Unable to connect to server using Paramiko package

I'm trying to connect to my server using SSH with port 2022 (not 22) in Python. So I wrote the following code that uses Paramiko package:
import sys
import paramiko
try:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy)
client.connect('ccap#10.40.2.222', '2022', '', 'ccap')
finally:
client.close()
But when I'm running it in my IDE (PyCharm) I get the following error:
/usr/local/lib/python3.5/dist-packages/paramiko/ecdsakey.py:164: CryptographyDeprecationWarning: Support for unsafe construction of public numbers from encoded data will be removed in a future version. Please use EllipticCurvePublicKey.from_encoded_point
self.ecdsa_curve.curve_class(), pointinfo
Traceback (most recent call last):
File "/home/mshapirs/PycharmProjects/OnlineTest.py/OnlineTest.py", line 9, in
client.connect('ccap#10.40.2.222', '2022', '', 'ccap')
File "/usr/local/lib/python3.5/dist-packages/paramiko/client.py", line 334, in connect
to_try = list(self._families_and_addresses(hostname, port))
File "/usr/local/lib/python3.5/dist-packages/paramiko/client.py", line 204, in _families_and_addresses
hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM
File "/usr/lib/python3.5/socket.py", line 733, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
You should provide username as a separate parameter, not prepended to the host address.
Look at the docs for .connect. It has username and hostnamelisted separately.

Send email using python

I want to send emails from my Gmail account using python. I followed steps given in this stackoverflow post: How to send an email with Python?
But, my the mails that I sent do not reach the addresses.
This is the error that I get:
Traceback (most recent call last):
File "something.py", line 24, in <module>
server = smtplib.SMTP('myserver')
File "/anaconda2/lib/python2.7/smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "/anaconda2/lib/python2.7/smtplib.py", line 317, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/anaconda2/lib/python2.7/smtplib.py", line 292, in _get_socket
return socket.create_connection((host, port), timeout)
File "/anaconda2/lib/python2.7/socket.py", line 557, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
What should I be doing here?
What you've get is a DNS query error indicating that domain myserver does not exist.
You have to replace the argument myserver in server = smtplib.SMTP('myserver') with the actual address of SMTP server, such as smtp.mail.yahoo.com.
This is how I do it.
import smtplib
server=smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login('your_email#gmail.com','your_password')
server.sendmail('your_email#gmail.com','your_email#gmail.com','test email')

Python and ftplib, unable to login?

I'm learning Python and I tried using the FTPLib module for Python with this code:
import ftplib
connect = ftplib.FTP('ftp://www.website.com')
connect.login = ('username', 'password')
data = []
connect.dir(data.append)
connect.quit()
for line in data:
print line
(I'm aware that the website, username and password is incorrect, I used my website data which I don't want to share) I received the following error after running the code:
Traceback (most recent call last):
File "ftp.py", line 3, in <module>
ftp = FTP('ftp://www.website.com') # connect to host, default port
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 120, in __init__
self.connect(host)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 135, in connect
self.sock = socket.create_connection((self.host, self.port), self.timeout)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
Just to clarify, I'm using Python 2.7 on a Mac. I don't think there is anymore details I could share. Thank you for your help!
Thanks Joel Hinz, I just needed to remove 'ftp://' from my hostname. Thanks!

Categories

Resources