Question

So I'm having some trouble with these Python lists I am trying merge together. I am trying to write an algorithm to check if the machine is connected to the internet. So I ping different Google servers and if none of them responds I assume the machine is offline.

The script I am using to ping the servers is a rewrite I made of another script and it can be found here. The echo() function sends one ping and returns either the response delay (in seconds) or None if it fails. The recursive() functions sends multiple pings and returns a list with the responses from echo() (e.g. [0.054, 0.134, None, 0.037]).

The recursive function (short of docstring and some tired comments):

def recursive(dest_addr, count=4, timeout=1, verbose=False):

    if verbose:
        print("PING {} ; SEQUENCE {} ; TIMEOUT {}s".format(dest_addr, count, timeout))
        nrc = 0

    log = []
    for i in xrange(count):
        log.append(echo(dest_addr, timeout))
        if verbose:
            if log[-1] is None:
                print("Echo Request Failed...")
                nrc += 1
            else:
                print("Echo Received:  sequence_id={}  delay={} ms").format(i, round(log[-1]*1000, 3))

    return log

So when I ping these servers, I get multiple lists of those, and attempt to merge them like so:

results = recursive("google.ca")  # North America
results = results.extend(recursive("google.com.br"))  # South America

Doing that renders me the following error:

AttributeError: 'NoneType' object has no attribute 'extend'

I would like to know why that is happening.

Thanks in advance.

Was it helpful?

Solution

If the exception is really raised from the code you presented (I assume it's the second line), then the only way how I can explain the AttributeError is that the recursive() function actually returned None.

However, as @hcwsha pointed out, extend() itself returns None and you do store it in the results, so if there is at least one line similar to that one, it definitely causes the culprit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top