python 解析域名,连接主机IP

1. 实现代码(test.py)

#!/usr/bin/python
import socket

# 初始信息
host = 'www.google.com'
port = 80

# 解析域名获取IP
ip = socket.gethostbyname(host)
print 'Ip address of ' + host + ' is ' + ip

# 通过IP连接主机
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip, port))
print 'Socket Connected to ' + host + ' on ip ' + ip

参考
1. https://www.cnblogs.com/hazir/p/python_socket_programming.html

你可能感兴趣的:(计算机网络)