Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
socket.timeout: timed out
#1


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.
Reply
#2
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.
Reply
#3
Alright, thanks a lot.
Happy to know why it doesn't work..
I'm gonna try something else.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Issues with "connection timed out" duckredbeard 0 1,472 Dec-31-2022, 10:46 PM
Last Post: duckredbeard
  FTp timeout except korenron 2 3,593 Feb-01-2022, 06:51 AM
Last Post: korenron
  [Selenium]Timed out receiving message from renderer: 10.000 wood_6636 0 2,653 Jun-26-2020, 08:59 AM
Last Post: wood_6636
  Connection timed out error when connecting to SQL server kenwatts275 2 3,345 Jun-02-2020, 07:35 PM
Last Post: bowlofred
  Timed images menator01 0 1,845 Oct-24-2019, 07:34 AM
Last Post: menator01
  timeout value in subprocess jonesin1974 2 5,056 Dec-01-2017, 02:18 PM
Last Post: snippsat
  AsyncSSH and timeout Standard_user 1 5,501 Nov-03-2016, 06:05 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020