changed host.py to be a super basic server that just responds with an echo

This commit is contained in:
Nicholas Dyer 2022-12-21 13:20:13 -05:00
parent 5b4892ab69
commit ac7880e1c2

View File

@ -1,36 +1,25 @@
import psutil import socket
LOCAL_IP = '127.0.0.1'
SERVER_PORT = 29987
SERVER_BUFFER = 1024
class InterfaceInfo: udp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
""" udp_server_socket.bind((LOCAL_IP, SERVER_PORT))
Class used for storing interface information to make it easier to use
"""
def __init__(self, interface_name, address, subnet_mask):
self.interface_name = interface_name
self.address = address
self.subnet_mask = subnet_mask
def __str__(self): waiting_for_connection = True
return '(Interface:{0}, Address:{1}, Subnet Mask:{2})'.format(self.interface_name,
self.address, self.subnet_mask)
def __repr__(self): while waiting_for_connection:
return '(Interface:{0}, Address:{1}, Subnet Mask:{2})'.format(self.interface_name, received = udp_server_socket.recvfrom(SERVER_BUFFER)
self.address, self.subnet_mask) message = received[0]
address = received[1]
decoded_message = message.decode('UTF-8')
if decoded_message.split(';')[0] != 'PYPONGREQ':
message = 'PYPONGRST;ERROR:INVALID HEADER'
udp_server_socket.sendto(message.encode('UTF-8'), address)
else:
message = 'PYPONGRST;ECHO:{0}'.format(decoded_message)
udp_server_socket.sendto(message.encode('UTF-8'), address)
def main(): udp_server_socket.close()
raw_interface_data = psutil.net_if_addrs()
interface_data = []
# Extract needed information from psutil and put it into a InterfaceInfo object
for interface in raw_interface_data:
address = (raw_interface_data.get(interface)[0]).address
subnet_mask = (raw_interface_data.get(interface)[0]).netmask
interface_data.append(InterfaceInfo(interface, address, subnet_mask))
print(interface_data)
if __name__ == '__main__':
main()