python 解析域名,连接主机IP

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Canhui_WANG/article/details/78533301

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

展开阅读全文

没有更多推荐了,返回首页