- 1 year ago
Python | Check if website exists by IP address
Comment
1 / 1
To check if a website exists by its IP address using Python, you can perform a simple HTTP request to that IP address and check the response status code. Here's an example:
pythonimport requests
def check_website_exists(ip_address):
url = f"http://{ip_address}"
try:
response = requests.get(url)
if response.status_code == 200:
return True
except requests.exceptions.RequestException:
pass
return False
# Example usage
ip_address = "192.168.0.1"
exists = check_website_exists(ip_address)
if exists:
print(f"Website at IP address {ip_address} exists.")
else:
print(f"Website at IP address {ip_address} does not exist.")
In this example, the check_website_exists
function takes an IP address as input, constructs a URL using that IP address, and sends an HTTP GET request to that URL. If the response status code is 200 (indicating a successful response), it returns True
, indicating that the website exists. Otherwise, it returns False
.
You can replace "192.168.0.1"
with the IP address you want to check.