Managing socket exceptions

In order to handle exceptions, we'll use the try and except blocks. Different types of exceptions are defined in Python's socket library for different errors. These exceptions are described here:

  • exception socket.timeout: This block catches exceptions related to the expiration of waiting times.
  • exception socket.gaierror: This block catches errors during the search for information about IP addresses, for example when we are using the getaddrinfo() and getnameinfo() methods.
  • exception socket.error: This block catches generic input and output errors and communication. This is a generic block where you can catch any type of exception.

The next example shows you how to handle the exceptions.

You can find the following ...

Get Mastering Python for Networking and Security now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.