Challenge: localhost Socket Coding

profileBreeze4811
socket_client_student.py

#!/usr/bin/python3 __author__ = 'Rick Hubbard' # Developed for DeAnza College CIS64E # localhost (127.0.0.1) Socket Client import socket size = 512 host = '' #Will default to localhost (127.0.0.1) port = 9242 #Arbitrary port selection #Define Socket Family as Internet and Type as TCP (i.e., Socket Stream) socket_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Define a Python tuple with coordinates to the server server_coordinates = (host, port) #[Attempt to] Connect with the server socket_client.connect(server_coordinates) #If connection with server was successful, then send a message as a BYTE string (not chr string) try: message = b' This is an example of sending a message via a socket from a client to a server\n' #Ideally, message will be sent from client to server via the provisioned socket and server will process message socket_client.sendall(message) except socket.errno as e: #If message send was unsuccessful, capture error message print("Socket error ", e) #Display error message finally: socket_client.closer() #When doen, always Close() sockets!