因此,我正在写一个项目,检查错误的电子邮件字符。我在这里搜索时没有发现有帮助的东西,也没有关于duckduckgo的东西。它运行正常,但我在shell上看到了以下内容:
e mail: abc @ xyz.com *** ERROR: 2. The number of @'s in your email is suspect. ***
e mail: .abc@xyz.com *** ERROR: 2. The number of @'s in your email is suspect. ***
e mail: abc@xyz.c *** ERROR: 2. The number of @'s in your email is suspect. ***注意,testAtsign函数(2. The number of @'s in your email is suspect.)在某种程度上接管了应该显示其他错误的空间,例如不允许的特殊字符?
我认为,我的testSpecialChars函数存在一个问题,就是允许testAtsign函数接管。这会不会是不允许的清单的问题呢?
任何想法都会受到极大的赞赏。
emailList = ["abc@xyz.com",
"abc@@xyz.com",
"@xyz.com",
"abc.xyz.com",
"abc@x.yz",
"abc@xyz.c",
"a@b.c",
"abc@xyz..com",
"abc.@xyz.com",
"abc@.xyz.com",
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@aaaaaa.aaaaa",
"' or 1=1 '==",
"abc@xyz.$%",
"abc@xyz.()",
"abc'@xyz.com",
"aaaaa@aaaaa",
"abc @ xyz.com",
".abc@xyz.com",
"abc@xyz.c"]
def errorMessage(email, error):
print("e mail: {} \t*** ERROR: {} ***".format(email, error))
def testAtsign (email):
if "@" in email:
#should be 1 @
#are there more?
atCount = 0
for character in email:
if character == "@":
atCount += 1
if atCount != 1:
errorMessage(email, "2. The number of @'s in your email is suspect.")
return True
elif email[0] == "0":
errorMessage(email, "3.The @ is not in a valid position.")
return True
else:
testLast5 = email[-5]
if "@" in testLast5:
errorMEssage(email, "4. Okay, your @ is not in the last 5 characters, whats up with you?")
return True
else:
return False
else:
errorMessage(email, "5. your @ is missing")
return True
def testDot(email):
if "." in email:
#has to be at least ONE
if email[0] == ".":
errorMessage(email, "10. Your '.' is in the first position.")
return True
testLast2 = email[-2:]
if "." in testLast2:
errorMessage(email, "11. Your '.' is in the last position.")
return True
#should not be doubled or next to @
elif ".." in email or ".@" in email or "..@" in email or "@." in email or "@.." in email:
errorMessage(email, "6. Were sensing an erorr in your '.' config.")
return True
else:
errorMessage(email, "7. Where is the '.'?")
return True
def testSpecialChars(email) :
#first test for spaces
if " " in email:
errorMessage(email, "8. We dont allow spaces in our emails here.")
return True
#create list of unallowables
unallowable = "! # $ % ^ & * ( ) : ; < > ? / { } =".split()
#add quotes
unallowable.append('"')
unallowable.append("'")
for character in email:
if character in unallowable:
errorMEssage(email, "9. Character {} is not allowed".format(character))
return True
for email in emailList:
foundError = False
if len(email) < 7 or len(email) > 30:
errorMessage(email, "1. Invalid Length") #labeling the errors with numbers to keep track
foundError = True
if not foundError:
foundError = testAtsign(email)
if not foundError:
foundError = testDot(email)
if not foundError:
foundError = testSpecialChars(email)
if not foundError:
print("Rad, your email seems valid.".format(email))
print("flag")发布于 2019-03-16 07:56:14
在这个函数中,testAtsign代码在电子邮件地址上循环以计数"@“字符,并对循环中的计数进行检查。因此,除非地址中的第一个字符是"@“,否则函数将始终返回True,因为"@”的计数不等于1。
通过将检查移出循环来修正这个问题;实际上,可以通过使用Python的计数方法来获取每个地址中"@“字符的数量来完全删除这个循环。
固定的函数如下所示:
def testAtsign(email):
if "@" in email:
# should be 1 @
# are there more?
atCount = email.count("@")
if atCount != 1:
errorMessage(email, "2. The number of @'s in your email is suspect.")
return True
elif email[0] == "0": # <- this might need fixing?
errorMessage(email, "3.The @ is not in a valid position.")
return True
else:
testLast5 = email[-5] # <- this only gets a single character
if "@" in testLast5:
errorMessage(
email,
"4. Okay, your @ is not in the last 5 characters, whats up with you?",
)
return True
else:
return False
else:
errorMessage(email, "5. your @ is missing")发布于 2019-03-15 02:41:51
您首先检查的是testAtsign()。在函数内部,如果检测到任何错误,则将foundError的值更改为“True”。由于foundError的值被更新为True,所有其他的'if‘条件(对于testDot()和testSpecialChars())都失败了,根本不执行。if语句if len(email) < 7 or len(email) > 30也是如此。一旦这是真的,并且foundError在其内部被更新为true,即使是testAtSign()也不会执行。
https://stackoverflow.com/questions/55174679
复制相似问题