#!/usr/bin/env python3 from socket import * from _thread import start_new_thread from struct import pack, unpack # Create a socket. This doesn't open a port or anything yet # Parameters: # AF_INET is ipv4, AF_INET6 # SOCK_STREAM means TCP, SOCK_DGRAM would be UDP our_socket = socket(AF_INET, SOCK_STREAM) our_socket.connect(("isoptera.lcsc.edu", 5080)) ADDMON = 1 DELMON = 2 def listen_to_server(): while True: uname_len = unpack(">B", our_socket.recv(1))[0] uname = our_socket.recv(uname_len).decode("utf-8") result_len = unpack(">H", our_socket.recv(2))[0] result = our_socket.recv(result_len).decode("utf-8") print("Received Report! " + uname + ": " + result) start_new_thread(listen_to_server, ()) while True: choice = input("Add or delete a monitor? ") action = 0 if "add" in choice.lower(): action = ADDMON elif "del" in choice.lower(): action = DELMON else: print("Couldn't understand choice") continue uname = input("Enter the username: ") action_len = pack("!BB", action, len(uname)) our_socket.send(action_len) our_socket.send(uname.encode("utf-8")) our_socket.close()