简体   繁体   中英

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?

PS 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?

PS 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM