baqarhussain commited on
Commit
1bc262b
·
verified ·
1 Parent(s): 0a934d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -22
app.py CHANGED
@@ -1,40 +1,47 @@
1
  import streamlit as st
 
2
  import socket
3
 
4
- # Function to resolve the IP address from a URL
5
- def get_ip_address_from_url(url):
6
  try:
7
- # Remove 'http://', 'https://', and 'www.' if present
8
- if url.startswith('http://'):
9
- url = url[7:]
10
- elif url.startswith('https://'):
11
- url = url[8:]
12
 
13
- if url.startswith('www.'):
14
- url = url[4:]
 
 
 
 
15
 
16
- # Get the IP address of the domain
17
- ip_address = socket.gethostbyname(url)
18
- return ip_address
19
  except socket.gaierror:
20
- st.error("Invalid URL or Unable to resolve IP address. Please ensure the domain is correct.")
21
- return None
 
22
 
23
  # Streamlit app
24
  def main():
25
- st.title("Get IP Address from URL")
 
26
 
27
- # Input field to enter a URL
28
- url = st.text_input("Enter the URL of the website (e.g., google.com)")
29
 
30
- # Button to get the IP address
31
- if st.button("Get IP Address"):
32
  if url:
33
- ip_address = get_ip_address_from_url(url)
 
 
34
  if ip_address:
35
- st.success(f"The IP Address for {url} is: {ip_address}")
 
 
 
36
  else:
37
- st.warning("Please enter a valid URL.")
38
 
39
  if __name__ == "__main__":
40
  main()
 
1
  import streamlit as st
2
+ import requests
3
  import socket
4
 
5
+ # Function to get IP address and country name for a given URL
6
+ def get_ip_and_country(url):
7
  try:
8
+ # Get the IP address of the website
9
+ ip_address = socket.gethostbyname(url)
 
 
 
10
 
11
+ # Query the IP geolocation service (ipinfo.io)
12
+ response = requests.get(f"https://ipinfo.io/{ip_address}/json")
13
+ data = response.json()
14
+
15
+ # Extract country information
16
+ country = data.get("country", "Country not available")
17
 
18
+ return ip_address, country
 
 
19
  except socket.gaierror:
20
+ return None, "Invalid URL or URL not found"
21
+ except requests.exceptions.RequestException as e:
22
+ return None, f"Error: {e}"
23
 
24
  # Streamlit app
25
  def main():
26
+ st.title("IP Address and Country Finder")
27
+ st.write("Enter the URL of the website below to find the IP address and country.")
28
 
29
+ # URL input by the user
30
+ url = st.text_input("Enter Website URL (e.g., www.example.com)")
31
 
32
+ # Button to submit and fetch details
33
+ if st.button("Get IP and Country"):
34
  if url:
35
+ # Validate and fetch IP and country
36
+ ip_address, country = get_ip_and_country(url)
37
+
38
  if ip_address:
39
+ st.success(f"IP Address: {ip_address}")
40
+ st.success(f"Country: {country}")
41
+ else:
42
+ st.error(country)
43
  else:
44
+ st.error("Please enter a valid URL")
45
 
46
  if __name__ == "__main__":
47
  main()