Python-based asyncore module usage example tutorial, pythonasyncore

Source: Internet
Author: User

Python-based asyncore module usage example tutorial, pythonasyncore

This article analyzes the principles and usage of the asyncore module in Python by examples and shares it with you for your reference. The specific analysis is as follows:

The asyncore library is a standard library of python, which is an asynchronous socket package. We can directly use underlying libraries such as sockets when operating the network, but asyncore makes it easier for us to operate the network and avoid directly using socket, select, poll and other tools are complex.

This library is simple and contains a function and a class.
* Loop () function
* Dispatcher base class
It should be noted that the loop function is global, not the dispatcher method.

Every class Object inherited from dispatcher can be considered as a socket to be processed, such as a TCP connection or UDP, or even an uncommon one. Easy to use. We need to define a class that inherits the dispatcher, and then we can rewrite (overwrite) Some methods.

The methods we need to override are generally headers with handle.

class refuse(dispatcher):  def handle_accept():    #do nothing ...    pass

The loop () function is used to detect an instance in dict that stores dispatcher. This dictionary is called channel. Every time you create a dispatcher object, you will add yourself to a default dict (you can also specify the channel yourself ). When an object is added to a channel, the socket behavior has been defined, and the program only needs to call loop () to implement all functions.

Asyncore is a good design in the python standard library
There is an asyncore example in the python standard document.

import asyncore, socketclass http_client(asyncore.dispatcher):  def __init__(self, host, path):    asyncore.dispatcher.__init__(self)    self.create_socket(socket.AF_INET, socket.SOCK_STREAM)    self.connect( (host, 80) )    self.buffer = 'GET %s HTTP/1.0\r\n\r\n' % path  def handle_connect(self):    pass  def handle_close(self):    self.close()  def handle_read(self):    print self.recv(8192)  def writable(self):    return (len(self.buffer) > 0)  def handle_write(self):    sent = self.send(self.buffer)    self.buffer = self.buffer[sent:]c = http_client('www.python.org', '/')asyncore.loop()

Run this function and find that the home page of python.org has been downloaded. That is to say, we have implemented an http layer protocol? But we only use socket-level Apis... Let's take a look at the mysteries of these lines of code!

Writable and readable are called when a socket can be written or data is detected, and a bool is returned to determine whether handle_read or handle_write

Open asyncore. py and you can see that the writable and readable methods defined in the dispatcher class are quite simple:

def readable(self):  return Truedef writable(self):  return True

That is to say, once a readable or writable function is detected, handle_read/handle_write is called directly. However, in the preceding example, we see an overload (which looks like a C ++ virtual function, isn't it ?)

def writable(self):  return (len(self.buffer) > 0)

Obviously, when we have data to be sent, we will return a True value to the writable caller, so that we do not need to make any further judgment in handle_write. The logic is clear and the code is clear, it takes some time to understand the problem, but it is not difficult!

The rest of the code looks very clear, and there is a sense of force. When an http server sends and processes your request and closes the socket, our handle_close () also completes our mission accordingly. Close () deletes the object from the channel and destroys the socket object.

def close(self):  self.del_channel()  self.socket.close()

The loop () function detects an empty channel and exits the loop. The program completes the task and exits.

I hope this article will help you with Python programming.


What if people always cough?

Take medicine! Or go to the hospital to check the lung
 
For beginners of python, what modules are required for learning python crawlers?

Urllib, urllib2, urlparse, BeautifulSoup, mechanic, cookielib, etc.
It is not difficult to master these databases. What is difficult for Web Crawlers is that you need to design your own pressure control algorithms, your resolution algorithms, and graph Traversal Algorithms.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.