Python Forum
A lot of confusion and I can't seem to solve this issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A lot of confusion and I can't seem to solve this issue
#11
(Jul-24-2022, 12:15 AM)Calli Wrote: Thanks for sharing this but what you have shared is completely the opposite of what i wanted done. For instance if I check this IP
118.31.106.59 is down!

It says that it's down but the fact that this is not down when you check using a specific port which in this case is 9200.

The host itself could ignore ICMP packets (standard on Windows).
Something between could filter out ICMP packets. Using ping is not a reliable method to detect if a host is reachable over network.

Then you shared this code:
import requests
 
with open('ip_lst1.txt') as f:
    for ip in f:
        ip = ip.strip()
        print(ip)
        response = requests.get(f"http://{ip}:9200/_cat/indices")
        print(response.text.strip())
You got this Exception: requests.exceptions.ConnectionError

Then catch this exception:
import requests
 
with open('ip_lst1.txt') as f:
    for ip in f:
        ip = ip.strip()
        print(ip)
        try:
            response = requests.get(f"http://{ip}:9200/_cat/indices")
        except requests.exceptions.ConnectionError:
            print("Connection error")
        else:
            print(response.text.strip())
In addition, you could and should add a timeout. It happens very frequently, that a host answers veryyy slooooooow.
import requests
 
with open('ip_lst1.txt') as f:
    for ip in f:
        ip = ip.strip()
        print(ip)
        try:
            # with timeout of 5 seconds
            # if timeout happens -> requests.exceptions.ConnectionError is raised
            response = requests.get(f"http://{ip}:9200/_cat/indices", timeout=5)
        except requests.exceptions.ConnectionError:
            print("Connection error")
        else:
            print(response.text.strip())
By the way, f should be fd or another better recognizable name.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: A lot of confusion and I can't seem to solve this issue - by DeaD_EyE - Jul-26-2022, 12:19 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020