ConnectionRefusedError: [Errno 111] Connection refused - python

I was trying to use ftp and I am getting the following error:
>>> ftp = ftplib.FTP('192.168.248.108')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/ftplib.py", line 118, in __init__
self.connect(host)
File "/usr/lib/python3.5/ftplib.py", line 153, in connect
source_address=self.source_address)
File "/usr/lib/python3.5/socket.py", line 711, in create_connection
raise err
File "/usr/lib/python3.5/socket.py", line 702, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
I was trying to take it step by step since the whole client and server codes were not running. Help please. Thank you.
EDIT:
This is the client side code:
from ftplib import FTP
ftp = FTP('192.168.248.128')
ftp.login(user='username', passwd = 'password')
ftp.cwd('/Desktop/FTP')
def placeFile():
filename = 'myfile.txt'
ftp.storbinary('STOR '+filename, open(filename, 'rb'))
ftp.quit()
placeFile()

First of all check this ip to see if ftp service is available, and if it is check the port that it is listening on, cause it maybe (rare but possible) is configured to listen on a different port than the standard one - 21 . Also maybe the connection is blocked by a firewall, and that is why connection gets refused.
Also haven't seen the whole code of yours but I think another/different problem is this: def placeFile() should be changed to this instead def placeFile(ftp) - cause the function placeFile doesn't really know that ftp references to the ftp client you created above.

Related

Error to send e-mail using module smtplib

I'm doing a code to send emails automatically using Python and a local server (where I work). I don't know why this error happens.
I've tried to connect the server using commands from module smtplib -> smtplib.SMTP_SSL(hot, port) and smtplib.SMTP(hot,port) but both doesn't work.
import smtplib
server = smtplib.SMTP('IPfromCompanyServer')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\dbou\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\dbou\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\dbou\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 307, in _get_socket
self.source_address)
File "C:\Users\dbou\AppData\Local\Programs\Python\Python37\lib\socket.py", line 727, in create_connection
raise err
File "C:\Users\dbou\AppData\Local\Programs\Python\Python37\lib\socket.py", line 716, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
Make sure you're getting to Mail server IP and port number right. And some clients like Gmail don't allow you to send mails automatically without disabling the secure mail transfer feature.
Additionally you could add this piece of code to your program before you log in.
try:
self.smtp.ehlo()
self.smtp.starttls()
self.smtp.ehlo
except:
print "No TLS "
#login here
And maybe this link outta help out a little:
https://superuser.com/questions/1292420/sending-an-email-from-python-using-local-python-smtp-server

Using a Python script to establish FTP connection behind FTP proxy

Im trying to connect to an ftp server that is behind ftp proxy.
The proxy server does not require username and password, the ftp server does.
I have searched through several posts:
Proxies in python
How to use urllib2 to access ftp/http server using proxy with authentification
How to connect to ftp server via proxy using ftplib
How to specify an authenticated proxy for a python http connection?
Here is the last version of my code. So far I've figured out that to open the ftp server behind an ftp proxy, the notation ftp://username:password#server can be used. I'm using the urllib2 library to define the proxy server.
import urllib2
proxy_host = '101.11.44.84:8021' # only host name, no scheme (http/ftp)
proxy_handler = urllib2.ProxyHandler({'ftp': proxy_host})
auth = urllib2.FTPHandler()
try:
opener_thru_proxy = urllib2.build_opener(proxy_handler, auth)
except:
logger.exception('build_opener error')
raise
print opener_thru_proxy
try:
conn = opener_thru_proxy.open('ftp://user:password#100.159.66.113')
except:
logger.exception('opener thru proxy error')
raise
print conn.read()
conn.close()
The output of this code yields:
2016-05-23 16:15:28,286 - root - ERROR - opener thru proxy error
Traceback (most recent call last):
File "<string>", line 293, in <module>
File "C:\_Studna\Python\python-2.7.10.amd64\lib\urllib2.py", line 431, in open
File "C:\_Studna\Python\python-2.7.10.amd64\lib\urllib2.py", line 449, in _open
File "C:\_Studna\Python\python-2.7.10.amd64\lib\urllib2.py", line 409, in _call_chain
File "C:\_Studna\Python\python-2.7.10.amd64\lib\urllib2.py", line 1412, in ftp_open
File "C:\_Studna\Python\python-2.7.10.amd64\lib\urllib2.py", line 1434, in connect_ftp
File "C:\_Studna\Python\python-2.7.10.amd64\lib\urllib.py", line 875, in __init__
File "C:\_Studna\Python\python-2.7.10.amd64\lib\urllib.py", line 884, in init
File "C:\_Studna\Python\python-2.7.10.amd64\lib\ftplib.py", line 135, in connect
File "C:\_Studna\Python\python-2.7.10.amd64\lib\socket.py", line 575, in create_connection
URLError: <urlopen error ftp error: [Errno 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>
It seems that I'm unable to establish the ftp connection with the ftp server.
However, when I use the same set of connection data and use it in Total Commander, the connection works correctly. See the image enclosed Total Commander ftp connection window.

python - use pyftpdlib for file transfer

I am working on file transfer in python. I've been searching a lot but still haven't found a simple demo of a working ftp client-server using pyftpdlib. I think this library is very useful but I don't know how to start using it.
Thank you so much for any help!
EDIT:
OK, I followed the tutorial. My goal is to be able to send/receive files within local machine first.
I run the basic_ftpd.py and get this:
[I 14-07-09 15:08:27] >>> starting FTP server on 127.0.0.1:2121, pid=7000 <<<
[I 14-07-09 15:08:27] poller: <class 'pyftpdlib.ioloop.Select'>
[I 14-07-09 15:08:27] masquerade (NAT) address: None
[I 14-07-09 15:08:27] passive ports: None
Then I run winnt_ftpd.py and I get error:
Traceback (most recent call last):
File "***\lib\winnt_ftpd.py", line 41, in <module>
from pyftpdlib.authorizers import WindowsAuthorizer
ImportError: cannot import name WindowsAuthorizer
Let's assume that I want to send some file in C:/share and in local address I think it should be 127.0.0.1:2121 or localhost:2121. Then from client side I want to get directory listing:
import ftplib
ftp = ftplib.FTP("127.0.0.1:2121")
ftp.login("user", "12345")
data = []
ftp.dir(data.append)
ftp.quit()
for line in data:
print "-", line
But this is not working, I get following error:
Traceback (most recent call last):
File "C:\Users\***\src\client1.py", line 8, in <module>
ftp = ftplib.FTP("127.0.0.1:2121")
File "C:\Python27\lib\ftplib.py", line 117, in __init__
self.connect(host)
File "C:\Python27\lib\ftplib.py", line 132, in connect
self.sock = socket.create_connection((self.host, self.port), self.timeout)
File "C:\Python27\lib\socket.py", line 551, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11004] getaddrinfo failed
EDIT:
I changed
ftp = ftplib.FTP("127.0.0.1:2121")
to
ftp = ftplib.FTP("127.0.0.1")
Then I get this error:
socket.error: [Errno 10061] No connection could be made because the target machine actively refused it
Any suggestion?
What about trying this:
conn = ftplib.FTP()
conn.connect('127.0.0.1', 2121)
conn.login('user','12345')
the FTP() constructor accept only the hostname or IP address, instead if you pass nothing and then you configure all with connect() you can pass a tuple with (IP, port)
This should make the trick

