0

Why can I not connect to my FTP? When I specify a port I receive error "socket.gaierror: [Errno -2] name of service not known", and when I don't specify my port I receive [errno 111] Connection refused. What am I doing wrong?

from ftplib import FTP
from pathlib import Path

file_path = ('TEST.txt')

with FTP('url:port', 'user', 'pass') as ftp, 
open(file_path, 'rb') as file:
ftp.storbinary(f'STOR {file_path.name}', file)
Kenster
  • 23,465
  • 21
  • 80
  • 106
  • What is your `url` like? It should not include the protocol (`ftp://`). It should just be something like `ftp.whatever.example.org`, as per the [documentation](https://docs.python.org/3/library/ftplib.html). It also looks like the way you specify a port is in the `connect` function based on [this](https://stackoverflow.com/questions/24788061/connecting-to-ftp-with-python-specifying-port-and-usn-passwd), so if it still gives issues, try specifying the port that way. – Random Davis Jan 19 '21 at 21:10
  • @RandomDavis my url is xxx.sftp.xxx.com. Should it be ftp.xxx.sftp.xxx.com? – Layton Hauler Jan 19 '21 at 21:17
  • No, just the protocol in the URL would cause issues, that should be fine how you have it. Including the port at the end like `xxx.sftp.xxx.com:####` could be an issue, can you try the alternative way of specifying the port and see if that changes anything? – Random Davis Jan 19 '21 at 21:34
  • Should you be using SFTP or FTP? They are completely different protocols. – Kenster Jan 19 '21 at 21:34
  • @Kenster I would like to use SFTP, however I am new to programming and cannot find an SFTP solution that works with my needs currently. – Layton Hauler Jan 19 '21 at 21:49
  • @RandomDavis I'm struggling to find a way to specify the port in a different way without breaking up my statements. I am an amateur python coder and am reading many tutorials and documents. – Layton Hauler Jan 19 '21 at 21:50
  • @LaytonHauler did you follow the second link I posted? This one? https://stackoverflow.com/questions/24788061/connecting-to-ftp-with-python-specifying-port-and-usn-passwd – Random Davis Jan 19 '21 at 22:12
  • For your literal question, see [Access FTP URL with ftplib](https://stackoverflow.com/q/64427208/850848). – Though, if you want to use SFTP, then you are asking a wrong question. See [SFTP in Python? (platform independent)](https://stackoverflow.com/q/432385/850848). – Martin Prikryl Jan 20 '21 at 07:47

0 Answers0