View difference between Paste ID: 0eJnwEBE and DSCMmEjn
SHOW: | | - or go back to the newest paste.
1
import socket
2
import sys
3
from threading import Thread
4-
# Create a TCP/IP socket
4+
5-
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
5+
def recv_from_client(conn, addr):
6
    while True:
7-
# Bind the socket to the port
7+
        try:
8-
server_address = ('localhost', 10000)
8+
            #Pega o que o cliente enviou
9-
print('starting up on {} port {}'.format(*server_address))
9+
            data = conn.recv(1024)
10-
sock.bind(server_address)
10+
            #Mostra o que o cliente enviou
11
            print("'{}:{}' received from {}".format(*addr, data.decode("utf-8")))
12-
# Listen for incoming connections
12+
        finally:
13-
sock.listen(5)
13+
            conn.close()
14
            break
15-
while True:
15+
16-
    # Wait for a connection
16+
if __name__ == "__main__":
17-
    print('waiting for a connection')
17+
    # Create a TCP/IP socket
18-
    connection, client_address = sock.accept()
18+
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
19-
    try:
19+
20-
        print('connection from', client_address)
20+
    # Bind the socket to the port
21
    server_address = ('localhost', 10000)
22-
        # Receive the data in small chunks and retransmit it
22+
    print('starting up on {} port {}'.format(*server_address))
23-
        while True:
23+
    sock.bind(server_address)
24-
            data = connection.recv(8)
24+
25-
            if data.decode() == "Conectei":
25+
    # Listen for incoming connections
26-
                print("Vlw")
26+
    sock.listen(5)
27
    while True:
28-
    finally:
28+
        # Wait for a connection
29-
        # Clean up the connection
29+
        print('waiting for a connection')
30-
        connection.close()
30+
        connection, client_address = sock.accept()
31
32
        #Cria um thread para cada cliente connectado
33
        Thread(target=recv_from_client, args=(connection, client_address,)).start()
34
    sock.close()