changed host.py to be a super basic server that just responds with an echo
This commit is contained in:
parent
5b4892ab69
commit
ac7880e1c2
@ -1,36 +1,25 @@
|
||||
import psutil
|
||||
import socket
|
||||
|
||||
LOCAL_IP = '127.0.0.1'
|
||||
SERVER_PORT = 29987
|
||||
SERVER_BUFFER = 1024
|
||||
|
||||
class InterfaceInfo:
|
||||
"""
|
||||
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
|
||||
udp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
udp_server_socket.bind((LOCAL_IP, SERVER_PORT))
|
||||
|
||||
def __str__(self):
|
||||
return '(Interface:{0}, Address:{1}, Subnet Mask:{2})'.format(self.interface_name,
|
||||
self.address, self.subnet_mask)
|
||||
waiting_for_connection = True
|
||||
|
||||
def __repr__(self):
|
||||
return '(Interface:{0}, Address:{1}, Subnet Mask:{2})'.format(self.interface_name,
|
||||
self.address, self.subnet_mask)
|
||||
while waiting_for_connection:
|
||||
received = udp_server_socket.recvfrom(SERVER_BUFFER)
|
||||
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():
|
||||
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()
|
||||
udp_server_socket.close()
|
||||
|
Loading…
Reference in New Issue
Block a user