0

嗨,我想在我的服务器中将我的 ip 和端口作为用户输入,但是我遇到了一些我无法处理的错误,请帮助我...

import socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


host = input("Enter The Server Ip: ")
port = input("Enter The Server Port: ")

serversocket.bind((host, port)) 

serversocket.listen(10)

while True :
    clientsocket, address = serversocket.accept()

    print("Received Connection From %s" % str(address))

    message = 'Connection Established' + "\r\n"
    clientsocket.send(message.encode("ascii"))

    clientsocket.close()

错误 :

line 11, in <module>
    serversocket.bind((host, port))  # Host will be replaced with IP, if changed and not running on host
TypeError: an integer is required (got type str)

或者有时我得到:

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

1 回答 1

1

input()给你port作为字符串,但socket需要它作为integer

port = int(port)
于 2020-06-14T22:32:31.817 回答