Http checks python

The python training is here, I want to check if any web server is working on my local network using this code, but it gives me a lot of errors in concole.

#!/usr/bin/env python

import httplib
last = 1
while last <> 255:
        url = "10.1.1." + "last"
        connection = httplib.HTTPConnection("url", 80)
        connection.request("GET","/")
        response = connection.getresponse()
        print (response.status)
        last = last + 1
+3
source share
4 answers

I suggest changing the while loop to a more idiomatic loop and handling exceptions:

#!/usr/bin/env python

import httplib
import socket


for i in range(1, 256):
    try:
        url = "10.1.1.%d" % i
        connection = httplib.HTTPConnection(url, 80)
        connection.request("GET","/")
        response = connection.getresponse()
        print url + ":", response.status
    except socket.error:
        print url + ":", "error!"

To learn how to add a timeout to this so that you don’t need to check each server for so long, see here .

+5
source

as indicated, you have basic quotes. but more fundamentally:

  • Pythonesque , . , , , funner ( ) .
  • -,
  • : ( -) -.
  • 10.1.1. * - "" . RFC 1918 , "" : 10.0.0.0 - 10.255.255.255, 172.16.0.0 - 172.31.255.255 192.168.0.0 - 192.168.255.255. "" .
  • - ( ) , 80 ( 8000, 8001 8080).
  • -, DNS .. - ( ).

, , , , (5), () .

btw -, "" . IP- Yahoo .

import urllib
import threading
import socket

def t_run(thread_list, chunks):
    t_count = len(thread_list)
    print "Running %s jobs in groups of %s threads" % (t_count, chunks)
    for x in range(t_count / chunks + 1):
        i = x * chunks
        i_c = min(i + chunks, t_count)
        c = len([t.start() for t in thread_list[i:i_c]])
        print "Started %s threads for jobs %s...%s" % (c, i, i_c - 1)
        c = len([t.join() for t in thread_list[i:i_c]])
        print "Finished %s threads for job index %s" % (c, i)

def url_scan(ip_base, timeout=5):
    socket.setdefaulttimeout(timeout)
    def f(url):
        # print "-- Trying (%s)" % url
        try:
            # the print will only complete if there a server there
            r = urllib.urlopen(url)
            if r:
                print "## (%s) got %s bytes" % (url, len(r.read()))
            else:
                print "## (%s) failed to connect" % url
        except IOError, msg:
            # these are just the common cases
            if str(msg)=="[Errno socket error] timed out":
                return
            if str(msg)=="[Errno socket error] (10061, 'Connection refused')":
                return
            print "## (%s) got error '%s'" % (url, msg)
            # you might want 8000 and 8001, too
            return [threading.Thread(target=f, 
                             args=("http://" + ip_base + str(x) + ":" + str(p),)) 
                    for x in range(255) for p in [80, 8080]]

# run them (increase chunk size depending on your memory)
# also, try different timeouts
t_run(url_scan("209.131.36."), 100)
t_run(url_scan("209.131.36.", 30), 100)
+2

last url. Python , . :

#!/usr/bin/env python

import httplib
last = 1
while last <> 255:
        url = "10.1.1.%d" % last
        connection = httplib.HTTPConnection(url, 80)
        connection.request("GET","/")
        response = connection.getresponse()
        print (response.status)
        last = last + 1
+1

URL- 'url': ,

    connection = httplib.HTTPConnection("url", 80)

mean . Once you fix this (by removing these quotes), you will try to connect to "10.1.1.last", given the quotes on the previous line. Set this line to

    url = "10.1.1." + str(last)

and it can work! -)

+1
source

Source: https://habr.com/ru/post/1718854/


All Articles