Login Register






Python Connect timeout filter_list
Author
Message
Python Connect timeout #1
I've been trying to get into socket programming with python, but every time I try to connect. All i've tried so far is the generic

Code:
>>> import socket >>> socket.setdefaulttimeout(5) >>> s = socket.socket() >>> s.connect(("*insert ip address*",21)) >>> ans = s.recv(1024) >>> print ans
Could anyone give me some info as to some possible reasons why it may be unable to connect before timing out? I'm sure there are probably multiple possibilities. I'm pretty new to network programming so any help is appreciated.
(This post was last modified: 06-24-2014, 03:06 AM by nullzero.)

Reply

RE: Python Connect timeout #2
First, of course you should make sure that the IP you're trying to connect to is actually running and open on port 21. Second, why do you define a default timeout? The defualt is none, so I see absolutely no reason to set a default timeout? I believe that it's here, you're problem is. You're also not really sending anything to the IP, you're just trying to receive something. What should the server sent to you, if you don't give it anything to respond to?

Instead try doing as followed.

We'll first setup a server, so you have something to connect to.

Code:
import socket HOST = '' # Symbolic name meaning the local host PORT = 50007 # Arbitrary non-privileged port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(1) conn, addr = s.accept() print 'Connected by', addr while 1: data = conn.recv(1024) if not data: break conn.send(data) conn.close()

Next, well make the client (which is what you're trying to do with your code), which connects to the server you just created.

Code:
import socket HOST = '' # The remote host PORT = 50007 # The same port as used by the server s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) s.send('Hello, world') data = s.recv(1024) s.close() print 'Received', repr(data)

This code can be found here: https://docs.python.org/release/2.5.2/li...ample.html

Reply

RE: Python Connect timeout #3
this might be obvious - but are you sure there is a service you can connect to present?

if ftp is running and present have you ensured youve sent everything expected by the server? such as a username,password,etc. its been years since ive read the ftp rfc but it may be waiting for additional input before it accepts the connection?

Reply

RE: Python Connect timeout #4
(06-24-2014, 06:14 AM)Anima Templi Wrote: First, of course you should make sure that the IP you're trying to connect to is actually running and open on port 21. Second, why do you define a default timeout? The defualt is none, so I see absolutely no reason to set a default timeout? I believe that it's here, you're problem is. You're also not really sending anything to the IP, you're just trying to receive something. What should the server sent to you, if you don't give it anything to respond to?

That helps. I guess I was having some misconceptions about connect. Thanks.

Reply