类型错误:在尝试制作端口扫描器时需要 str、bytes 或 bytearray,而不是 int

问题描述

我知道有与我的问题类似的问题,但仍然找不到针对我的具体情况的解决方案。

我正在尝试用 python 编写端口扫描程序:

import socket


def portscan():
    sock = socket.socket(socket.AF_INET,socket.soCK_STREAM)
    ip = input("Enter your the ip of the target: ")
    try:
        for port in range(1000):
            if sock.connect_ex((port,ip)):
                print(f"Port {port} is open")
            else:
                print(f"Port {port} is closed")
    except SystemError or SystemExit:
        print("System Exiting...")

我收到此错误:TypeError: str,bytes or bytearray expected,not int

我尝试将命令更改为:

if sock.connect_ex((port,str(ip)))

Ip 现在是一个字符串,正如参数中的预期,但仍然没有成功,也看到了一些 youtube 视频,他们的代码正在运行,而我的不是 o-o

非常感谢您的帮助:) 提前致谢!

解决方法

颠倒你的论点顺序。

sock.connect_ex((ip,port))