python asyncore

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python asyncore相关的知识,希望对你有一定的参考价值。

原文地址:http://effbot.org/librarybook/asyncore.htm

The asyncore module

This module provides a “reactive” socket implementation. Instead of creating socket objects, and calling methods on them to do things, this module lets you write code that is called when something can be done. To implement an asynchronous socket handler, subclass the dispatcher class, and override one or more of the following methods:

  • writable is called by the asyncore framework to check if the dispatcher has data to send. The default implementation always returns True.

  • readable is called to check if the dispatcher is ready to process incoming data, if any. The default implementation always returns True.

  • handle_connect is called when a connection is successfully established.

  • handle_expt is called when a connection fails (Windows), or when out-of-band data arrives (Unix).

  • handle_accept is called when a connection request is made to a listening socket. The callback should call the accept method to get the client socket. In most cases, the callback should create another socket handler to handle the actual communication.

  • handle_read is called when there is data waiting to be read from the socket. The callback should call the recv method to get the data.

  • handle_write is called when data can be written to the socket. Use thesend method to write data.

  • handle_close is called when the socket is closed or reset.

  • handle_error(type, value, traceback) is called if a Python error occurs in any of the other callbacks. The default implementation prints an abbreviated traceback to sys.stdout.

The first example shows a time client, similar to the one for the socket module:

 
Example: Using the asyncore module to get the time from a time server
# File: asyncore-example-1.py

import asyncore
import socket, time

# reference time (in seconds since 1900-01-01 00:00:00)
TIME1970 = 2208988800L # 1970-01-01 00:00:00

class TimeRequest(asyncore.dispatcher):
    # time requestor (as defined in RFC 868)

    def __init__(self, host, port=37):
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect((host, port))

    def writable(self):
        return 0 # don‘t have anything to write

    def handle_connect(self):
        pass # connection succeeded

    def handle_expt(self):
        self.close() # connection failed, shutdown

    def handle_read(self):
        # get local time
        here = int(time.time()) + TIME1970

        # get and unpack server time
        s = self.recv(4)
        there = ord(s[3]) + (ord(s[2])<<8) + (ord(s[1])<<16) + (ord(s[0])<<24L)

        self.adjust_time(int(here - there))

        self.handle_close() # we don‘t expect more data

    def handle_close(self):
        self.close()

    def adjust_time(self, delta):
        # override this method!
        print "time difference is", delta

#
# try it out

request = TimeRequest("www.python.org")

asyncore.loop()

log: adding channel <TimeRequest  at 8cbe90>
time difference is 28
log: closing channel 192:<TimeRequest connected at 8cbe90>

If you don’t want the log messages, override the log method in your dispatchersubclass.

Here’s the corresponding time server. Note that it uses two dispatchersubclasses, one for the listening socket, and one for the client channel.

 
Example: Using the asyncore module to implement a time server
# File: asyncore-example-2.py

import asyncore
import socket, time

# reference time
TIME1970 = 2208988800L

class TimeChannel(asyncore.dispatcher):

    def handle_write(self):
        t = int(time.time()) + TIME1970
        t = chr(t>>24&255) + chr(t>>16&255) + chr(t>>8&255) + chr(t&255)
        self.send(t)
        self.close()

class TimeServer(asyncore.dispatcher):

    def __init__(self, port=37):
        asyncore.dispatcher.__init__(self)
        self.port = port
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.bind(("", port))
        self.listen(5)
        print "listening on port", self.port

    def handle_accept(self):
        channel, addr = self.accept()
        TimeChannel(channel)

server = TimeServer(8037)
asyncore.loop()

log: adding channel <TimeServer  at 8cb940>
listening on port 8037
log: adding channel <TimeChannel  at 8b2fd0>
log: closing channel 52:<TimeChannel connected at 8b2fd0>

In addition to the plain dispatcher, this module also includes adispatcher_with_send class. This class allows you send larger amounts of data, without clogging up the network transport buffers.

The following module defines an AsyncHTTP class based on thedispatcher_with_send class. When you create an instance of this class, it issues an HTTP GET request, and sends the incoming data to a “consumer”target object.

以上是关于python asyncore的主要内容,如果未能解决你的问题,请参考以下文章

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python

python初识