Python2仿冒用户名检测脚本开发求助:特定字符替换类仿冒识别
Python 2 Username Spoof Detection Solution
Hey there! Let’s work through this username spoof detection script you’re building. Here’s a straightforward, effective approach to implement the logic you need:
Core Approach
The key idea is to standardize characters that are commonly used to spoof others, then compare the standardized versions of the two usernames. Here’s the step-by-step breakdown:
- First, validate that both usernames have the same length (if not, they can’t be spoofs).
- Define a mapping of easily confused characters to a shared "standard" character (e.g., map both
0andOtoO, bothIandltol). - Convert each character in both usernames to its standard form, then check if all converted characters match.
Full Python 2 Code Implementation
def is_spoofed_username(username1, username2): # Early exit if lengths don't match if len(username1) != len(username2): return False # Mapping of confused characters to a standard equivalent confusion_map = { '0': 'O', 'O': 'O', 'I': 'l', 'l': 'l', # Add more spoof pairs here as needed: # '1': 'l', # '8': 'B', # '5': 'S' } # Compare each character pair after standardization for char1, char2 in zip(username1, username2): # Get standard char (fallback to original if not in map) std_char1 = confusion_map.get(char1, char1) std_char2 = confusion_map.get(char2, char2) if std_char1 != std_char2: return False # All characters matched after standardization return True # Test the function with your examples print(is_spoofed_username('allison', 'aIIison')) # Output: True print(is_spoofed_username('john_doe', 'j0hn_d0e'))# Output: True print(is_spoofed_username('alice', 'alic3')) # Output: False print(is_spoofed_username('bob', 'bobby')) # Output: False
How This Works
- Length Check: We first check if the usernames are the same length—this is a fast way to rule out non-spoof cases without extra processing.
- Confusion Mapping: The
confusion_mapensures that any character used to spoof another gets converted to the same standard value. For example,I(uppercase i) andl(lowercase L) both becomel, so comparing them will register as a match. - Character-by-Character Comparison: We iterate through each pair of characters, convert them to their standard form, and check for matches. If any pair fails, we immediately return
False.
Extending the Script
If you need to cover more spoof scenarios, just add new entries to the confusion_map. For example, to catch 1 being used as l or 5 being used as S, simply add those key-value pairs to the dictionary.
内容的提问来源于stack exchange,提问作者positivetypical
相关产品推荐
相关产品推荐

