【问题标题】:Python3 check if Server OnlinePython3 检查服务器是否在线
【发布时间】:2021-11-19 18:47:03
【问题描述】:

我有一个生成随机 IPv4 的脚本。 现在我想检查它们是否真的存在。 这是我当前的脚本:

import random

def gen():
    global ip
    ip = (str(random.randint(0, 255)) + '.' + str(random.randint(0, 255)) + '.' + str(random.randint(0, 255)) + '.' + str(random.randint(0, 255)))

gen()

我想获得尽可能多的在线 IP,所以它应该非常快。

感谢您的帮助!

【问题讨论】:

    标签: python-3.x sockets server


    【解决方案1】:

    检查是否可达

    import nmap, socket
    
    your_ip = "196.64.12.3"
    scanner = nmap.PortScanner()
    host = socket.gethostbyname(your_ip)
    scanner.scan(host, '1', '-v')
    print("IP Status: ", scanner[host].state())
    

    或尝试ping ip:

    import os
    
    your_ip = "196.64.12.3"
    ret = os.system("ping -o -c 3 -W 3000 {}".format(your_ip)
    if ret != 0:
        print "IP Status -> Alive"
    else:
        print "IP Status -> Dead"
    

    【讨论】:

      猜你喜欢
      • 2011-07-10
      • 1970-01-01
      • 2017-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多