Python ftplib WinError 10060 - python

I am learning to retrieve files from an ftp server using ftplib from this link : https://docs.python.org/2/library/ftplib.html
When i run this code
from ftplib import FTP
ftp = FTP('ftp.debian.org')
ftp.login()
I get
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
From this answer https://stackoverflow.com/questions/4946960/when-using-ftplib-in-python#= i get to know that this is a server side issue which can be fixed by changing to ACTV mode.
So i changed my code to
from ftplib import FTP
ftp = FTP()
ftp.set_pasv(True)
ftp.connect('ftp.debian.org')
ftp.login()
Still same error. Can anyone tell me what other reasons could there be from my problem?
Edit - Using Python 3.6.1 on Thonny(IDE) in a 64 bit Win 10 environment

Nothing wrong with this code. It works for me. Maybe the server was just very slow at the time you tried it. You can set a timeout in the connect:
ftp.connect('ftp.debian.org',timeout=seconds)

I had the same problem. As the ftplib description Passive mode is on by default. So set it to False. It works for me.
ftp.set_pasv(False)
Or active passive in server(it's Debian 11 for me) as define the min & max port:
pasv_min_port=10000
pasv_max_port=11000
Of course, you need add the ports in firewall:
ufw allow 10000:11000/tcp

Setting passive mode to False works for me. Thanks!!
ftp.set_pasv(False)

Related

Python automatic connection in local network call for WinError 10061

I'm trying to make a program which can automatically connect to a computer in the local network based on the port inputted by the server. then the client, with the same port, tries by using the arp -a command to find every computer in the local network and try to connect to him.
This is the connection Method:
def connect(self):
devices = []
for device in os.popen('arp -a'): devices.append(device)
for ip in devices:
b = re.findall(r"(?:\s|\A)(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?=\s|\Z)", ip)
try:
print(b[0])
client_socket = socket.socket()
client_socket.settimeout(3)
client_socket.connect((b[0], self.port))
if type(client_socket) != None:
return client_socket
except Exception as e:
print(e)
I get a pretty weird issue: when I try to be a server on one computer, it works out just fine. However, when I try to switch the roles and be the client on that computer, it suddenly cant find the target computer and when it tries its IP (which I know since I checked the IP address of the computer with ipconfig), it errors out:
[WinError 10061] No connection could be made because the target machine actively refused it
timed out
I'm trying this on 2 different computers and it connects perfectly when i try this with this computer as server. Any help would be appreciated.
Edit: Also, I thought it would be helpful to note that no matter how high i set the timeout to be on the socket, it just waits that amount of time with the correct IP, then says it timed out. This is despite the server binding the port already...
Edit 2.0: Thought about looking at connection errors of the connection program at the other computer... completely different. no 10061 errors, just timing out and list index errors which are perfectly understandable with the nature of the function. Why does it only does the 10061 error on one computer? why when its 2 different computers? I'd like to know.

ftp.retrbinary timing out while downloading from remote server

I'm trying to download a file from a ftp server of a client. I have my code that gets the file and it works fine on my laptop. When I run it from the console in my production server which is inside a virtual machine it doesn't work. It also doesnt work on a virtual machine inside of my desktop pc.
The timout happen on ftp.retrbinary
Code:
# python > 3.6
from ftplib import FTP
file_csv = 'test.txt'
ftp = FTP(host=hostname, timeout=20)
login = ftp.login(user=user_name, passwd=user_pass)
ftp.set_pasv(False)
ftp.cwd('/csv_files/')
localfile = open(file_csv, 'wb')
ftp.retrbinary('RETR ' + file_csv, localfile.write, 1024)
ftp.quit()
localfile.close()
I've set the timeout to 20 or the code will never stop unless i force it. The message i get after the time out is
~/.pyenv/versions/3.6.8/lib/python3.6/socket.py in accept(self)
203 For IP sockets, the address info is a pair (hostaddr, port).
204 """
--> 205 fd, addr = self._accept()
206 # If our type has the SOCK_NONBLOCK flag, we shouldn't pass it onto the
207 # new socket. We do not currently allow passing SOCK_NONBLOCK to
I haven't been able to solve this. Please if someone can help with this, tahnks.
Do not use FTP active mode, unless you have a very specific reason to do so. The active mode usually does not work due to ubiquitous firewalls and NATs.
Use the passive mode instead. To use the passive mode, remove this call:
ftp.set_pasv(False)
ftplib defaults to the passive mode.
For some background, refer to my article on FTP connection modes.
Thanks for the answer Martin, I need to use set_pasv or it wont work.
I Solved the problem, my virtualmachine was using the default NAT settings. After switching to another settings it worked.(to 'Use bridged networking' on vmware)

Python 3.4 - failing to connect to http with urllib

I'm trying to run a simple Python script which connects to a website and reads a document on the website, but it fails. Any help, would be most welcome, thank you!
The code is as follows:
import urllib.request
fhand = urllib.request.urlopen('http://www.py4inf.com/code/romeo.txt')
for line in fhand:
print (line.strip())
And I'm getting the following two error messages:
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
urllib.error.URLError: {urlopen 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}
This is the windows error, not urllib's one. The latter has a handy default timeout value of never, but the former depends on settings of your proxy (with default being 60 seconds for initial connection and 120 for any further GET requests.
I can't help much with windows, but at least now you know where to look.
I had this same issue, and tried specifying a timeout in the urllib.request.urlopen, but no dice.
What did work was including a header, as described here. The first answer worked, and so did the second, so I used the simpler, second solution.

Errno 10060 and IMAP4_SSL

I'm trying to write a script that logs into my email server(yahoo) and checks for messages from a certain sender. This is my first time useing the IMAP module and I can't seem to get it to work. Right now I have only a few lines of code.
from imaplib import *
server = IMAP4_SSL('mail.yahoo.com')
server.login('myusername','mypassword')
mail_folders = server.list()
for folders in mail_folders:
print(folders)
at this point all I'm trying to do is login to the mail server and retrieve a list of folders. However I never get connected. the interpreter throws a
[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
I'm not sure if this has something to do with SSL or what. I have managed to get a connection with httplib.HTTPSConnection, But I would rather use IMAP then webscrape.
I think the problem is just the server name that is wrong. Replacing 'mail.yahoo.com' with 'imap.mail.yahoo.com' worked for me.

problem: Socket error [Address already in use] in python/selenium

I have a troublesome problem socket.error error: [Errno 10048]: Address already in use. Only one usage of each socket address (protocol/IP address/port) is normally permitted during automated tests using Selenium with Python. The problem is so interesting that it runs on one machine (Linux) works correctly, but on another machine (WindowsXP) generates this error.
I would add that the problem arose after the reinstallation of the system and set up all over again - with the previous configuration everything worked properly.
Is there maybe something I forgot? Has anyone come up with such a problem before?
Does anyone have an idea of ​​how to deal with this problem?
The current configuration / libraries:
python 2.7, numpy, selenium.py
If you open/close the socket multiple times, it could be in the TIME_WAIT state. This would explain why it acts differently on separate platforms (different TIME_WAIT settings and TCP stack). If you're controlling the socket object, you can set SO_REUSEADDR before binding to fix the problem.
For example:
sock = socket.socket()
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, server.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) | 1)
You can run netstat -b from the command prompt to give you a list of open sockets with the state and owning process.
I found the answer in the post below:
Python urllib2. URLError: <urlopen error [Errno 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted>
It turned out that this problem is limitation of Windows
There are several possibilities. If none of your tests can listen on some port (you don't say what port) then perhaps your Windows machine is running something on a port that you previously had open; this new service may have appeared during the reinstall. If, on the other hand, it's only a problem for some tests, or it's a little sporadic, then it may be either a programming issue (forgetting to close a socket in an early test which interferes with a later one) or a timing issue (the earlier test's socket isn't quite through closing before the new one tries to open up). Obviously there are different ways to address each of these problems, but I don't think we can help more than this without more details.
Maybe there is a software on your Windows that already use port 4444, can you try set Selenium to another port and try again?

Categories

Resources