最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Python IPv4 Validation Only Checking First Byte Instead of All Four Bytes - Stack Overflow

programmeradmin10浏览0评论

I'm writing a Python function to validate IPv4 addresses. for a Task of CS50 Python. My function should:

Ensure the format is X.X.X.X, where each X is a number between 0-255. Return True for valid IPv4 addresses and False otherwise.

However, my test case is failing with this error message:

:( test_numb3rs.py catches numb3rs.py only checking if first byte of IPv4 address is in range**

check50 error

my main code is:

import re

def main():
    print(validate(input("IPv4 Address: ")))

def validate(ip):
    match = re.fullmatch(r"(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})", ip)
    return bool(match) and all(0 <= int(octet) <= 255 for octet in match.groups())

if __name__ == "__main__":
    main()

and my Test Code is:

from numb3rs import validate
def test_validate():
    # Valid cases
    assert validate("127.0.0.1") == True
    assert validate("255.255.255.255") == True

    # Invalid cases
    assert validate("512.512.512.512") == False
    assert validate("1.2.3.1000") == False
    assert validate("cat") == False

I have tried following multiple solutions available but still could not understand the issue. I Tried using exit(1) in a different version of my code, but that also did not solve the issue. Instead, it made the cases where any one octet goes above 255 fail.

I'm writing a Python function to validate IPv4 addresses. for a Task of CS50 Python. My function should:

Ensure the format is X.X.X.X, where each X is a number between 0-255. Return True for valid IPv4 addresses and False otherwise.

However, my test case is failing with this error message:

:( test_numb3rs.py catches numb3rs.py only checking if first byte of IPv4 address is in range**

check50 error

my main code is:

import re

def main():
    print(validate(input("IPv4 Address: ")))

def validate(ip):
    match = re.fullmatch(r"(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})", ip)
    return bool(match) and all(0 <= int(octet) <= 255 for octet in match.groups())

if __name__ == "__main__":
    main()

and my Test Code is:

from numb3rs import validate
def test_validate():
    # Valid cases
    assert validate("127.0.0.1") == True
    assert validate("255.255.255.255") == True

    # Invalid cases
    assert validate("512.512.512.512") == False
    assert validate("1.2.3.1000") == False
    assert validate("cat") == False

I have tried following multiple solutions available but still could not understand the issue. I Tried using exit(1) in a different version of my code, but that also did not solve the issue. Instead, it made the cases where any one octet goes above 255 fail.

Share Improve this question edited Mar 21 at 0:48 kcw78 8,1363 gold badges17 silver badges51 bronze badges asked Mar 20 at 21:38 Rubab KhaskheliRubab Khaskheli 35 bronze badges 1
  • Why not use the built in ipaddress module? – JRiggles Commented Mar 21 at 12:14
Add a comment  | 

2 Answers 2

Reset to default 0

All of your tests are accurate checks. However, check50 is very picky about how it expects you to test the validate() function. There are at least 2 issues you need to address. I will describe each below.

This line has invalid numbers for all 4 bytes of the address:

assert validate("512.512.512.512") == False

So, after it fails on the 1st byte, the others aren't considered. Change it to these 2 lines to resolve that issue:

assert validate("512.5.5.5") == False
assert validate("5.5.5.512") == False

This line has a 4 digit value at the 4th byte and that is considered a different error.

assert validate("1.2.3.1000") == False

Change it to this line to resolve that issue:

assert validate("1.2.3.999") == False

Note: Once you modify the 1st test, you really don't need the 2nd test.

import re
def validate(ip):
    match = re.fullmatch(r"^(25[0-5]|2[0-4][0-9]|1?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|1?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|1?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|1?[0-9][0-9]?)$", ip)
    return True if match else False

ip = "127.0.0.1"
r = validate(ip)
print(r) # Output : True

ip = "255.255.255.255"
r = validate(ip)
print(r) # Output : True

ip = "512.512.512.512"
r = validate(ip)
print(r) # Output : False

ip = "1.2.3.1000"
r = validate(ip)
print(r) # Output : False

ip = "cat"
r = validate(ip)
print(r) # Output : False

Idea Behind:

25[0-5] Matches numbers from 250 to 255

2[0-4][0-9] Matches numbers from 200 to 249

1?[0-9][0-9]? Matches numbers from 0 to 199

\. Matches the literal dot (.) between octets

^ and $ Ensure the entire string is an IPv4 address

发布评论

评论列表(0)

  1. 暂无评论