1

I want to establish a FTP connection to Windows server (let's say 10.20.30.50) I am using Python ftblib package

session = ftplib.FTP('10.20.30.40','admin','password@123')
file = open('download.jpg','rb')                  # file to send
session.storbinary('STOR download.jpg', file)     # send the file
file.close()                                    # close file and FTP
session.quit()

But I am getting the error

socket.error: [Errno 10061] No connection could be made because the target machine actively refused it

Traceback:

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 571, in create_connection
    raise err
socket.error: [Errno 10061] No connection could be made because the target machine actively refused it.

I tried telnet 10.20.30.40 8081 and it is successful.

I allow port 8081 for ftp. I open port by adding new inbound rules.

I tried several available solutions on Stack Overflow as well as other sites but they all are talking about to check the port. I do all the things but still no help.

Thanks

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
C P Verma
  • 320
  • 7
  • 23
  • 1
    How exactly did you *"try telnet"*? Did you connect to port 21 using telnet? From the same machine that runs your Python code? + *"I opened specific port also for connection"* - That's pretty vague. Where/how did you *"opened port"*? – Martin Prikryl Aug 09 '18 at 08:26
  • 1
    Did you copy your first code line correctly? It has an odd number of apostrophes so it doesn't parse the way it should. I don't think *that* is the cause of your bug, but if you didn't copy the code correctly there's a risk we're not seeing what your actual problem is. – alkanen Aug 09 '18 at 08:31
  • @MartinPrikryl I am using telent by this way-- telnet 10.20.30.40 8081. I allow port 8081 for ftp. I open port by adding new inbound rules – C P Verma Aug 10 '18 at 05:53
  • @alkanen I edit my question – C P Verma Aug 10 '18 at 05:59

2 Answers2

0

If your FTP server uses a non-standard port (8081), you have to specify that, when using FTP class.

session = ftplib.FTP()
session.connect('10.20.30.40', 8081)
session.login('admin','password@123')

See also Python ftplib - specify port.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
0

I forgot to enable FTP service in server. I followed this link and it is solved my issue

https://medium.com/@JohGeoCoder/allowing-ftp-access-on-windows-server-2016-hosted-on-microsoft-azure-af25898df958

C P Verma
  • 320
  • 7
  • 23