Untitled

 avatar
unknown
python
2 years ago
3.0 kB
8
Indexable
import socket
import threading
import json
import os
import sys
from colorama import Fore, init, Style
init(autoreset = True)

def enter_server():
    global nickname
    global password
    nickname = input("Choose Your Nickname:")
    if nickname == 'ixelizm':
        password = input("Enter Password for Admin:")

    # Store the ip and port number for connection
    ip = "7.tcp.eu.ngrok.io"
    port = 12821
    global client
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    #Connect to a host
    client.connect((ip,port))

os.system('cls||clear')
enter_server()

stop_thread = False

def recieve():
    while True:
        global stop_thread
        if stop_thread:
            break
        try:
            message = client.recv(1024).decode('utf-8')
            if message == 'NICK':
                client.send(nickname.encode('utf-8'))
                next_message = client.recv(1024).decode('utf-8')
                if next_message == 'PASS':
                    client.send(password.encode('utf-8'))
                    if client.recv(1024).decode('utf-8') == 'REFUSE':
                        print("Connection is Refused !! Wrong Password")
                        stop_thread = True
                elif next_message == 'BAN':
                    print('Connection Refused due to Ban')
                    client.close()
                    stop_thread = True
            else:

                if nickname == message.split(":")[0]:
                    user, text = message.split(":")
                    print(Fore.GREEN + Style.BRIGHT + user + ":",Fore.WHITE + Style.BRIGHT + text)
                else:
                    if ":" in message:
                        user, text = message.split(":")
                        print(Fore.RED + Style.BRIGHT + user + ": " + Fore.WHITE + Style.BRIGHT + text)
                    else:
                        print(message)
        except:
            print('Error Occured while Connecting')
            client.close()

def write():
    while True:
        if stop_thread:
            break
        #Getting Messages
        message = f'{nickname}: {input("")}'
        if message[len(nickname)+2:].startswith('/'):
            if nickname == 'ixelizm':
                if message[len(nickname)+2:].startswith('/kick'):
                    # 2 for : and whitespace and 6 for /KICK_
                    client.send(f'KICK {message[len(nickname)+2+6:]}'.encode('utf-8'))
                elif message[len(nickname)+2:].startswith('/ban'):
                    # 2 for : and whitespace and 5 for /BAN
                    client.send(f'BAN {message[len(nickname)+2+5:]}'.encode('utf-8'))
            else:
                print("Commands can be executed by Admins only !!")
        else:
            client.send(message.encode('utf-8'))

recieve_thread = threading.Thread(target=recieve)
recieve_thread.start()
write_thread = threading.Thread(target=write)
write_thread.start()


Editor is loading...