In Python, try is the keyword that marks a block of code for handling potenital exceptions. The try block is followed by one or more except blocks (i.e., exception handlers) and an optional finally block. The exception handling mechanism in Python is provided through the keywords try, except, finally and raise.
# import the socket module
# Create a socket object
socketObject = socket.socket()
# Establish connection to a http server(a non existing one)
addressToConnect = "http://www.non-existing-example.com"
print("Connected to specified host")
# Request for a http page
HTTPMessage = "GET / HTTP/1.1\r\nHost: localhost\r\n Connection: close\r\n\r\n"
bytes = str.encode(HTTPMessage)
# Receive the data
data = socketObject.recv(1024)
print("Closing the connection")
except IOError as Ex:
print("An IO Error occurred.Details:")
An IO Error occurred.Details:
[Errno 8] nodename nor servname provided, or not known