0

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)
Prateek Dewan
  • 1,587
  • 3
  • 16
  • 29

1 Answers1

1

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

port = int(port)
furas
  • 134,197
  • 12
  • 106
  • 148
  • Doesnt Work Again********** host = input("Enter The Server Ip: ") port = input("Enter The Server Port: ") serversocket.bind((host, int(port))) ************* error : line 11, in serversocket.bind((host, int(port))) OSError: [WinError 10049] The requested address is not valid in its context – Emad Roshan Jun 15 '20 at 15:46
  • what address did you use as IP ? It seems you uses wrong value. It has to be your local IP (local IP of your network card or wifi) or `0.0.0.0`. And it is different problem which has nothing to do with previous problem. – furas Jun 15 '20 at 16:02