Python Forum

Full Version: socket.timeout: timed out
You're currently viewing a stripped down version of our content. View the full version with proper formatting.


Hello everyone, I'm a newbie to Python programming and I've really fallen in love with the language; although I've got some little qualms right now.

While reading the book "Violent Python A Cookbook for Hackers, Forensic Analysts, Penetration Testers and Security Engineers", I came across this piece of code:

 import socket
 socket.setdefaulttimeout(2)
s = socket.socket()
 s.connect(("192.168.95.148",21))
 ans = s.recv(1024)
 print ans
When I typed it into the python console on Unix: I got this error message:

Error:
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/socket.py", line 228, in meth return getattr(self._sock,name)(*args) socket.timeout: timed out
So I carried out some research online for a solution or an explanation to what is going on. But I didn't find any that was convincing. For example, I were asked to modify that code to look like this:

import socket

try:
	s = socket.socket()
	s.settimeout(10)
	s.connect((192.168.95.148", 21))
	ans = s.recv(1024)
	print ans
	s.shutdown(1)
	s.close()
	
except socket.error as socketerror:
	print("Error: ", socketerror)
except socket.timeout:
	print("NO RESPONSE")
After running that one, I get this other error message:

Error:
('Error: ', timeout('timed out',))
So, please if anyone could explain what this error messages actually mean and help me understand how I could fix that, I would be very grateful.
The statement s.settimeout(10) sets a timeout period to 10 seconds. Then the statement s.connect(("192.168.95.148", 21)) tries to connect to the device which has the ip address 192.168.95.148 (this ip address is part of an internal network, not accessible from the Internet but only accessible within your internal network), on port 21. After 10 seconds, you will notice that the connection doesn't work, most probably because there is no device with such ip address on your internal network, so you get a timeout.
Alright, thanks a lot.
Happy to know why it doesn't work..
I'm gonna try something else.