user1596692
user1596692

Reputation:

Python server response codes

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.

Upvotes: 0

Views: 222

Answers (1)

jdi
jdi

Reputation: 92559

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.

Upvotes: 2

Related Questions