-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp_server.py
More file actions
executable file
·33 lines (28 loc) · 963 Bytes
/
tcp_server.py
File metadata and controls
executable file
·33 lines (28 loc) · 963 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
import sys, socket
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = ('localhost', 8081)
print('[+] Iniciando en {}:{}'.format(*server_address))
sock.bind(server_address)
# Listen for incoming connections
sock.listen(1)
while True:
# Wait for a connection
print('[*] Esperando conexión')
connection, client_address = sock.accept()
try:
print('[!] Conexión desde ', client_address)
# Receive the data in small chunks and retransmit it
while True:
data = connection.recv(16)
print('Recibido {!r}'.format(data))
if data:
print('[!] Reenviando datos al cliente...')
connection.sendall(data)
else:
print('[X] NO se recibieron datos', client_address)
break
finally:
# Clean up the connection
connection.close()