Chapter 3: Socket Programming

 

  1. Which method of the sockets module allows a domain name to be obtained from an IP address?

With the gethostbyaddr(address) method, we can obtain a domain name from an IP address.

  1. Which method of the socket module allows a server socket to accept requests from a client socket from another host?

socket.accept() is used to accept the connection from the client. This method returns two values: client_socket and client_address, where client_socket is a new socket object used to send and receive data over the connection.

 

  1. Which method of the socket module allows the sending of data to a given address?

socket.sendto(data, address) is used to send data to a given address.

 

  1. Which method of the socket module allows you to associate a host and a port with a specific socket?

The bind(IP,PORT) method allows you to associate a host and a port with a specific socket; for example,
>>> server.bind((“localhost”, 9999)).

 

  1. Which is the the difference between the TCP and UDP protocols and how do you implement them in Python with the socket module?

The main difference between TCP and UDP is that UDP is not connection-oriented. This means that there is no guarantee that our packets will reach their destinations, and there is no error notification if a delivery fails.

 

  1. Which method of the socket module allows you to convert a hostname to the IPv4 address format?

socket.gethostbyname(hostname)

 

  1. Which method of the socket module allows you to implement port-scanning with sockets and check the port state?

socket.connect_ex(address) is used for implementing port scanning with sockets.

  1. Which exception of the socket module allows you to catch exceptions related to the expiration of waiting times?

socket.timeout

 

  1. Which exception of the socket module allows you to catch errors during the search for information about IP addresses?

The socket.gaierror exception, which is thrown with the message “connection error to the server: [Errno 11001] getaddrinfo failed".

 

  1. Which exception of the socket module allows you to catch generic input and output errors and communications?

socket.error

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.142.2