Can not get users public IP when browsing from streamlit share

I am building an app using streamlit. That app requires users’ public IP addresses for further processing. I have used geocoder module to get the IP address. And it is working when I run the streamlit app in localhost. This was the code I was using.

import geocoder
import streamlit as st
location = geocoder.ip("me")
ip_address = location.ip
ip_url = f"https://reallyfreegeoip.org/json/{ip_address}"
r = requests.get(ip_url)
ip_details = r.json()
county_name = ip_details["country_name"]
st.write(county_name)

But as soon as I deploy the app in streamlit share it does not work. Instead of working I am getting the below error in the log, and the app crashes. The app does not load at all.

Status code 429 from http://ipinfo.io/json: ERROR - 429 Client Error: Too Many Requests for url: http://ipinfo.io/json

So, I changed the method for getting the IP address. This time I used the below code to get the IP address, and it is again working well in the localhost.

import requests
import streamlit as st
ip = requests.get('https://api64.ipify.org').text
ip_url = f"https://reallyfreegeoip.org/json/{ip}"
r = requests.get(ip_url)
ip_details = r.json()
county_name = ip_details["country_name"]
st.write(county_name)

But as soon as I deploy the app in the streamlit share. It is getting the United States IP only. This time no error message like the previous one.

1 Like

Hi @hridoyboss12, welcome to the Streamlit community!

I think this has to do with a quirk of Streamlit sharing, which is that each app itself is running inside of an iframe. So in that sense, I suspect that the geolocation can only say the IP address is in the U.S., as that’s where our main data center is running at this time.

Best,
Randy

But Why am I getting 429 Client Error: Too Many Requests error when using the geocoder. It is working in my localhost. But why is not it working at all with streamlit sharing?

Forget this approach! You will only ever get the ip address of the machine running streamlit, but never the ip address of the machine running the browser. This only works for you locally because it is the same machine.

Probably because other users of the infrastructure have already used this service and the quota has been exceeded. You share the infrastructure with many other users.