TypeError: an integer is required (got type str) Python

TypeError: an integer is required (got type str) Python

代码如下:

import socket
import time

target = input("[+] Target ip:  ")
port = input("[+] Port:  ")
print("[+] Sending package to:   > " + target, "<")
time.sleep(1)

byte_message = bytes("Test", "utf-8")
opened_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
opened_socket.sendto(byte_message, (target ,port))

我在第 12 行收到此错误:

TypeError: an integer is required (got type str)

将端口转换为 int。

try:
    port = int(input("[+] Port:  ")) # Maybe verify value of port is valid such as ensuring its not negative
except:
    # Specify what to do if port is not a number ie invalid
    print("Please specify valid input port")