1

I am creating a simple chat server/client program in python right now. I am wondering how I can create a port using the command line. I know how to hard code a port in and have that actually working, but I am trying to allow the server to take in the port from the command line and then have the client enter the port they are trying to connect to instead of just having both the ports just hardcoded in.

Here is my code that I have for the hard coded ports :

import socket


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM);


s.bind((socket.gethostname(), 1234))
s.listen(5)


while True:
    clientsocket, address = s.accept()
    print(f"Connection from {address} has been established")
    clientsocket.send(bytes("Welocme to the server","utf-8"))

and here is what I tried changing it too

portnum = input("Enter port number")
s.bind((socket.gethostname(), portnum))
s.listen(5)

0 Answers0