Search code examples
pythondjangosocketsudplauterbach

Django socket connection timout


I am trying to check if a certain device (Lauterbach PowerDebug Pro) is connected to the network. I have the following code that I am using in Django view.

    def checkConnection(hostname, port, timeout=5):
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.settimeout(timeout)
        message = bytes.fromhex('{1024 bytes here}')
        s.sendto(message,(hostname,port))
        try:
            if s.recv(1024):
                return True
        except ConnectionResetError:
            return False
        except socket.timeout:
            return False

If I run this function from the Django View (called through AJAX), it will return False.

If I run this code in a Python console, it will return True. If I run manage.py shell and manually write this code, it will return True.

However, if I run manage.py shell, import and call this function, it will return False, due to a timeout error.

How come? I can't figure out why it will not work. I have captured the messages using Wireshark and they are all the same. Below is the output.

Code written manually in Django shell (returned True):

No. Time Source Destination
Protocol Length Info 233 17.202222 10.171.90.59 10.171.94.100 UDP 1066 59221 → 60010 Len=1024

Frame 233: 1066 bytes on wire (8528 bits), 1066 bytes captured (8528 bits) on interface 0 Ethernet II, Src: IntelCor_c1:d7:3e (b4:6b:fc:c1:d7:3e), Dst: All-HSRP-routers_5a (00:00:0c:07:ac:5a) Internet Protocol Version 4, Src: 10.171.90.59, Dst: 10.171.94.100 User Datagram Protocol, Src Port: 59221, Dst Port: 60010 Data (1024 bytes)

0000 03 01 01 00 62 ea 46 fd 54 52 41 43 45 33 32 00
....b.F.TRACE32. 0010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0080 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0090 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0100 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0110 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0120 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0130 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0140 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0150 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0160 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0170 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0180 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0190 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 01a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 01b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 01c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 01d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 01e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 01f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0200 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0210 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0220 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0230 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0240 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0250 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0260 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0270 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0280 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0290 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 02a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 02b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 02c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 02d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 02e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 02f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0300 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0310 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0320 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0330 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0340 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0350 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0360 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0370 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0380 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0390 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 03a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 03b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 03c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 03d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 03e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 03f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ Data: 0301010062ea46fd54524143453332000000000000000000… [Length: 1024]

No. Time Source Destination
Protocol Length Info 234 17.220294 10.171.94.100 10.171.90.59 UDP 58 60002 → 59221 Len=16

Frame 234: 58 bytes on wire (464 bits), 58 bytes captured (464 bits) on interface 0 Ethernet II, Src: Cisco_74:52:0b (a4:6c:2a:74:52:0b), Dst: IntelCor_c1:d7:3e (b4:6b:fc:c1:d7:3e) Internet Protocol Version 4, Src: 10.171.94.100, Dst: 10.171.90.59 User Datagram Protocol, Src Port: 60002, Dst Port: 59221 Data (16 bytes)

0000 23 00 00 00 0a ab 5a 3b 54 52 41 43 45 33 32 00

.....Z;TRACE32.

Data: 230000000aab5a3b5452414345333200
[Length: 16]

checkConnection function imported and ran in Django shell (returned True):

No. Time Source Destination
Protocol Length Info 437 53.946438 10.171.90.59 10.171.94.100 UDP 1066 59224 → 60010 Len=1024

Frame 437: 1066 bytes on wire (8528 bits), 1066 bytes captured (8528 bits) on interface 0 Ethernet II, Src: IntelCor_c1:d7:3e (b4:6b:fc:c1:d7:3e), Dst: All-HSRP-routers_5a (00:00:0c:07:ac:5a) Internet Protocol Version 4, Src: 10.171.90.59, Dst: 10.171.94.100 User Datagram Protocol, Src Port: 59224, Dst Port: 60010 Data (1024 bytes)

0000 03 01 01 00 62 ea 46 fd 54 52 41 43 45 33 32 00
....b.F.TRACE32. 0010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0080 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0090 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0100 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0110 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0120 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0130 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0140 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0150 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0160 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0170 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0180 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0190 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 01a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 01b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 01c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 01d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 01e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 01f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0200 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0210 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0220 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0230 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0240 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0250 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0260 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0270 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0280 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0290 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 02a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 02b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 02c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 02d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 02e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 02f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0300 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0310 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0320 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0330 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0340 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0350 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0360 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0370 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0380 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0390 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 03a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 03b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 03c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 03d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 03e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 03f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ Data: 0301010062ea46fd54524143453332000000000000000000… [Length: 1024]

No. Time Source Destination
Protocol Length Info 438 53.948831 10.171.94.100 10.171.90.59 UDP 58 60002 → 59224 Len=16

Frame 438: 58 bytes on wire (464 bits), 58 bytes captured (464 bits) on interface 0 Ethernet II, Src: Cisco_74:52:0b (a4:6c:2a:74:52:0b), Dst: IntelCor_c1:d7:3e (b4:6b:fc:c1:d7:3e) Internet Protocol Version 4, Src: 10.171.94.100, Dst: 10.171.90.59 User Datagram Protocol, Src Port: 60002, Dst Port: 59224 Data (16 bytes)

0000 23 00 00 00 0a ab 5a 3b 54 52 41 43 45 33 32 00

.....Z;TRACE32.

Data: 230000000aab5a3b5452414345333200
[Length: 16]

So what gives?

Edit: It works for TCP connections (ex. checkConnection('10.11.12.13', 23) will return True). For some reason, only the UDP message will not be received.

In this case, checkConnection is:

    def checkConnection(hostname, port, timeout=5):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(timeout)
        try:
            s.connect((hostname, port))
            s.shutdown(socket.SHUT_WR)
            while True:
                try:
                    data = s.recv(1024)
                    if data:
                        s.close()
                        return True
        except socket.timeout:
            return False
        except TimeoutError:
            return False
        except ConnectionRefusedError:
            return False
        except ConnectionResetError:
            return False

Solution

  • It seems the trick is to use recvfrom() for UDP.

            try:
                data, address = s.recvfrom(1024)
                if data:
                    return True
    

    recv()
    The recv() call is normally used only on a connected socket (see connect(2)).
    It is equivalent to the call:

    recvfrom(fd, buf, len, flags, NULL, 0);

    I still don't understand why recv() worked in all situations, except running from a Django view.