Python version 2.7.3
Python code, interactive mode
import socket
socket.setdefaulttimeout(2)
s = socket.socket()
s.connect(("192.168.95.148",21))
Errors out
>>> s.connect(("192.168.95.148",21))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.timeout: timed out
Syntax looks correct. I even tried with another IP address that is pingable and same error.
Thanks!
2 Answers 2
I am looking at the port you are using (21) and it's the FTP port.
https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
I don't see any errors in your code per se, but here some things I am worried about:
a) A lot of places lock down and turn off FTP, telnet, etc. ports
that are historically used, but in today's security consious world,
we use scp and ssh (instead of ftp and ssh). You might talk to your
system administrators and see if that service is even turned on,
or let alone your filewall even allows that port to go through.
b) I am not used to seeing the default parameters, but I assume you want
socket(socket.AF_INET, socket.SOCK_STREAM)
I have Python 2.6, and that is the defaults for that (which I assume is the same for 2.7).
c) I would try this on different ports to see if it is a port problem. Of course, the early numbered ports are special "well-known ports" that normal users can't use. I usually use port 9711 or 8888.
d) I would also try it without the timeout and see if that works.
I hope this helps!
1 Comment
This is not a direct answer to your question, but if you're seeking to do FTP transfers, may I suggest to use the standard ftplib module instead?