Spaces:
Running
Running
File size: 990 Bytes
6b509f7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#
# SPDX-FileCopyrightText: Hadad <[email protected]>
# SPDX-License-Identifier: Apache-2.0
#
import random # Import the random module to generate random numbers
def generate_ip() -> str:
"""
Generate a random IPv4 address as a string.
Returns:
str: A randomly generated IPv4 address in dotted decimal notation,
where each octet is a number between 1 and 254 inclusive.
Explanation:
This function creates an IP address by generating four random integers,
each representing one octet of the IP address.
The range 1 to 254 is used to avoid special addresses like 0 (network) and 255 (broadcast).
The four octets are then joined together with dots to form a standard IPv4 address string.
"""
# Generate four random integers between 1 and 254 inclusive, convert each to string,
# then join them with '.' to form a valid IPv4 address string
return ".".join(str(random.randint(1, 254)) for _ in range(4)) |