From 8109c2fe308b7a9c1f5380a15b4e3222142f9d5e Mon Sep 17 00:00:00 2001 From: Manuel Vergara Date: Thu, 11 Jan 2024 23:08:41 +0100 Subject: [PATCH] =?UTF-8?q?Update=20Python=20Ofensivo=20-=20M=C3=A1s=20eje?= =?UTF-8?q?rcicios=20con=20conexiones=20socket?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../04_socket/client.py | 27 +++++++++ .../04_socket/server.py | 58 +++++++++++++++++++ .../05_socket/client.py | 28 +++++++++ .../05_socket/server.py | 37 ++++++++++++ 4 files changed, 150 insertions(+) create mode 100644 python-ofensivo/07_conexiones_red_protocolos/04_socket/client.py create mode 100644 python-ofensivo/07_conexiones_red_protocolos/04_socket/server.py create mode 100644 python-ofensivo/07_conexiones_red_protocolos/05_socket/client.py create mode 100644 python-ofensivo/07_conexiones_red_protocolos/05_socket/server.py diff --git a/python-ofensivo/07_conexiones_red_protocolos/04_socket/client.py b/python-ofensivo/07_conexiones_red_protocolos/04_socket/client.py new file mode 100644 index 0000000..ee71ef5 --- /dev/null +++ b/python-ofensivo/07_conexiones_red_protocolos/04_socket/client.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +import socket + + +def start_client(): + + host = 'localhost' + port = 1234 + + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.connect((host, port)) + + while True: + msg = input("[+] Introduce un mensaje: ") + + s.sendall(msg.encode('utf-8')) + + if msg.strip() == 'bye': + break + + data = s.recv(1024) + + print(f"\n[+] Mensaje del servidor: {data.decode().strip()}") + + +start_client() diff --git a/python-ofensivo/07_conexiones_red_protocolos/04_socket/server.py b/python-ofensivo/07_conexiones_red_protocolos/04_socket/server.py new file mode 100644 index 0000000..02ce5f8 --- /dev/null +++ b/python-ofensivo/07_conexiones_red_protocolos/04_socket/server.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 + +import socket +import threading +import pdb + + +class ClientThread(threading.Thread): + + def __init__(self, conn, addr): + super().__init__() + self.conn = conn + self.addr = addr + + print(f"\nConexión establecida desde {self.addr}") + + def run(self): + msg = '' + + while True: + + data = self.conn.recv(1024) + msg = data.decode() + + # pdb.set_trace() # Breakpoint + + if msg.strip() == 'bye': + break + + print(f"\n[+] Recibido del cliente {self.addr}: {msg.strip()}") + self.conn.send(data) + + print(f"\n[-] Cliente {self.addr} desconectado") + self.conn.close() + + +HOST = 'localhost' +PORT = 1234 + +with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + + # Alterar propiedades del socket + # Reutilizar direcciones + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + # Otras propiedades: https://docs.python.org/3/library/socket.html#socket.socket.setsockopt + # socket.IPPROTO_IP, socket.IPPROTO_IPV6, socket.IPPROTO_TCP, socket.IPPROTO_UDP... + + # Enlazar socket a dirección y puerto + s.bind((HOST, PORT)) + + print(f"Servidor TCP escuchando en {HOST}:{PORT}") + + while True: + + s.listen() + conn, addr = s.accept() + new_thread = ClientThread(conn, addr) + new_thread.start() diff --git a/python-ofensivo/07_conexiones_red_protocolos/05_socket/client.py b/python-ofensivo/07_conexiones_red_protocolos/05_socket/client.py new file mode 100644 index 0000000..91c893a --- /dev/null +++ b/python-ofensivo/07_conexiones_red_protocolos/05_socket/client.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +import socket + + +def start_chat_client(): + + host = 'localhost' + port = 1234 + + client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + client_socket.connect((host, port)) + + while True: + + client_msg = input(f"\n[+] Mensaje para enviar al servidor: ") + client_socket.send(client_msg.encode()) + + if client_msg == "bye": + break + + server_msg = client_socket.recv(1024).strip().decode() + print(f"\n[+] Mensaje del servidor: {server_msg}") + + client_socket.close() + + +start_chat_client() diff --git a/python-ofensivo/07_conexiones_red_protocolos/05_socket/server.py b/python-ofensivo/07_conexiones_red_protocolos/05_socket/server.py new file mode 100644 index 0000000..a20b89c --- /dev/null +++ b/python-ofensivo/07_conexiones_red_protocolos/05_socket/server.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + +import socket + + +def start_chat_server(): + host = 'localhost' + port = 1234 + + server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + # reuse socket TIME_WAIT + server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + server_socket.bind((host, port)) + server_socket.listen(1) + + print(f"\n[+] Servidor listo para aceptar una conexión en {host}:{port}") + + conn, addr = server_socket.accept() + + print(f"\n[+] Conexión establecida con {addr[0]}:{addr[1]}") + + while True: + + client_msg = conn.recv(1024).strip().decode() + print(f"\n[+] Mensaje del cliente: {client_msg}") + + if client_msg == "bye": + break + + server_msg = input(f"\n[+] Mensaje para el cliente: ") + conn.send(server_msg.encode()) + + print(f"\n[+] Conexión finalizada con {addr[0]}:{addr[1]}") + conn.close() + + +start_chat_server()