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.pyimport asyncore import socket, time# reference time (in seconds since 1900-01-01 00:00:00) TIME1970 = 2208988800L # 1970-01-01 00:00:00class 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 writedef handle_connect(self):pass # connection succeededdef handle_expt(self):self.close() # connection failed, shutdowndef handle_read(self):# get local timehere = int(time.time()) + TIME1970# get and unpack server times = 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 datadef handle_close(self):self.close()def adjust_time(self, delta):# override this method!print "time difference is", delta# # try it outrequest = 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.pyimport asyncore import socket, time# reference time TIME1970 = 2208988800Lclass TimeChannel(asyncore.dispatcher):def handle_write(self):t = int(time.time()) + TIME1970t = 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 = portself.create_socket(socket.AF_INET, socket.SOCK_STREAM)self.bind(("", port))self.listen(5)print "listening on port", self.portdef 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.
轉載于:https://www.cnblogs.com/zhuweiblog/p/5189037.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的python asyncore的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求职小记(持续更新)
- 下一篇: 函数名作为参数传递