Created
October 20, 2021 20:38
-
-
Save HectorSaldes/4915e6aa2101aa2e9e4c186504a3be53 to your computer and use it in GitHub Desktop.
[Sockets chat] - Archivo cliente para la conexión al servidor
This file contains hidden or 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
""" | |
? Saldaña Espinoza Hector | |
* Seguridad informática | |
! 7B | |
""" | |
import socket | |
import Cryptography | |
import sys | |
def main(): | |
# * Tomamtos los datos del usuario, en caso de dar enter tomará datos default | |
crypto = Cryptography.Cryptography() | |
client_socket = socket.socket() | |
addr = input('IP (localhost): ') | |
addr = addr if addr else 'localhost' | |
port = input('PORT (5000): ') | |
port = port if port else 5000 | |
username = input('USUARO (Usuario): ') | |
username = username if username else 'Usuario' | |
# * Se realiza y se mantiene activa la conexión | |
try: | |
client_socket.connect((str(addr), int(port))) | |
print('-'*20, '¡Conexión establecida!') | |
# * Se mantiene activo el chat | |
while True: | |
print('Escribe un mensaje o (q/Q) para finalizar todo') | |
smsDecrypt = input(f'{username}: ') | |
if(smsDecrypt == 'q' or smsDecrypt == 'Q'): | |
client_socket.sendall(smsDecrypt.encode('UTF-8')) | |
print('Conexión finalizada') | |
break | |
else: | |
smsEncrypt = crypto.encrypt(smsDecrypt) | |
sms = f'{smsDecrypt} > {smsEncrypt}'; | |
print(f'{username}: {smsEncrypt}') | |
client_socket.sendall(sms.encode('UTF-8')) | |
response = client_socket.recv(1024).decode('UTF-8') | |
print(f'Servidor: {response}') | |
print('-'*20) | |
client_socket.close() | |
except: | |
print('\nOcurrió un error :c') | |
sys.exit() | |
# * Manda a ejecutar la función principal | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment