-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq1.py
More file actions
34 lines (24 loc) · 1.21 KB
/
q1.py
File metadata and controls
34 lines (24 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#Q1. Write a Python program to perform the following:
#Validate a given public IP address to check if it follows the correct format (IPv4).
#Validate a given email address to check if it’s a valid Gmail address, considering:
#It should contain "@gmail.com".
#The username before "@gmail.com" should contain only lowercase letters , numbers and permitted symbols.
#Provide informative error messages for invalid IP or email.
import re
import ipaddress
def check_ip(ip):
rule = 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]?)$"
if not re.match(rule, ip):
return "galat IP hai bhai"
ipadd_obj= ipaddress.ip_address(ip)
return "Private hai" if ipadd_obj.is_private else "Public hai"
def check_email(email):
rule = r"^[a-z0-9._%+-]+@gmail\.com$"
rule= r"^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$" #this one i have use to check mail_id of other type like:- [email protected]
return "sahi hai" if re.match(rule, email) else "galat mail id"
def main():
ip = input("IP type kijye: ")
print(check_ip(ip))
email = input("Email type kijye: ")
print(check_email(email))
main()