Last active
December 1, 2023 17:31
-
-
Save dclobato/a2971853bed7105ee665d774efb9072b to your computer and use it in GitHub Desktop.
Snippet: servidor em python para cifrar com cifra de cesar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
def codifica(texto: str, passo: int = 3) -> str: | |
retorno = "" | |
for letra in texto: | |
if letra.isupper(): | |
retorno += chr(((ord(letra) - 65 + passo) % 26) + 65) | |
elif letra.islower(): | |
retorno += chr(((ord(letra) - 97 + passo) % 26) + 97) | |
else: | |
retorno += letra | |
return retorno | |
ENDERECO = "0.0.0.0" # Liga a todos os endereços. Se quiser só local, | |
# pode colocar 127.0.0.1, ou o IP da interface | |
PORTA = 0 # Escolhe uma porta livre acima de 1023 | |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | |
s.bind((ENDERECO, PORTA)) | |
_, PORTA = s.getsockname() | |
print(f"Aguardando conexao em {ENDERECO}:{PORTA}") | |
s.listen() | |
conn, addr = s.accept() | |
with conn: | |
print(f"Conexao vindo de {addr[0]}:{addr[1]}") | |
while True: | |
data = conn.recv(1024).decode() | |
if not data: | |
break | |
conn.sendall(codifica(data).encode()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment