We are going to create a function that allows us to connect to a socket server. The function definition is as follows:
def socket_connect(address, port):
s.settimeout(1.0)
addr_info = socket.getaddrinfo(address, port)
addr = addr_info[0][-1]
try:
print("Attempting to connect to socket server ...")
s.connect(addr)
print("Connection successful!")
except Exception as e:
print(e)
As you can see, we pass in an IP address and port number for where the socket server is located. We then use socket.getaddrinfo to create the addr_info object that can be passed into the socket connect method. We wrap the connection attempt in try/except just in case we run into a connection issue, such as the server not existing.
There are two primary tasks that we need to develop: a receive task and a system status task. Let's develop those now, along with any supporting application classes and functions.