Chatroom Server em Python

Publicado por henrique (última atualização em 21/05/2022)

[ Hits: 518 ]

Homepage: naotem.com

Download chats.py




Server do chat room em python

  



Esconder código-fonte

#!/bin/python3
import threading
import socket
from sys import argv

red  = "\033[1;31m"
blue  = "\033[1;34m"
cyan  = "\033[1;36m"
green = "\033[0;32m"
reset = "\033[0;0m"
bold  = "\033[;1m"
reverse = "\033[;7m"
white = "\033[37m"
gray = "\033[0;37m"
def usage():
    print('-' * 90 + green + '''

    █████████  █████   █████   █████████   ███████████    ███████████  █████ █████
  ███░░░░░███░░███   ░░███   ███░░░░░███ ░█░░░███░░░█   ░░███░░░░░███░░███ ░░███ 
 ███     ░░░  ░███    ░███  ░███    ░███ ░   ░███  ░     ░███    ░███ ░░███ ███  
░███          ░███████████  ░███████████     ░███        ░██████████   ░░█████   
░███          ░███░░░░░███  ░███░░░░░███     ░███        ░███░░░░░░     ░░███    
░░███     ███ ░███    ░███  ░███    ░███     ░███        ░███            ░███    
 ░░█████████  █████   █████ █████   █████    █████    ██ █████           █████   
  ░░░░░░░░░  ░░░░░   ░░░░░ ░░░░░   ░░░░░    ░░░░░    ░░ ░░░░░           ░░░░░    


exemplo:
          ./chats.py <ip> <port>           
'''.title() + reset + '-' * 90)
try:
    host = argv[1]
    port = int(argv[2])
except:
    usage()
    exit()
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
server.listen()

clients = []
nicknames = []

def broadcast(message):
    for client in clients:
        client.send(message)

def handle(client):
    while True:
        try:
            message = client.recv(1024)
            broadcast(message)
        except:
            index = clients.index(client)
            clients.pop(index)
            client.close()
            nickname = nicknames[index]
            broadcast(f'{nickname} Saiu do chat'.encode('ascii'))
            nicknames.pop(index)
            print(reset)
            break

def receive():
     while True:
         client, address = server.accept()
         print(red + f"Conectado pelo: {str(address)}")

         client.send('Usuario:'.encode('ascii'))
         nickname = client.recv(1024).decode('ascii')
         nicknames.append(nickname)
         clients.append(client)

         print(f'novo usuario conectado: {nickname}'.capitalize())
         broadcast(f'{nickname} Entrou no chat'.encode('ascii'))
         client.send('\nConectou no server'.encode('ascii'))

         thread = threading.Thread(target=handle, args=(client,))
         thread.start()

print("Em busca...")
receive()

Scripts recomendados

Scanner de rede com threading

IPs Classfull

Shell reverso em Python - cliente

Scanner de rede em Python

Importar múltiplos hosts ZABBIX


  

Comentários

Nenhum comentário foi encontrado.


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts