如何泛洪IP地址以查找服务器

时间:2015-01-24 06:15:51

标签: python networking ip client server

我正在尝试使用暴力在我的客户端生成一系列本地IP地址,希望找到服务器正在等待连接的IP。我已经知道服务器的端口号正在倾听。有人请告诉我为什么我的代码不能正常工作。谢谢你提前

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")

1 个答案:

答案 0 :(得分:0)

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

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