Python Forum
URL check - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: URL check (/thread-31267.html)



URL check - erdravi - Dec-01-2020

Hi

I want to find programmatically, whether a given URL is accessible via internet/intranet.

Thanks.


RE: URL check - Axel_Erfurt - Dec-01-2020

use requests status

import requests

url = "https://python-forum.io"

r = requests.get(url)
if r.status_code == requests.codes.ok:
    print("OK")



RE: URL check - erdravi - Dec-02-2020

(Dec-01-2020, 10:38 AM)Axel_Erfurt Wrote: use requests status

import requests

url = "https://python-forum.io"

r = requests.get(url)
if r.status_code == requests.codes.ok:
    print("OK")

Thanks. How to differentiate, whether the site is accessible by internet or intranet?


RE: URL check - bowlofred - Dec-02-2020

You can't tell unless you test it from both places. A server is free to respond in different ways to different clients. All you can tell is if it's accessible from your client.

If you know the IP ranges of your intranet, you can see if the hostname resolves to an IP in that range.


RE: URL check - DeaD_EyE - Dec-11-2020

(Dec-02-2020, 04:02 AM)erdravi Wrote: Thanks. How to differentiate, whether the site is accessible by internet or intranet?


In this case, the first step is to resolve the IP Address.

from ipaddress import ip_address
from urllib.parse import urlparse
from socket import gethostbyname


# get the hostname
url = "https://python-forum.io"
host = urlparse(url).hostname
print(host)

# resolve the ip address
ip = gethostbyname(host)
print(ip)

# converting the str into a IPv4Address object
ip = ip_address(ip)
print(ip)
# IPv4Address has the property is_private

if ip.is_private:
    print(url, "is a target in a private network")
else:
    print(url, "is a target in the internet")