Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
URL check
#1
Hi

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

Thanks.
Reply
#2
use requests status

import requests

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

r = requests.get(url)
if r.status_code == requests.codes.ok:
    print("OK")
Reply
#3
(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?
Reply
#4
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.
Reply
#5
(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")
Axel_Erfurt likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

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