如何泛洪IP地址来寻找服务器

How to flood IP addresses to find server

我正在尝试使用蛮力在我的客户端中生成一系列本地 ip 地址,希望找到正在等待 connection.I 已经知道服务器所在端口号的服务器 IP是listening.Can有人请告诉我为什么我的代码不是working.Thanks提前

from socket import *
s=socket(AF_INET,SOCK_STREAM)
ip="192.168."
print("Searching for IP...")
for i in range(0,256):
    for j in range(0,256):
        host=ip+str(i)+"."+str(j)
        add=(host,1060)
        try:
            s.connect(add)
            print("Server Found:",add)
        except:
            print(host,"failed")

正如评论所说,只需在您的代码中添加一些内容即可:

from socket import *

ip="192.168."
print("Searching for IP...")
for i in range(0,256):
    for j in range(0,256):
        host=ip+str(i)+"."+str(j)
        add=(host,1060)
        print host
        s=socket(AF_INET,SOCK_STREAM) # create the socket in loop
        s.settimeout(4) # set the timeout value as 4 seconds
        try:
            s.connect(add)
            print("Server Found:",add)
        except:
            print(host,"failed")
        s.close() # close it
        del s

settimeout 将使代码更有效并且不会加快太多时间。

使用相同的套接字将是错误的

[Errno 10022]

喜欢下面的代码:

from socket import *

ip="192.168."
print("Searching for IP...")
s=socket(AF_INET,SOCK_STREAM) # create the socket in loop
for i in range(0,256):
    for j in range(0,256):
        host=ip+str(i)+"."+str(j)
        add=(host,1060)
        print host

        #s.settimeout(4) # set the timeout value
        try:
            s.connect(add)
            print("Server Found:",add)
        except Exception, error:
            print(host,"failed")
            print error
        #s.close() # close it
        #del s