diff --git a/2ano/1semestre/rc1/pratica06/Guide6_SocketsPython.pdf b/2ano/1semestre/rc1/pratica06/Guide6_SocketsPython.pdf new file mode 100644 index 0000000..48cc89c Binary files /dev/null and b/2ano/1semestre/rc1/pratica06/Guide6_SocketsPython.pdf differ diff --git a/2ano/1semestre/rc1/pratica06/SocketsPython_code/clientTCP.py b/2ano/1semestre/rc1/pratica06/SocketsPython_code/clientTCP.py new file mode 100644 index 0000000..e30aae0 --- /dev/null +++ b/2ano/1semestre/rc1/pratica06/SocketsPython_code/clientTCP.py @@ -0,0 +1,31 @@ +import socket +import signal +import sys + +def signal_handler(sig, frame): + print('\nDone!') + sys.exit(0) + +signal.signal(signal.SIGINT, signal_handler) +print('Press Ctrl+C to exit...') + +## + +ip_addr = "127.0.0.1" +tcp_port = 5005 + +sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + +sock.connect((ip_addr, tcp_port)) + +while True: + try: + message=input("Message to send? ").encode() + if len(message)>1: + sock.send(message) + response = sock.recv(4096).decode() + print('Server response: {}'.format(response)) + except (socket.timeout, socket.error): + print('Server error. Done!') + sys.exit(0) + diff --git a/2ano/1semestre/rc1/pratica06/SocketsPython_code/clientTCPv2.py b/2ano/1semestre/rc1/pratica06/SocketsPython_code/clientTCPv2.py new file mode 100644 index 0000000..4ab92bc --- /dev/null +++ b/2ano/1semestre/rc1/pratica06/SocketsPython_code/clientTCPv2.py @@ -0,0 +1,37 @@ +import socket +import signal +import sys +import datetime +import struct + +def signal_handler(sig, frame): + print('\nDone!') + sys.exit(0) + +signal.signal(signal.SIGINT, signal_handler) +print('Press Ctrl+C to exit...') + +## + +ip_addr = "127.0.0.1" +tcp_port = 5005 + +sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + +sock.connect((ip_addr, tcp_port)) + +order=0 +while True: + try: + message=input("Message to send [max 20 chars]? ") + version=1 + order+=1 + size=len(message) + if size<20: + message+='-'*(20-size) + pkt=struct.pack('!BLL20s',version,order,size,message[:20].encode()) + sock.send(pkt) + except (socket.timeout, socket.error): + print('Server error. Done!') + sys.exit(0) + diff --git a/2ano/1semestre/rc1/pratica06/SocketsPython_code/clientTCPv3.py b/2ano/1semestre/rc1/pratica06/SocketsPython_code/clientTCPv3.py new file mode 100644 index 0000000..bce210c --- /dev/null +++ b/2ano/1semestre/rc1/pratica06/SocketsPython_code/clientTCPv3.py @@ -0,0 +1,35 @@ +import socket +import signal +import sys +import datetime +import struct + +def signal_handler(sig, frame): + print('\nDone!') + sys.exit(0) + +signal.signal(signal.SIGINT, signal_handler) +print('Press Ctrl+C to exit...') + +## + +ip_addr = "127.0.0.1" +tcp_port = 5005 + +sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + +sock.connect((ip_addr, tcp_port)) + +order=0 +while True: + try: + message=input("Message to send? ").encode() + version=1 + order+=1 + size=len(message) + pkt=struct.pack('!BLL{}s'.format(size),version,size,order,message) + sock.send(pkt) + except (socket.timeout, socket.error): + print('Server error. Done!') + sys.exit(0) + diff --git a/2ano/1semestre/rc1/pratica06/SocketsPython_code/clientUDP.py b/2ano/1semestre/rc1/pratica06/SocketsPython_code/clientUDP.py new file mode 100644 index 0000000..d4c4adb --- /dev/null +++ b/2ano/1semestre/rc1/pratica06/SocketsPython_code/clientUDP.py @@ -0,0 +1,22 @@ +import socket +import signal +import sys + +def signal_handler(sig, frame): + print('\nDone!') + sys.exit(0) + +signal.signal(signal.SIGINT, signal_handler) +print('Press Ctrl+C to exit...') + +## + +ip_addr = "127.0.0.1" +udp_port = 5005 + +sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + +while True: + message=input("Message to send? ").encode() + if len(message)>0: + sock.sendto(message, (ip_addr, udp_port)) diff --git a/2ano/1semestre/rc1/pratica06/SocketsPython_code/serverTCP.py b/2ano/1semestre/rc1/pratica06/SocketsPython_code/serverTCP.py new file mode 100644 index 0000000..596feb1 --- /dev/null +++ b/2ano/1semestre/rc1/pratica06/SocketsPython_code/serverTCP.py @@ -0,0 +1,44 @@ +import socket +import threading +import signal +import sys + +def signal_handler(sig, frame): + print('\nDone!') + sys.exit(0) + +signal.signal(signal.SIGINT, signal_handler) +print('Press Ctrl+C to exit...') + +## + +def handle_client_connection(client_socket,address): + print('Accepted connection from {}:{}'.format(address[0], address[1])) + try: + while True: + request = client_socket.recv(1024) + if not request: + client_socket.close() + else: + msg=request.decode() + print('Received {}'.format(msg)) + msg=("ECHO: "+msg).encode() + client_socket.send(msg) + except (socket.timeout, socket.error): + print('Client {} error. Done!'.format(address)) + +ip_addr = "0.0.0.0" +tcp_port = 5005 + +server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +server.bind((ip_addr, tcp_port)) +server.listen(5) # max backlog of connections + +print('Listening on {}:{}'.format(ip_addr, tcp_port)) + +while True: + client_sock, address = server.accept() + client_handler = threading.Thread(target=handle_client_connection,args=(client_sock,address),daemon=True) + client_handler.start() + + diff --git a/2ano/1semestre/rc1/pratica06/SocketsPython_code/serverTCPsel.py b/2ano/1semestre/rc1/pratica06/SocketsPython_code/serverTCPsel.py new file mode 100644 index 0000000..949f62f --- /dev/null +++ b/2ano/1semestre/rc1/pratica06/SocketsPython_code/serverTCPsel.py @@ -0,0 +1,55 @@ +import socket +import selectors +import signal +import sys + +def signal_handler(sig, frame): + print('\nDone!') + sys.exit(0) + +signal.signal(signal.SIGINT, signal_handler) +print('Press Ctrl+C to exit...') + +## + +def handle_client_connection(server_socket): + client_socket, address = server_socket.accept() + print('Accepted connection from {}:{}'.format(address[0], address[1])) + client_socket.setblocking(False) + sel.register(client_socket, selectors.EVENT_READ, handle_data) + +def handle_data(client_socket): + address=client_socket.getpeername() + try: + request = client_socket.recv(1024) + if not request: + sel.unregister(client_socket) + client_socket.close() + else: + msg=request.decode() + print('Received {}'.format(msg)) + msg=("ECHO: "+msg).encode() + client_socket.send(msg) + except (socket.timeout,socket.error): + print('Client {} error. Done!'.format(address)) + +ip_addr = "0.0.0.0" +tcp_port = 5005 + +sel = selectors.DefaultSelector() +server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +server.bind((ip_addr, tcp_port)) +server.listen(5) # max backlog of connections + +server.setblocking(False) #Set to non-blocking +sel.register(server, selectors.EVENT_READ, handle_client_connection) + +print('Listening on {}:{}'.format(ip_addr, tcp_port)) + +while True: + events = sel.select() + for key, mask in events: + print(key) + callback = key.data + selsocket=key.fileobj + callback(selsocket) diff --git a/2ano/1semestre/rc1/pratica06/SocketsPython_code/serverTCPv2.py b/2ano/1semestre/rc1/pratica06/SocketsPython_code/serverTCPv2.py new file mode 100644 index 0000000..89f098c --- /dev/null +++ b/2ano/1semestre/rc1/pratica06/SocketsPython_code/serverTCPv2.py @@ -0,0 +1,44 @@ +import socket +import threading +import signal +import sys +import struct + +def signal_handler(sig, frame): + print('\nDone!') + sys.exit(0) + +signal.signal(signal.SIGINT, signal_handler) +print('Press Ctrl+C to exit...') + +## + +def handle_client_connection(client_socket,address): + print('Accepted connection from {}:{}'.format(address[0], address[1])) + try: + while True: + request = client_socket.recv(29) + if not request: + client_socket.close() + else: + pkt=struct.unpack('!BLL20s',request) + print(pkt) + print('Received ver: {}, order: {}, size: {} -> {}'.format(pkt[0],pkt[1],pkt[2],pkt[3].decode())) + except (socket.timeout, socket.error): + print('Client {} error. Done!'.format(address)) + +ip_addr = "0.0.0.0" +tcp_port = 5005 + +server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +server.bind((ip_addr, tcp_port)) +server.listen(5) # max backlog of connections + +print('Listening on {}:{}'.format(ip_addr, tcp_port)) + +while True: + client_sock, address = server.accept() + client_handler = threading.Thread(target=handle_client_connection,args=(client_sock,address),daemon=True) + client_handler.start() + + diff --git a/2ano/1semestre/rc1/pratica06/SocketsPython_code/serverUDP.py b/2ano/1semestre/rc1/pratica06/SocketsPython_code/serverUDP.py new file mode 100644 index 0000000..6bbf06f --- /dev/null +++ b/2ano/1semestre/rc1/pratica06/SocketsPython_code/serverUDP.py @@ -0,0 +1,22 @@ +import socket +import signal +import sys + +def signal_handler(sig, frame): + print('\nDone!') + sys.exit(0) + +signal.signal(signal.SIGINT, signal_handler) +print('Press Ctrl+C to exit...') + +## + +ip_addr = "0.0.0.0" +udp_port = 5005 + +sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) +sock.bind((ip_addr, udp_port)) + +while True: + message, sender_addr_port = sock.recvfrom(2048) + print('Message from {}:{} -> {}'.format(sender_addr_port[0],sender_addr_port[1],message.decode()))