I tried to tinker with the Streamlit source code to get this to work but didn’t have any success so far so I was wondering if the Streamlit community already found a solution to this.
Is there a way to tap into the tornado web server from a Streamlit application and grab the connecting client IP address? (a.k.a. RequestHandler.request.remote_ip).
My current use case is to let people fill in a Streamlit form that should only be filled in once while active and instead of dealing with:
1: Let people enter their mail address
2: Set up a mail server
3: Send mail verification link to mail address
4: Make an endpoint in a different application(Flask/Bottle/FastAPI, cause Streamlit currently doesn’t handle different endpoints or REST calls yet) that accepts the verification token
5: Log/toggle verification in a DB
It would be an easy restriction(not bulletproof, but good enough for this form) to just log the request IP in a DB after the form has been submitted.
Seeing as there were already some discussions about requests and API calls in Streamlit, a feature like this could be used in the same way as the st.get_url() proposal. st.get_request_ip() would seem logical.
Coincidentally I was looking into implementing captcha’s into this Streamlit app as well but having access to the request IP could also restrict automated form responses since the form won’t show if the IP has been logged.
I had this working using the report_thread module but can’t seem to get my old code working anymore at the moment. Seems that the Streamlit team has done some changes to it and maybe don’t want people to use it when I read through this discussion:
It’s a pity because being able to grab the IP is something that’s extremely useful. I do hope they re-add some way to grab it.
The last reply on this thread seems to have a reference to get the session_id at least, I’ll dive into it to see if this one can provide the client IP as well.
Not sure if you’ve found a solution yet. For my application I was deploying a streamlit app to an internal network but wanted to restrict which subnets could connect to the streamlit app from the application level.
Quick hack: if you open up your python venv → Lib → site-packages → streamlit → web → server → browser_websocket_handler.py
find the open() method and scroll down to the “return None” statement. just before the self._session_id = self._runtime.connect_session(…) statement add the following line:
this is a builtin attribute of the tornado.web.RequestHandler class that gets the client’s ip address and stores it in streamlits user_info dict. Once you have this setup, then you can just go into your streamlit app.py and add the following lines.
from streamlit.runtime.scriptrunner import get_script_run_ctx
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.