Search code examples
pythonpython-3.xfastapiuvicorn

How can I see the domain name or ip of the PC accessing my API method?


I have an API method deploy on my local server,

@app.post("/test/api")
async def method():
     if incoming.request.url or domain == "this":
         do some operation
     else:
         skip it
     .....
     return something

Now, Few people are using my API method, but is there any way I could track who is calling my api method and do specific extra operations to the once I specified who is calling my api.

How can I track the incoming domain name or ip or url of the people who are using my api method?

Note: Need a basic example on how to acheive it if you familiar with it

Is it something possible?


Solution

  • If you were using flask it could happen simply by getting remote_addr as below:

    from flask import FLASK, request
    @app.route('/test/api', methods=['POST']):
    def method():
        visitor_ip = request.remote_addr
    

    In fastapi it should happen like this: request.client.host

    any way you can work with headers in your code and get many details of your client