-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from SalesianosZaragoza/dev
Dev Estable V1.0
- Loading branch information
Showing
2 changed files
with
90 additions
and
20 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,37 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import socket | ||
import time | ||
|
||
HOST = "127.0.0.1" # The server's hostname or IP address | ||
PORT = 65432 # The port used by the server | ||
HOST = "127.0.0.1" | ||
PORT = 65432 | ||
TIEMPO_ESPERA = 60 # segundos | ||
|
||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | ||
s.connect((HOST, PORT)) | ||
s.sendall(b"Hello, world") | ||
data = s.recv(1024) | ||
|
||
print(f"Received {data!r}") | ||
|
||
# Establecer un tiempo de espera para el cliente | ||
s.settimeout(TIEMPO_ESPERA) | ||
|
||
while True: | ||
try: | ||
# Solicitar al usuario que ingrese un mensaje | ||
message = input("Ingrese un mensaje (o presione Enter para salir): ") | ||
|
||
# Salir del bucle si no se ingresa ningún mensaje | ||
if not message: | ||
break | ||
|
||
# Convertir el mensaje a bytes y enviarlo al servidor | ||
s.sendall(message.encode()) | ||
|
||
# Recibir la respuesta del servidor | ||
data = s.recv(1024) | ||
|
||
# Decodificar y mostrar la respuesta recibida del servidor | ||
response_from_server = data.decode() | ||
print(f"Mensaje recibido del servidor: {response_from_server}") | ||
|
||
except socket.timeout: | ||
print("Tiempo de espera alcanzado. Cerrando conexión.") | ||
break |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,64 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import socket | ||
import time | ||
|
||
HOST = "127.0.0.1" # Standard loopback interface address (localhost) | ||
PORT = 65432 # Port to listen on (non-privileged ports are > 1023) | ||
|
||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | ||
s.bind((HOST, PORT)) | ||
s.listen() | ||
conn, addr = s.accept() | ||
with conn: | ||
print(f"Connected by {addr}") | ||
while True: | ||
data = conn.recv(1024) | ||
if not data: | ||
break | ||
conn.sendall(data) | ||
HOST = "127.0.0.1" | ||
PORT = 65432 | ||
TIEMPO_ESPERA = 60 # segundos | ||
|
||
# Inicializar la variable para almacenar el mensaje del cliente | ||
input_client = "" | ||
|
||
#Lista de strings con las opciones que se pueden usar como comandos | ||
command_list = ["LIST", "CREATE", "CONNECT", "JOIN", "MSG"] | ||
|
||
def establecerConexion(): | ||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | ||
s.bind((HOST, PORT)) | ||
s.listen() | ||
conn, addr = s.accept() | ||
with conn: | ||
print(f"Conectado por {addr}") | ||
|
||
# Establecer un tiempo de espera para el servidor | ||
conn.settimeout(TIEMPO_ESPERA) | ||
|
||
while True: | ||
try: | ||
data = conn.recv(1024) | ||
if not data: | ||
print("Cliente desconectado") | ||
break | ||
|
||
# Decodificar y guardar el mensaje del cliente | ||
input_client = data.decode() | ||
|
||
# Modificar el mensaje a enviar de vuelta al cliente | ||
response_to_client = f"Mensaje desde el servidor: {input_client}" | ||
|
||
# Enviar de vuelta el mensaje modificado al cliente | ||
conn.sendall(response_to_client.encode()) | ||
admitirComandos(input_client) | ||
|
||
except socket.timeout: | ||
print("Tiempo de espera alcanzado. Cerrando conexión.") | ||
break | ||
|
||
"""Metodo admitir comandos, | ||
Ve que el comando empiece por / o // o + y luego compruebe que la cadena de texto sea en mayusculas. | ||
O que sea en minusculas o mayusculas el metodo establezca todo a mayusculas y sean palabras reservadas | ||
Comprobar si la variable comienza con "/" y el resto está en mayúsculas y en la lista""" | ||
def admitirComandos(input_client): | ||
if input_client.startswith("/"): | ||
print("El input empieza por /") | ||
if input_client[1:].isupper() and input_client[1:] in command_list: | ||
print("El input es un comando valido") | ||
#Falta llamar a la funcion correspondiente, por ahora no está implementado porque no sabemos como se van a llamar las funciones | ||
else: | ||
print("El comando no existe o no está escrito correctamente. Recuerda que los comandos son en mayusculas y empiezan por /") | ||
else: | ||
print("El input no incluye un comando (no comienza por /)") | ||
|
||
#Llamada al metodo establecerConexion | ||
establecerConexion() |