python 域名转IP 在查看制定端口是否开放,pythonip,#!/usr/local


#!/usr/local/bin/python#-*- coding: UTF-8 -*-import socket#获取域名的IP地址def www_ip(name):  #域名转IP    try:        result = socket.getaddrinfo(name, None)        return result[0][4][0]    except:        return 0def ip_port(ip):  #查看IP端口是否开放    port=21    s=socket.socket()    #address="127.0.0.1"    try:        s.connect((ip,port))        #print 'IP:',ip,"port:",port,"以开放"        return 1    except socket.error,e:        #print 'IP:',ip,"port:",port,"未开放"        return 0if __name__=='__main__':    www= "www.163.com"    IP=www_ip(www)    if IP:        print www,"ip地址:",IP        if ip_port(IP):            print IP,"开放21端口"        else:            print IP,"未开21放端口"    else:        print www,"域名转IP失败"#该片段来自于http://byrx.net

评论关闭