0

How i get IP Address from this request HTTP using python? I found some posts using C#, like this: Get remote IP address in Azure function

I have this code:

import logging
import json
import socket
import getpass
import os
from requests import get
import azure.functions as func
from azure.common.client_factory import    get_client_from_json_dict,get_client_from_cli_profile
from azure.mgmt.sql import SqlManagementClient
from azure.mgmt.resource import SubscriptionClient
from azure.common.credentials import ServicePrincipalCredentials

def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')

Myip = get('https://api.ipify.org').text
username = getpass.getuser()

return func.HttpResponse(
         f"List of IPs: {str(Myip)}",
         status_code=200
    )

The image shows the result of the local request.

https://i.stack.imgur.com/AkZKm.png

2 Answers2

0

The result of your request should be saved to a response variable.
Before you read the body of your response, you can use this bit of code to get the IP address. Say your response variable is called rsp

print rsp.raw._fp.fp._sock.getpeername()

There is a similar question here = How do I get the IP address from a http request using the requests library?

More info here, Python: get remote IP from HTTP request using the requests module

Update Python3.8 with requests 2.22.0

resp = requests.get('https://www.google.com', stream=True)
resp.raw._connection.sock.getsockname()
Troy Witthoeft
  • 2,498
  • 2
  • 28
  • 37
  • Hello Troy, thanks for answer. I have no request, my azure function receive a request HTTP, my code is: import logging import json import socket from requests import get import azure.functions as func from azure.common.client_factory import get_client_from_json_dict,get_client_from_cli_profile from azure.mgmt.sql import SqlManagementClient from azure.mgmt.resource import SubscriptionClient from azure.common.credentials import ServicePrincipalCredentials def main(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') – Reginaldo Silva Sep 10 '20 at 18:46
  • Hi Troy, i've added my code in post, how do i get this response? I have the func.HttpResponse in azure functions – Reginaldo Silva Sep 10 '20 at 18:52
  • Yes, your Azure function is receiving a request. Are you looking for that incoming IP address? Or -now that you have updated your code to make an additional outbound request - are you looking for the IP of `https://api.ipify.org`? Which one? Take a look at this question here = https://stackoverflow.com/questions/22492484/how-do-i-get-the-ip-address-from-a-http-request-using-the-requests-library – Troy Witthoeft Sep 10 '20 at 19:32
0

You can get the source IP of the incoming request from the x-forwarded-for header in the req object. I use the following code to log it:

    if "x-forwarded-for" in req.headers:
        source_ip = req.headers["x-forwarded-for"].split(':')[0]
        logging.info("Incoming request from IP: " + source_ip)