Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
(Python)尝试一次扫描一个IP地址的端口范围时出错_Python_Sockets_Ip_Port - Fatal编程技术网

(Python)尝试一次扫描一个IP地址的端口范围时出错

(Python)尝试一次扫描一个IP地址的端口范围时出错,python,sockets,ip,port,Python,Sockets,Ip,Port,我试图一次扫描用户指定的端口范围中的一个IP地址,直到第四个八位字节达到255。但是,我遇到了一个错误,即没有定义名为“count”的变量,尽管我将其定义为=1,因此程序运行于每个IP地址,如53.234.12.1、53.234.12.2、53.234.12.3等 这就是我在说了这么多,做了这么多之后试图在口译员中展示的内容: 这是我的密码: import socket server = 'google.com' startPort = int(input("Enter started

我试图一次扫描用户指定的端口范围中的一个IP地址,直到第四个八位字节达到255。但是,我遇到了一个错误,即没有定义名为“count”的变量,尽管我将其定义为=1,因此程序运行于每个IP地址,如53.234.12.1、53.234.12.2、53.234.12.3等

这就是我在说了这么多,做了这么多之后试图在口译员中展示的内容:

这是我的密码:

import socket

server = 'google.com'


startPort = int(input("Enter started port number to scan: "))
endPort = int(input("Enter ending port number to scan: "))
threeOctet = str(input("Enter the first three octets of an IP to scan: "))
countFullIP = threeOctet + "." + str(count)
count = 1

for countFullIP in range(0,256):
    for count in range (startPort,endPort):
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((server,countFullIP))
            print('IP',countFullIP)
        except:
            print('IP is invalid')
        try:
             print('Port',count,'is open')
        except:
             print('Port',count,'is closed')
如蒙协助,将不胜感激。谢谢大家!

更换两条线路

countFullIP = threeOctet + "." + str(count)
count = 1

正如您所看到的,count在赋值之前被引用

根据评论中提到的附加要求更新代码

import socket

server = 'google.com'

startPort = int(input("Enter started port number to scan: "))
endPort = int(input("Enter ending port number to scan: "))
threeOctet = str(input("Enter the first three octets of an IP to scan: "))

for count in range(0,256):
    countFullIP = threeOctet + "." + str(count)
    for count in range (startPort,endPort):
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((server, countFullIP))
            print('IP',countFullIP)
        except:
            print('IP is invalid', countFullIP)
        try:
             print('Port',count,'is open')
        except:
             print('Port',count,'is closed')

谢谢你。我不再为未定义的变量获取错误。但是,还有最后一个问题,即IP地址没有显示。我怀疑在循环遍历每个IP地址后,我需要使用字符串连接来构建IP,虽然我认为这是使用countFullIP变量实现的,但我可能弄错了。@czium24您可能希望根据最后一个不显示IP的问题来更新代码。现在将显示此代码。正在显示捕获错误的循环的“除外”部分,或者换句话说,解释器认为IP地址无效,并且从技术上讲,正在显示一个错误以及在打开范围内指定的每个端口,这似乎不太可能。我在循环之前添加了count=1,以避免未定义的变量错误。countFullIP最初采用“127.0.0.1”。然后,您添加了一个for循环,其中countFullIP的取值范围为0到256。希望你做你缩进的事。
import socket

server = 'google.com'

startPort = int(input("Enter started port number to scan: "))
endPort = int(input("Enter ending port number to scan: "))
threeOctet = str(input("Enter the first three octets of an IP to scan: "))

for count in range(0,256):
    countFullIP = threeOctet + "." + str(count)
    for count in range (startPort,endPort):
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((server, countFullIP))
            print('IP',countFullIP)
        except:
            print('IP is invalid', countFullIP)
        try:
             print('Port',count,'is open')
        except:
             print('Port',count,'is closed')