April 2020
Intermediate to advanced
294 pages
7h 53m
English
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 ...
Read now
Unlock full access