Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing socket connection in Python

Tags:

python

sockets

This question will expand on: Best way to open a socket in Python
When opening a socket how can I test to see if it has been established, and that it did not timeout, or generally fail.

Edit: I tried this:

try:
    s.connect((address, '80'))
except:
    alert('failed' + address, 'down')

but the alert function is called even when that connection should have worked.

like image 981
UnkwnTech Avatar asked Oct 07 '08 06:10

UnkwnTech


People also ask

How do you test socket programming?

Just run the server in one window, the client in another, and specify the connection address 127.0. 0.1 (which means localhost , your own computer). I must be doing something wrong then because when I do that nothing happens. If I run The server application and then the client application it just stands there....

What is sendall in Python?

sendall is a high-level Python-only method that sends the entire buffer you pass or throws an exception. It does that by calling socket. send until everything has been sent or an error occurs.


4 Answers

It seems that you catch not the exception you wanna catch out there :)

if the s is a socket.socket() object, then the right way to call .connect would be:

import socket
s = socket.socket()
address = '127.0.0.1'
port = 80  # port number is a number, not string
try:
    s.connect((address, port)) 
    # originally, it was 
    # except Exception, e: 
    # but this syntax is not supported anymore. 
except Exception as e: 
    print("something's wrong with %s:%d. Exception is %s" % (address, port, e))
finally:
    s.close()

Always try to see what kind of exception is what you're catching in a try-except loop.

You can check what types of exceptions in a socket module represent what kind of errors (timeout, unable to resolve address, etc) and make separate except statement for each one of them - this way you'll be able to react differently for different kind of problems.

like image 118
kender Avatar answered Sep 24 '22 23:09

kender


You can use the function connect_ex. It doesn't throw an exception. Instead of that, returns a C style integer value (referred to as errno in C):

s =  socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = s.connect_ex((host, port))
s.close()
if result:
    print "problem with socket!"
else:
    print "everything it's ok!"
like image 25
ssoto Avatar answered Sep 21 '22 23:09

ssoto


You should really post:

  1. The complete source code of your example
  2. The actual result of it, not a summary

Here is my code, which works:

import socket, sys

def alert(msg):
    print >>sys.stderr, msg
    sys.exit(1)

(family, socktype, proto, garbage, address) = \
         socket.getaddrinfo("::1", "http")[0] # Use only the first tuple
s = socket.socket(family, socktype, proto)

try:
    s.connect(address) 
except Exception, e:
    alert("Something's wrong with %s. Exception type is %s" % (address, e))

When the server listens, I get nothing (this is normal), when it doesn't, I get the expected message:

Something's wrong with ('::1', 80, 0, 0). Exception type is (111, 'Connection refused')
like image 29
bortzmeyer Avatar answered Sep 21 '22 23:09

bortzmeyer


12 years later for anyone having similar problems.

try:
    s.connect((address, '80'))
except:
    alert('failed' + address, 'down')

doesn't work because the port '80' is a string. Your port needs to be int.

try:
    s.connect((address, 80))

This should work. Not sure why even the best answer didnt see this.

like image 32
Patrik Bütler Avatar answered Sep 22 '22 23:09

Patrik Bütler