# Obtaining IP Address using socket wrapped in class object

**URL:** <https://discuss.python.org/t/obtaining-ip-address-using-socket-wrapped-in-class-object/48830>\
**Category:** Python Help\
**Created:** [March 18, 2024, 3:48am UTC](https://discuss.python.org/t/obtaining-ip-address-using-socket-wrapped-in-class-object/48830 "2024-03-18T03:48:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![pinokioblabla](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@pinokioblabla](https://discuss.python.org/u/pinokioblabla)\
**Post date:** [March 18, 2024, 3:48am UTC](https://discuss.python.org/t/obtaining-ip-address-using-socket-wrapped-in-class-object/48830/1 "2024-03-18T03:48:17Z")

</div>

Hello, I have been looking but couldn’t find anything. So inside class object I am using socket library to get current PC’s IP Address, but I am using socket library inside **init**. Whenever I tried to print the class I always got: `<class ' __main__.ActivityWatchClient'>`. The code:

```python
class ActivityWatchClient:
    def __init__ (
        self,
        client_name: str = "unknown",
        testing=False,
        host=None,
        port=None,
        protocol="http",
    ) -> None:
        """
        A handy wrapper around the aw-server REST API. The recommended way of interacting with the server.

        Can be used with a `with`-statement as an alternative to manually calling connect and disconnect in a try-finally clause.

        :Example:

        .. literalinclude:: examples/client.py
            :lines: 7-
        """
        self.cert_content = """ Certificate """
        
        self.testing = testing

        self.client_name = client_name
        self.client_username = getpass.getuser()
        self.client_hostname = socket.gethostname()
        self.client_ip = ""
        try:
            self.client_ip = socket.gethostbyname(self.client_hostname)
        except:
            self.client_ip = "127.0.0.1"
        _config = load_config()
        server_config = _config["server" if not testing else "server-testing"]
        client_config = _config["client" if not testing else "client-testing"]

```

Am I doing anything wrong with this code? My expectation is getting the IP Address of the computer currently running this program. Please help.

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [March 18, 2024, 3:58am UTC](https://discuss.python.org/t/obtaining-ip-address-using-socket-wrapped-in-class-object/48830/2 "2024-03-18T03:58:02Z")

</div>

> [@pinokioblabla](#):
>
> Whenever I tried to print the class I always got: `<class ' __main__.ActivityWatchClient'>`

Okay, and why is this a problem? Exactly what do you think you should see instead, and why do you think you should see that?

> [@pinokioblabla](#):
>
> My expectation is getting the IP Address of the computer currently running this program.

When I create a `ActivityWatchClient`, is _that thing itself_ the address that you want? Or is it, instead, an object _that contains_ the IP address information?

---

<div class="post-metadata">

**Author:** ![csm10495](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/csm10495/32/9731_2.png) [@csm10495](https://discuss.python.org/u/csm10495)\
**Post date:** [March 18, 2024, 3:58am UTC](https://discuss.python.org/t/obtaining-ip-address-using-socket-wrapped-in-class-object/48830/3 "2024-03-18T03:58:43Z")

</div>

I would say to start in the interpreter with something like:

```python
import socket

print(socket.gethostbyname(socket.gethostname()))

```

Does it give you the IP address you wanted?

Other notes:

IP addresses are usually a bit more complicated than it seems. There are local ip addresses, global ones, etc. then we have ipv4 and ipv6 addresses.

This type of code would generally only get a local ipv4 address. To get the others, you’d need other logic.

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [March 18, 2024, 4:29am UTC](https://discuss.python.org/t/obtaining-ip-address-using-socket-wrapped-in-class-object/48830/4 "2024-03-18T04:29:40Z")

</div>

I’d just like to point out that ‘bare’ excepts are a bad idea because they catch _all_ errors, even a `NameError` caused by a spelling mistake.

Catch only those exceptions that you’re going to handle.

---

<div class="post-metadata">

**Author:** ![pinokioblabla](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@pinokioblabla](https://discuss.python.org/u/pinokioblabla)\
**Post date:** [March 18, 2024, 4:42am UTC](https://discuss.python.org/t/obtaining-ip-address-using-socket-wrapped-in-class-object/48830/5 "2024-03-18T04:42:44Z")

</div>

- First point: Not pointing that as a problem because it does not throw any error, that is what usually happens when you try to output class.
- Second point: Correct, I am looking for a way to output the result of `self.client_ip`, because this program does not work as it should (expected result is outputting ip address of the device this program is running in), so I am looking for any bug which might be the cause

---

<div class="post-metadata">

**Author:** ![pinokioblabla](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@pinokioblabla](https://discuss.python.org/u/pinokioblabla)\
**Post date:** [March 18, 2024, 5:08am UTC](https://discuss.python.org/t/obtaining-ip-address-using-socket-wrapped-in-class-object/48830/6 "2024-03-18T05:08:59Z")

</div>

Hello, yes I have tried that too and it works well as expected. Problem is I need to wrap this in class, which makes it complicated. And I only need IPv4 so socket is sufficient.

---

<div class="post-metadata">

**Author:** ![pinokioblabla](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@pinokioblabla](https://discuss.python.org/u/pinokioblabla)\
**Post date:** [March 18, 2024, 5:39am UTC](https://discuss.python.org/t/obtaining-ip-address-using-socket-wrapped-in-class-object/48830/7 "2024-03-18T05:39:54Z")

</div>

Thanks, will make sure to be more detailed.

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [March 18, 2024, 1:05pm UTC](https://discuss.python.org/t/obtaining-ip-address-using-socket-wrapped-in-class-object/48830/8 "2024-03-18T13:05:22Z")

</div>

> [@pinokioblabla](#):
>
> Correct, I am looking for a way to output the result of `self.client_ip`

Is `print(some_instance.client_ip)` not sufficient?

---

<div class="post-metadata">

**Author:** ![sr-murthy](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/sr-murthy/32/12476_2.png) [@sr-murthy](https://discuss.python.org/u/sr-murthy)\
**Post date:** [March 18, 2024, 1:40pm UTC](https://discuss.python.org/t/obtaining-ip-address-using-socket-wrapped-in-class-object/48830/9 "2024-03-18T13:40:27Z")

</div>

I’m not sure I’ve understood your goal, but if you want an object to represent your local IP address, you could use the [`ipaddress`](https://docs.python.org/3/library/ipaddress.html) standard library. So something like this:

```python
>>> from ipaddress import IPv4Address
>>> loopback = IPv4Address(socket.gethostbyname(socket.gethostname()))
>>> loopback
IPv4Address('127.0.0.1')
>>> loopback.is_loopback
True
>>> localhost.is_private
True

```

---

<div class="post-metadata">

**Author:** ![pinokioblabla](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@pinokioblabla](https://discuss.python.org/u/pinokioblabla)\
**Post date:** [March 19, 2024, 8:00am UTC](https://discuss.python.org/t/obtaining-ip-address-using-socket-wrapped-in-class-object/48830/10 "2024-03-19T08:00:01Z")

</div>

Small update, I finally managed to output the `client_ip`, eventually I need to fill in one of the self argument.
