-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_server.py
More file actions
35 lines (28 loc) · 877 Bytes
/
chat_server.py
File metadata and controls
35 lines (28 loc) · 877 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import socket
import argparse
import sys
def argumentos():
parser = argparse.ArgumentParser()
parser.add_argument('-lhost', type=str, help="Indique la IP a la escucha")
parser.add_argument('-lport', type=str, help="Indique el puerto a la escucha")
args = parser.parse_args()
sys.stdout.write(str(server(args)))
def server(args):
host = args.lhost
port = int(args.lport)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
server.listen()
while True:
conn, ip = server.accept()
print(f"Conexión recibida de la IP {ip[0]} y el puerto {ip[1]}")
conn.send(b"Este es un servidor hecho con Python\n")
while True:
# Mensaje que el cliente nos envia
client_msg = conn.recv(1024).decode()
print(client_msg)
# Mensaje que vamos a recibir
mensaje = input().encode()
conn.send(mensaje)
conn.close
argumentos()