0

I'm writing test script for a TCP Server in Python 3.8.

The script worka well, but now I'm tring to implemente a more efficient error catching in order to identify timeout error.

To do that I started to catch the errors and the timeout error for the socket connect.

This is my SocketConnect function:

def SocketConnect( host, port ):
    global socketHandle

    opResult = errorMessagesToCodes['No Error']

    # Set Socket Timeout
    socketHandle.settimeout(1)
    logging.debug("Connect")
    try:
        socketHandle.connect((host, port))
    except socketHandle.error as e:
        opResult = errorMessagesToCodes['Socket Error']
        logging.error("!!! Socket Connect FAILED: %s" % e)

    return opResult

The socket handler is valid and, in order to test the timeout, I disable the server.

After one second after the connect the code goes to the except but I get this error:

socket.connect((host, port))

socket.timeout: timed out

During handling of the above exception, another exception occurred:

except socket.error as e: AttributeError: 'socket' object has no attribute 'error'

Is there something missing?

Because I don't understand why socket object ha no attribute error. I think this is a standard error for socket interface.

Thanks in advance for the help.

UPDATE: I tried to do a basic test (starting from a blank project): only a socket create and a socket connect (with a server not in listening mode) to simulate a timeout.

This is the code:

import socket
import logging

try:
    socketHandle = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socketHandle.error as e:
    logging.error("Socket Create FAILED: %s" % e)

socketHandle.settimeout(1)

try:
    socketHandle.connect(('localhost', 2000))
except socketHandle.error as e:
    logging.error("!!! Socket Connect FAILED: %s" % e)

The connect goes into timeout but I still get the error:

except socketHandle.error as e: AttributeError: 'socket' object has no attribute 'error'

I really don't know what is happening.

UPDATE 2:

I made some other tests, and if I use the try-catch inside the connect function I get the error but if I use the try catch in the main a did not get any error.

Best regards, Federico

1 Answer 1

1

The error is due to you redefining the module name socket; which is what contains socket.error. You are trying to access module level constants (in this case error from the socket module), from a socket object. You could also tighten the error handling to only catch a timeout. This may be needed anyhow, as it appears socket.error does not cover socket.timeout. Changing your socket name should solve the issue:

def SocketConnect(socx, host, port ):
    opResult = errorMessagesToCodes['No Error']

    # Set Socket Timeout
    socx.settimeout(1)
    logging.debug("Connect")
    try:
        socx.connect((host, port))
    except socket.timeout as e:
        opResult = errorMessagesToCodes['Socket Error']
        logging.error("!!! Socket Connect FAILED: %s" % e)

    return opResult
Sign up to request clarification or add additional context in comments.

7 Comments

Hi Robert, I changed the code as you suggested but I get the same error. Thanks
You are likely defining a socket object at a global level, can you share your entire code?
Yes exactly, I tring to define a global socket handler in order to shutdown the socket when the code crash with the module "atexit". I cannot share the code. Sorry
Well that is your problem, you can not rename the socket module and expect to access its members. Either change the name of your socket (as shown in the above example), use a context manager or try/except block instead of atexit, or import socket as something else
Is there a way to share a socket handle globally? I'm come from C language..So my python skill are not the best :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.