1. #!/usr/bin/python
    2. import socket
    3. # 初始信息
    4. host = 'www.google.com'
    5. port = 80
    6. # 解析域名获取IP
    7. ip = socket.gethostbyname(host)
    8. print('Ip address of ' + host + ' is ' + ip)
    9. # 通过IP连接主机
    10. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    11. s.connect((ip, port))
    12. print('Socket Connected to ' + host + ' on ip ' + ip)