为什么我会看到 python 套接字模块的 TypeError?

why do I see TypeError for python socket module?

我正在学习python,最近我正在尝试使用套接字模块。下面是客户端逻辑。

import socket
from threading import Thread
import ipaddress


lic_server_host = input("Please enter the hostname: ")
port = input("Please enter the License service port number: ")
client_socket = socket.socket()
client_socket.connect((lic_server_host, port))


def receive_data():
    while True:
        data = client_socket.recv(1000)
        print(data.decode())


def sending_data():
    while True:
        user_input = input()
        client_socket.sendall(user_input.encode())


t = Thread(target=receive_data)
t.start()
sending_data()

这里我将用户的输入作为主机名。然而。上面的程序无法将主机名转换为整数。我低于错误

client_socket.connect((lic_server_hostname, port))
TypeError: an integer is required (got type str)

我尝试使用一些 python 方法来解决这个问题,方法是在用户输入中引入 for 循环,如下所示

lic_server_host = input("Please enter the License server hostname: ")
for info in lic_server_hostname:
    if info.strip():
        n = int(info)
port = input("Please enter the License service port number: ")
client_socket = socket.socket()
client_socket.connect((n, port))

但现在我得到以下错误:

client_socket.connect((n, port))
TypeError: str, bytes or bytearray expected, not int

因此,根据错误,我在 "n" 上使用了 str() 函数。但是当我这样做时,我得到以下错误:

n = int(info)
ValueError: invalid literal for int() with base 10: 'l'

我也在互联网上搜索了上述错误,但解决方案对我没有帮助。

请帮我理解我的错误。

谢谢

input returns 当 connect 需要端口为 int 时的字符串。

client_socket.connect((lic_server_host, int(port)))