socket.error



  • Greetings,
    there seems to be no socket.error nor socket.OSError. How can I trap an error seeing that the socket library has no socket.error clause?

    here's my sample snippet:

    while True:
        msg = input('Enter message to send : ')
    
        try :
            # Set the whole string and convert encode the string to bytes
            s.sendto(str.encode(msg), (host, port))
    
            # receive data from client (data, addr)
            d = s.recvfrom(1024)
            reply = d[0]
            addr = d[1]
    
            print('Server reply : ' + str(bytes.decode(reply)))  # convert reply to string
        except socket.error:
            print('Error Code : ' + str(msg[0]) + ' Message ' + str(msg[1]))
            sys.exit()
    
    
    

    and the error is:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/lib/udpClient.py", line 32, in <module>
    AttributeError: 'module' object has no attribute 'error'
    
    

    The "except socket.error" is the error line

    thanks
    omari



  • @Ibrahim-Salim-Omari

    The usocket implementation does not include an form of custom errors. Instead it throws in instance of OSError

    instead of:

    except socket.error:
    

    try

    except OSError:
    


  • Which board are you using? I'm using the expressif esp32-wrover-b.
    Your code is no different than mine. Even if I enter it line by line in pymakr repl, I still get an error on: except socket.error.
    From the pymakr prompt, there is no socket.error nor socket.socket.error routine. And yes, I'm importing socket not usocket although I tried it with usocket and got the same error.

    The code runs fine as long as there are no errors at which time the socket.error routine is called.


  • Global Moderator

    For me, it works if I use it like this (perhaps you are importing usocket instead of socket?):

    from network import WLAN
    import time
    import socket
    import machine
    import sys
    wlan = WLAN()
    wlan.mode(WLAN.STA)
    wlan.connect(ssid="",auth=(WLAN.WPA2, "#"))
    print("connecting.",end ='')
    while not wlan.isconnected():
        print(".",end='')
        time.sleep(1)
    print("connected")
    print("WiFi connection established")
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    
    while True:
        msg = 'Enter message to send : '
    
        try :
            # Set the whole string and convert encode the string to bytes
            s.sendto(str.encode(msg), ('192.168.4.4', 80))
    
            # receive data from client (data, addr)
            d = s.recvfrom(1024)
            reply = d[0]
            addr = d[1]
    
            print('Server reply : ' + str(bytes.decode(reply)))  # convert reply to string
        except socket.error:
            print('Error Code : ' + str(msg[0]) + ' Message ' + str(msg[1]))
            sys.exit()
    

Log in to reply
 

Pycom on Twitter