Python socket server timeout

Python Socket Accept Timeout

Python Socket Accept Timeout

  1. Socket Accept, Reject, and Timeout
  2. Socket Methods and Their Uses in Python
  3. Example of Socket Accept Timeout in Python
  4. Set Default Timeout For Each Socket in Python
  5. Conclusion

The socket is the basic building block for network communication. A socket is opened whenever two network entities need to transfer the data.

These sockets stay connected during the session. But sometimes, while working with sockets in Python, you may be left waiting for long periods while the other end still accepts the socket connection.

This article discusses the timeout feature of sockets in Python that is necessary to mitigate the issue of waiting for socket accept for an infinite time.

Socket Accept, Reject, and Timeout

Whenever you try to establish a connection to a server using Python script, you are required to open a socket connection. This socket connection acts as a tunnel for transferring the data between the client and the server.

Socket Accept : When the socket is opened successfully, and the server and client are now connected to send and receive data, we term it socket accept . This scenario is the final goal of opening a socket.

Socket reject : Let us say you opened a socket but passed some different parameters, forgot to pass parameters or the process of opening a socket was not followed correctly; you would get a reject. It means the connection to send and receive data could not be established.

Читайте также:  Создать первую страницу html

Socket Accept timeout : This is an important yet overlooked scenario. Sometimes when you try to open a socket, you may not get any response from the other end.

Due to this, you are left waiting forever. This is an unwanted situation as you would like to close the current request and try another request rather than wait forever.

Socket Methods and Their Uses in Python

accept() : As the name suggests, the accept() method accepts an incoming socket request from another party. This method returns a socket connection that you can use to transfer the data between the connected entities.

bind() : This method binds or attaches a socket connection to an address. This method is a must to be called method if you want to work with sockets.

The bind() method accepts a tuple of an IP address and a port to which it binds the socket.

listen() : This is a server-side method that enables the server to accept a socket to transfer data between the connections.

connect() : This method accepts an address as an argument. It then connects the remote socket at the address.

settimeout() : This method accepts a non-zero number as the number of seconds to wait before it raises a TimeoutError . This method is important to mitigate the problem of infinite wait times.

Example of Socket Accept Timeout in Python

Let us look at an example of socket accept timeout with the help of code in Python. For this example, we would need to write two Python scripts, one for the server and the other for the client.

The first scenario is when there is no timeout limit set. In this case, the client would keep on waiting.

Note that, depending on your operating system, the request may be automatically turned down after some time.

The second scenario is where we set the timeout limit using the settimeout() method. In this case, you will get the TimeoutError after the set limit.

Let us see the codes of the three cases above one by one.

Socket Accept Timeout in Python When No Timeout Limit Is Set

import socket  with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:  s.bind(('', 1717)) 
import socket  with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:  s.connect(('127.0.0.1', 1717)) 

You can see no listen() method on the server side. Therefore, the server is not responding, and the client is waiting.

Socket Accept Timeout in Python When Timeout Is Set

In this case, you will see that the client aborts the operation and raises a TimeoutError after the timeout time is passed.

import socket  with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:  s.bind(('', 1717)) 
import socket  with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:  s.settimeout(3)  s.connect(('127.0.0.1', 1717)) 

In this case, a Python socket TimeoutError is raised. Please note that sockets can behave differently depending on the platform.

On the Linux machine, the socket throws the ConnectionRefusedError: [Errno 111] Connection refused error until you do not accept the socket on the server side.

Set Default Timeout For Each Socket in Python

This method would set the same default timeout for all your new sockets. In this way, you can save time and hassle by setting the timeout for each socket separately.

import socket  with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:  s.bind(('', 1717)) 
import socket  with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:  socket.setdefaulttimeout(3)  s.connect(('127.0.0.1', 1717)) 

Note that for setdefaulttimeout() , you must use the socket class directly instead of the object because it sets the timeout for all threads.

Conclusion

The socket timeout is an important aspect of socket programming in Python. If you do not handle the timeout, you may leave your client waiting for the socket forever.

Or in the other case, depending on your environment implementation, it may throw an error.

Related Article — Python Socket

Copyright © 2023. All right reserved

Источник

Оцените статью