简体   繁体   中英

Socket Programming Server With (Ip,port) as variable

HI I want to have my ip and port as user input in my server but i get some errors i cant handle please help me...

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()

Error:

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)

or some times i get:

TypeError: an integer is required (got type str)

input() gives you port as string but socket needs it as integer

port = int(port)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM