What is dispatcher in asyncore?
Overview
The asyncore module in Python provides you the tools to create a network of clients and servers. asyncore communiccates asynchronously through sockets, which helps avoid the use of threads and keeps the implementation simple. The asyncore module can be imported into your program by including the following code:
import asnycore
dispatcher() is a class of the asyncore module and is a wrapper around the low-level socket that provides functions for performing actions such as forming a connection, writing to a connection, reading from a connection, and closing a connection. We need dispatcher() to be able to use the asynchronous socket provided by asyncore. Below are the functions which can be used with the dispatcher() class and their general descriptions. These functions make use of their socket-partner functions like close(), connect(), send(), and recv() to achieve their purpose in the code.
Functions | Description |
handle_connect() | the socket makes a successful connection |
handle_accept() | called on a channel that's listening for connections |
handle_expt() | called when a connection fails |
handle_read() | data is to be read from the channel; use the 'recv' method to acquire the data |
handle_write() | data is to be written to the channel; use the 'send' method to send the data |
handle_close() | called when a connection closes |
You can read about the rest of the partner functions in the Python
. documentation https://docs.python.org/3/
Example
Please see the example below to understand the dispatcher() class in more detail. As you can see, the functions above are defined within our code to help us achieve their purpose.
import asyncoreclass Client(asyncore.dispatcher):def __init__(self, host, path):asyncore.dispatcher.__init__(self)self.create_socket()self.connect((host, 80))self.buffer = bytes('GET %s HTTPS/1.0\r\nHost: %s\r\n\r\n' %(path, host), 'ascii')def handle_close(self):self.close()def handle_read(self):print(self.recv(8192))print("recieved")def handle_write(self):sent = self.send(self.buffer)self.buffer = self.buffer[sent:]client = Client('www.google.com', '/')asyncore.loop()
Explanation
The code above is a simple Client class that queries a server by sending it a request. First, the class initializes the dispatcher() and then creates and connects to a socket. The ability to create a socket using the create_socket() function comes from the dispatcher() class. Once the socket is created, it connects to a host and port. host is the website you want to ping, and port is the port number of HTTPS to establish a connection. As the port number for HTTPS is 80, that is reflected in the code. We assemble the bytes to be sent on the connection. The handle_write() function is used to write to the connection using send(), and handle_read() to read from the connection using recv() method. The handle_close() close method closes the connections. All of these functions are running in an asyncore.loop(), which can be stopped by tweaking its parameters.
Free Resources