A way to grab the request IP from tornado?

Hi awesome people,

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.

Thanks in advance!

2 Likes

Hi @Fhireman , were you able to get the user ips for your usecase ?

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.

Might be linked to this issue actually:

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.

Hi
Any luck in getting the user Ip?
I appreciate if someone can let me know how to do it? It’s quite important for my project.
Many thanks

1 Like

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:

(line: 123 ) user_info[“ip_addr”] = self.request.remote_ip

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

def main():
client_ip_addr = get_script_run_ctx()
print(client_ip_addr.user_info)

Edit: I’m hoping to submit a pull request or at least fork this so the feature is builtin going forward

streamlit v1.28.1