Answer to Question #296966 in Python for faisal

Question #296966

First Run Server.py on the HOST side and run Client.py on the User side

After running Client.py on the User side I need four functions that the user side runs: REGISTER user, Display Registered users, De-register User

To Register a user to the Host server (aka Connect to the host server) you must have the input: “REGISTER <username (any name)>, <IP address of that user that wants to register> <PORT #s (range limit is 49152 through 65535)>”

After running the REGISTER COMMAND, it will either output “REGISTERED” or “NOT REGISTERED” on the user client-side If “REGISTERED” output is displayed, the User client should now be connected to the Host Server and Host Server can either save registered(connected) users in a list/database and/or output on Host Server-side “Connection at <username of user client> <IP ADDRESS of user client> <port # of user client (range limit is 49152 through 65535 >”


1
Expert's answer
2022-02-12T09:44:10-0500
class User:
    def __init__(self, name, ip, port):
        self.name = name
        self.ip = ip
        self.port = port

users = []
command = ''

def commands__list():
    print("""
    1. REGISTER <username>, <IP address> <PORT>
    2. USERS LIST
    3. DELETE USER
    4. EXIT
    """)

def users__list():
    if len(users) == 0:
        print('\nNo registered users yet')
    else:
        num = 1
        for user in users:
            print(f"{num}. {user.name} {user.ip} {user.port}")
            num += 1

def register__user(com):
    data = com.split(' ')
    if len(data) == 4:
        if data[3].isdigit():
            if int(data[3]) > 49152 and int(data[3]) < 65535:
                users.append(User(data[1], data[2], data[3]))
                print('\nREGISTERED')
            else:
                print('\nNOT REGISTERED')
                print('Port range limit is 49152 through 65535')
        else:
            print('\nPort must be numbers (range limit is 49152 through 65535)')
    else:
        print('\nWrong data')

def delete__user():
    users__list()
    if len(users) != 0:
        print('\nWhich user do you want to delete? Enter his number')
        num = input()
        if num.isdigit():
            num = int(num)
            if num > 0 and num <= len(users):
                users.remove(users[num-1])
                print('\nRemoval was successful')
            else:
                print('\nWrong number')
        else:
            print("\nYou didn't enter a number")

while (command != 'EXIT'):
    commands__list()
    command = input()
    if "REGISTER" in command:
        register__user(command);
    elif command == "USERS LIST" or command == "2":
        users__list()
    elif command == "DELETE USER" or command == "3":
        delete__user()
    elif command == "EXIT" or command == "4":
        break
    else:
        print('\nUnknown command')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS