0

Is there a way in python to check the server response codes (200, 301, 404) in the header of a specified ip range (1.1.1.1 - 1.1.1.254) maybe its even possible to do it multi-threaded?

P.S. fond out that its possible with the "HTTPResponse.status" object (http://docs.python.org/library/httplib.html) how could i now check the ip range with it?

P.S. May be it would be a good idea to first check if port 80 is open and then only test the ones with open ports i think it would speed it really up because of 254 ip's maybe 30 are using port 80.

1 Answer 1

2

You can just try and connect with a normal GET request to the root of the host, with a short timeout (or longer one if you want it to wait more). Then you can run it through a map.

import httplib
from multiprocessing import Pool

def test_ip(addr):
    conn = httplib.HTTPConnection(addr, timeout=1)
    try: 
        conn.request("GET", "/")
    except:
        return addr, httplib.REQUEST_TIMEOUT
    else:
        resp = conn.getresponse()
        return addr, resp.status
    finally:
        conn.close()

p = Pool(20)

results = p.map(test_ip, ["1.1.1.%d" % d for d in range(1,255)], chunksize=10)
print results

# [('1.1.1.1', 408), ('1.1.1.2', 408), ...]

Adjust Pool size and chunksize to suit.

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

3 Comments

Thanks for the code but please DON'T RUN THIS SCRIPT :) it froze my PC 2 times. It starts a few thousand of processes and even when you close it it continues. I have a pretty good PC (4*4,0 GHz 8GB Ram) and it doesn't work for me :) or do i do some thing wrong i adjusted the Pool size to the minimum and still kills my PC :(
Are you serious? It only starts Pool(size) number of processes. If you set it to 1 it should only be 1. Works just fine for me.
I would recommend opening a shell, and simply creating and pool, then check your process monitor and see how many python processes were created. They get initialized when you create the pool

Your Answer

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