We get the ip address from requests (python)

We get the ip address from requests (python)

When we receive any information about a domain, among other parameters, we learn its IP address. And getting it using python is not a big deal. However, let’s look at how to get the ip address directly from the request without using a direct socket access.

Most often, we use socket to get an IP address. And in the case when we need to perform only this operation, this is quite enough.

print(socket.gethostbyname("python.org"))

But it can be done a little differently, especially if we are already receiving some data from the server. At least the same headlines. Let’s take an example to see how to implement access to a raw socket object.

Installation of necessary libraries

In this case, we will need the requests library. To install it, write in the terminal:

pip install requests

Import the library into the script. Creating headers for the query

Once the necessary libraries are installed, we need to import them into the script. In this case, since we installed the requests library, we import it.

import requests

After that, we will create a dictionary with headers containing “User-Agent” and “Accept”. We will pass them to the request as a parameter to change the standard headers sent by python.

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 "
                  "YaBrowser/24.1.0.0 Safari/537.36",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,"
              "application/signed-exchange;v=b3;q=0.7"
}

We get headers and ip address

Let’s create the get_domain_info(domain: str) -> (dict, bool) function, which will receive the domain at the input, and return the headers sent by the server, as well as the domain’s IP address.
We will execute a request to receive headers, where we will transfer the domain address and headers. Let’s specify the quality allow_redirects = True. This is done so that redirection takes place. That is, in this case, we do the following: since we do not know the exact address of the site whose domain name is passed to the function, we will access it as a browser. To begin with, we will use the http protocol. If forwarding is enabled on the server, we will be automatically forwarded to the desired address. Also, stream=True. This is necessary to obtain the ip address from the request.

Let’s handle the raise_for_status() exception to cut off unnecessary status codes. In the case when the code is 200, we will receive the ip and port, which are returned in tuples when the following code is executed: res.raw._connection.sock.getpeername(). It is important that this data must be obtained at the beginning, before processing other request data, i.e. in the first place. And we will already partially receive the content from the headers sent by the server. Let’s return the dictionary from the received data to the user. In case of failure or incorrect status code, return False from the function.

def get_domain_info(domain: str) -> (dict, bool):
    try:
        res = requests.head(url=f"http://{domain}", headers=headers, stream=True, allow_redirects=True, timeout=5)
        try:
            res.raise_for_status()
            ip, port = res.raw._connection.sock.getpeername()
            serv = res.headers.get("Server")
            x_powered_by = res.headers.get('X-Powered-By')
            via = res.headers.get('Via')
            return {"ip": ip, "port": port, "addr": res.url, "server": serv, "x_powered_by": x_powered_by, "via": via}
        except Exception:
            return False
    except Exception:
        return False

Well, let’s create a main function, where we will request a domain from the user, send it to the function for obtaining information, and print out the received data in the form of a dictionary.

def main() -> None:
    domain = input("Введите домен: ")
    print(get_domain_info(domain))


if __name__ == "__main__":
    main()

I am not testing the output here as this code is an example. If you try to get the ip address using socket, the result will often be the same. However, if you see that the addresses are different, do not consider it a mistake. I was checking custom data by getting information from DNS. Here the thing is that there can be several NS-servers in one domain. And in this case, the closest value is returned. Well, free. In this case, the mechanism is still not completely clear to me. The main thing is that both addresses are correct.

Full script code

"""
pip install requests
"""
import requests

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 "
                  "YaBrowser/24.1.0.0 Safari/537.36",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,"
              "application/signed-exchange;v=b3;q=0.7"
}


def get_domain_info(domain: str) -> (dict, bool):
    try:
        res = requests.head(url=f"http://{domain}", headers=headers, stream=True, allow_redirects=True, timeout=5)
        try:
            res.raise_for_status()
            ip, port = res.raw._connection.sock.getpeername()
            serv = res.headers.get("Server")
            x_powered_by = res.headers.get('X-Powered-By')
            via = res.headers.get('Via')
            return {"ip": ip, "port": port, "addr": res.url, "server": serv, "x_powered_by": x_powered_by, "via": via}
        except Exception:
            return False
    except Exception:
        return False


def main() -> None:
    domain = input("Введите домен: ")
    print(get_domain_info(domain))


if __name__ == "__main__":
    main()

Let’s test the written code and get the headers and IP address for the python.org domain.

The result of the script

As you can see, the script worked correctly and returned the IP address, as well as information from the received headers.

And that’s probably all.

Thank you for attention. I hope you find this information useful!

Subscribe to our telegram channels! We have a lot of useful things!

Related posts