Chatroom Server em Python

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

[ Hits: 2.658 ]

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

Validação de IP

Ligando máquina pela rede usando Python

dhcps - DHCP Listener

IPs Classfull

B.S.A.N.S. - Break Security - Automated Network Scanner [PARTE 1]


  

Comentários
[1] Comentário enviado por maurixnovatrento em 15/07/2022 - 21:08h


Bacana;
___________________________________________________________
Conhecimento não se Leva para o Túmulo.
https://github.com/mxnt10


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts