Complete the code to print the IP address of a website using Python's socket module.
import socket ip = socket.gethostbyname([1]) print(ip)
The gethostbyname function requires the domain name as a string, so it must be enclosed in quotes.
Complete the code to split an IP address string into its four parts.
ip = "192.168.1.1" parts = ip.[1](".") print(parts)
join which combines strings instead of splitting.replace which changes characters but does not split.The split method breaks a string into parts based on the separator, here the dot ..
Fix the error in the code that checks if an IP address is valid by counting its parts.
ip = "10.0.0.256" parts = ip.split(".") if len(parts) [1] 4: print("Valid IP") else: print("Invalid IP")
= instead of == causes syntax errors.!= reverses the logic.To check if the IP address has exactly 4 parts, use the equality operator ==.
Fill both blanks to create a dictionary mapping domain names to their IP addresses.
import socket domains = ["example.com", "localhost"] ip_map = {domain: [1] for domain in domains if domain [2] "localhost"} print(ip_map)
== instead of != changes the filter logic.The dictionary comprehension gets the IP for each domain except 'localhost' by checking if domain is not equal (!=) to 'localhost'.
Fill all three blanks to create a list of IP addresses from a list of domain names.
import socket domains = ["example.com", "localhost", "openai.com"] ips = [[1] for [2] in domains if [3] != "localhost"] print(ips)
The list comprehension calls socket.gethostbyname(domain) for each domain in domains, skipping 'localhost'.