---
title: ip_host
description: "ip\\_host function in PySpark: Returns the canonical representation of an IPv4 or IPv6 address."
canonical_url: https://docs.databricks.com/gcp/en/pyspark/reference/functions/ip_host
---

# ip\_host

**Applies to:** ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Databricks Runtime 18.2 and above

> [!NOTE]
> **Beta**
>
> This feature is in [Beta](https://docs.databricks.com/gcp/en/release-notes/release-types.md).

Returns the canonical representation of an IPv4 or IPv6 address.

For the corresponding SQL function, see [`ip_host` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/ip_host.md).

## Syntax

```python
from pyspark.databricks.sql import functions as dbf

dbf.ip_host(col=<col>)
```

## Parameters

| Parameter | Type                          | Description                                                         |
| --------- | ----------------------------- | ------------------------------------------------------------------- |
| `col`     | `pyspark.sql.Column` or `str` | A STRING or BINARY value representing a valid IPv4 or IPv6 address. |

## Examples

**Example 1**: Validate an IPv4 address.

```python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('192.168.1.5',)], ['ipv4'])
df.select(dbf.ip_host('ipv4').alias('result')).collect()
```

```Output
[Row(result='192.168.1.5')]
```

**Example 2**: Canonicalize an IPv6 address.

```python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('2001:0DB8:0000:0000:0000:0000:0000:0001',)], ['ipv6'])
df.select(dbf.ip_host('ipv6').alias('result')).collect()
```

```Output
[Row(result='2001:db8::1')]
```

**Example 3**: Validate an IPv4-mapped IPv6 address.

```python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('::ffff:192.0.2.128',)], ['ip'])
df.select(dbf.ip_host('ip').alias('result')).collect()
```

```Output
[Row(result='::ffff:192.0.2.128')]
```

**Example 4**: Validate an IPv4 address in binary format. The input is the binary representation of the IPv4 address `192.168.1.5`.

```python
from pyspark.databricks.sql import functions as dbf
from pyspark.sql.functions import hex
df = spark.createDataFrame([(bytearray([0xC0, 0xA8, 0x01, 0x05]),)], ['ip'])
df.select(hex(dbf.ip_host('ip')).alias('result')).collect()
```

```Output
[Row(result='C0A80105')]
```

**Example 5**: `None` input returns `None`.

```python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([(None,)], 'ip: string')
df.select(dbf.ip_host('ip').alias('result')).collect()
```

```Output
[Row(result=None)]
```
