Chatroom Server em Python
Publicado por henrique (última atualização em 21/05/2022)
[ Hits: 2.841 ]
Homepage: naotem.com
Server do chat room em python
#!/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()
Shell reverso em Python - cliente
Peer-to-peer nós para processamento em multipontos
ArpRequest - requisição arp para endereço físico
Servidor de Backup com Ubuntu Server 24.04 LTS, RAID e Duplicati (Dell PowerEdge T420)
Visualizar câmeras IP ONVIF no Linux sem necessidade de instalar aplicativos
Atualizar Debian Online de uma Versão para outra
Instalar driver Nvidia no Debian 13
Redimensionando, espelhando, convertendo e rotacionando imagens com script
Debian 13 Trixie para Iniciantes
Convertendo pacotes DEB que usam ZSTD (Padrão Novo) para XZ (Padrão Antigo)