Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python socket connection exception

Tags:

People also ask

How does Python handle socket exception?

If you do intend to handle each error in a different way, then you can leave them separate as you already have. But make sure to break / return at the end of the exception block so that you don't try the next. It's done that way in the socket examples, by using a continue in the loop.

What is socket error connection refused?

Overview. Windows socket error 10061 is a Connection refused error sent to you by the target machine. You could not make a connection because the target machine actively refused it. The most common cause is a Network/Windows firewall or security software that blocking the connection on the port specified by the client.

What is socket connection in Python?

Sockets and the socket API are used to send messages across a network. They provide a form of inter-process communication (IPC). The network can be a logical, local network to the computer, or one that's physically connected to an external network, with its own connections to other networks.

How do you check if a socket is connected disconnected in Python?

The python socket howto says send() will return 0 bytes written if channel is closed. You may use a non-blocking or a timeout socket. send() and if it returns 0 you can no longer send data on that socket.


I have a socket-connection going on and I wanna improve the exception handling and I'm stuck. Whenever I call socket.connect(server_address) with an invalid argument the program stops, but doesn't seem to raise an exception. Here is my code:

import socket
import sys
import struct
class ARToolkit():
    
    def __init__(self):
        self.x = 0
        self.y = 0
        self.z = 0
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.logging = False
    
    def connect(self,server_address):
        try:
            self.sock.connect(server_address)
        except socket.error, msg:
            print "Couldnt connect with the socket-server: %s\n terminating program" % msg
            sys.exit(1)
    
    def initiate(self):
        self.sock.send("start_logging")
    
    def log(self):
        self.logging  = True  
        buf = self.sock.recv(6000)
        if len(buf)>0:
            nbuf = buf[len(buf)-12:len(buf)]
            self.x, self.y, self.z = struct.unpack("<iii", nbuf) 
    
    def stop_logging(self):
        print "Stopping logging"
        self.logging = False
        self.sock.close()
            

The class maybe looks a bit wierd but its used for receiving coordinates from another computer running ARToolKit. Anyway, the issue is at the function connect():

def connect(self,server_address):
    try:
        self.sock.connect(server_address)
    except socket.error, msg:
        print "Couldnt connect with the socket-server: %s\n terminating program" % msg
        sys.exit(1)

If I call that function with a random IP-address and portnumber the whole program just stops up at the line:

self.sock.connect(server_address)

The documentation I've read states that in case of an error it will throw a socket.error-exception. I've also tried with just:

except Exception, msg:

This, if I'm not mistaken, will catch any exceptions, and still it yields no result. I would be very grateful for a helping hand. Also, is it okay to exit programs using sys.exit when an unwanted exception occurs?

Thank you