pythonportscanning

TypeError: str, bytes or bytearray expected, not int when trying to make a Port Scanner


I know there are similar questions to my question, still can't find the solution to my specific case.

I'm trying to program a portscanner in 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...")

I'm getting this error : TypeError: str, bytes or bytearray expected, not int

I tried to change the command to :

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

Ip is a string now as expected in the argument but still not success, also saw some youtube videos and their code is working and mine isn't o-o

I would really appreciate your help with that :) Thanks in advance!


Solution

  • Reverse the order of your arguments.

    sock.connect_ex((ip, port))