Python SSL FTP connection timing out

I'm trying to get a connection established to a FTP server with SSL from within Python (v3.3.0). But I keep getting a timeout. I am NOT using port 990 as the SSL port (paranoid). Would that be the cause of this problem? And if so, how do I specify the port I am using?
Here's my script:
from ftplib import FTP
from ftplib import FTP_TLS
ftps = FTP_TLS('ip address')
ftps.auth()
ftps.sendcmd('USER uname')
ftps.sendcmd('PASS password')
ftps.prot_p()
ftps.retrlines('LIST')
ftps.close()
And here is the result:
Traceback (most recent call last):
File "Scrpit name removed for posting", line 12, in <module>
ftps.retrlines('LIST')
File "C:\Python33\lib\ftplib.py", line 767, in retrlines
conn = self.transfercmd(cmd)
File "C:\Python33\lib\ftplib.py", line 381, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "C:\Python33\lib\ftplib.py", line 742, in ntransfercmd
conn, size = FTP.ntransfercmd(self, cmd, rest)
File "C:\Python33\lib\ftplib.py", line 343, in ntransfercmd
source_address=self.source_address)
File "C:\Python33\lib\socket.py", line 424, in create_connection
raise err
File "C:\Python33\lib\socket.py", line 415, in create_connection
sock.connect(sa)
TimeoutError: [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
any advice would be greatly appreciated,
After looking at the ftplib source, it doesn't seem to want to use any port but 21.
I think you should be able to work around this, something like
import ftplib
ftplib.FTP.port = 995 # or whatever port you are using
ftps = ftplib.FTP_TLS('hostname', 'user', 'pwd')
ftps.retrlines('LIST')
Set the port through the connect
import ftplib
ftps = ftplib.FTP_TLS()
ftps.connect ('hostname', 991)

Network is unreachable if running script on webhosting

The next script runs fine on my mac. When I try to run it on my WebHosting (bluehost) I'm getting socket.error: [Errno 101] Network is unreachable. Any idea how can I fix it?
#!/usr/bin/python
# Required header that tells the browser how to render the text.
print "Content-type: text/html\r\n\r\n";
import imaplib
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('user#gmail.com', 'password')
mail.list()
# Out: list of "folders" aka labels in gmail.
mail.select("inbox") # connect to inbox.
print mail.list()
Traceback (most recent call last):
File "test2.py", line 6, in <module>
mail = imaplib.IMAP4_SSL('imap.gmail.com')
File "/home4/user/python27/lib/python2.7/imaplib.py", line 1148, in __init__
IMAP4.__init__(self, host, port)
File "/home4/user/python27/lib/python2.7/imaplib.py", line 163, in __init__
self.open(host, port)
File "/home4/user/python27/lib/python2.7/imaplib.py", line 1159, in open
self.sock = socket.create_connection((host, port))
File "/home4/user/python27/lib/python2.7/socket.py", line 571, in create_connection
raise err
socket.error: [Errno 101] Network is unreachable
Their support isn't helpful at all.
Can it be port related or maybe SSL?
On bluehosts help pages they mention that outgoing connnections are restricted, so ther problem isn't with your program. The only way of getting outbound connections to be allowed seems to pay for it.

Categories

Resources