"""Abstract Protocol base classes."""__all__ = ('BaseProtocol', 'Protocol', 'DatagramProtocol','SubprocessProtocol', 'BufferedProtocol',)class BaseProtocol:"""Common base class for protocol interfaces.Usually user implements protocols that derived from BaseProtocollike Protocol or ProcessProtocol.The only case when BaseProtocol should be implemented directly iswrite-only transport like write pipe"""def connection_made(self, transport):"""Called when a connection is made.The argument is the transport representing the pipe connection.To receive data, wait for data_received() calls.When the connection is closed, connection_lost() is called."""def connection_lost(self, exc):"""Called when the connection is lost or closed.The argument is an exception object or None (the lattermeaning a regular EOF is received or the connection wasaborted or closed)."""def pause_writing(self):"""Called when the transport's buffer goes over the high-water mark.Pause and resume calls are paired -- pause_writing() is calledonce when the buffer goes strictly over the high-water mark(even if subsequent writes increases the buffer size evenmore), and eventually resume_writing() is called once when thebuffer size reaches the low-water mark.Note that if the buffer size equals the high-water mark,pause_writing() is not called -- it must go strictly over.Conversely, resume_writing() is called when the buffer size isequal or lower than the low-water mark. These end conditionsare important to ensure that things go as expected when eithermark is zero.NOTE: This is the only Protocol callback that is not calledthrough EventLoop.call_soon() -- if it were, it would have noeffect when it's most needed (when the app keeps writingwithout yielding until pause_writing() is called)."""def resume_writing(self):"""Called when the transport's buffer drains below the low-water mark.See pause_writing() for details."""class Protocol(BaseProtocol):"""Interface for stream protocol.The user should implement this interface. They can inherit fromthis class but don't need to. The implementations here donothing (they don't raise exceptions).When the user wants to requests a transport, they pass a protocolfactory to a utility function (e.g., EventLoop.create_connection()).When the connection is made successfully, connection_made() iscalled with a suitable transport object. Then data_received()will be called 0 or more times with data (bytes) received from thetransport; finally, connection_lost() will be called exactly oncewith either an exception object or None as an argument.State machine of calls:start -> CM [-> DR*] [-> ER?] -> CL -> end* CM: connection_made()* DR: data_received()* ER: eof_received()* CL: connection_lost()"""def data_received(self, data):"""Called when some data is received.The argument is a bytes object."""def eof_received(self):"""Called when the other end calls write_eof() or equivalent.If this returns a false value (including None), the transportwill close itself. If it returns a true value, closing thetransport is up to the protocol."""class BufferedProtocol(BaseProtocol):"""Interface for stream protocol with manual buffer control.Important: this has been added to asyncio in Python 3.7*on a provisional basis*! Consider it as an experimental API thatmight be changed or removed in Python 3.8.Event methods, such as `create_server` and `create_connection`,accept factories that return protocols that implement this interface.The idea of BufferedProtocol is that it allows to manually allocateand control the receive buffer. Event loops can then use the bufferprovided by the protocol to avoid unnecessary data copies. Thiscan result in noticeable performance improvement for protocols thatreceive big amounts of data. Sophisticated protocols can allocatethe buffer only once at creation time.State machine of calls:start -> CM [-> GB [-> BU?]]* [-> ER?] -> CL -> end* CM: connection_made()* GB: get_buffer()* BU: buffer_updated()* ER: eof_received()* CL: connection_lost()"""def get_buffer(self, sizehint):"""Called to allocate a new receive buffer.*sizehint* is a recommended minimal size for the returnedbuffer. When set to -1, the buffer size can be arbitrary.Must return an object that implements the:ref:`buffer protocol <bufferobjects>`.It is an error to return a zero-sized buffer."""def buffer_updated(self, nbytes):"""Called when the buffer was updated with the received data.*nbytes* is the total number of bytes that were written tothe buffer."""def eof_received(self):"""Called when the other end calls write_eof() or equivalent.If this returns a false value (including None), the transportwill close itself. If it returns a true value, closing thetransport is up to the protocol."""class DatagramProtocol(BaseProtocol):"""Interface for datagram protocol."""def datagram_received(self, data, addr):"""Called when some datagram is received."""def error_received(self, exc):"""Called when a send or receive operation raises an OSError.(Other than BlockingIOError or InterruptedError.)"""class SubprocessProtocol(BaseProtocol):"""Interface for protocol for subprocess calls."""def pipe_data_received(self, fd, data):"""Called when the subprocess writes data into stdout/stderr pipe.fd is int file descriptor.data is bytes object."""def pipe_connection_lost(self, fd, exc):"""Called when a file descriptor associated with the child process isclosed.fd is the int file descriptor that was closed."""def process_exited(self):"""Called when subprocess has exited."""def _feed_data_to_buffered_proto(proto, data):data_len = len(data)while data_len:buf = proto.get_buffer(data_len)buf_len = len(buf)if not buf_len:raise RuntimeError('get_buffer() returned an empty buffer')if buf_len >= data_len:buf[:data_len] = dataproto.buffer_updated(data_len)returnelse:buf[:buf_len] = data[:buf_len]proto.buffer_updated(buf_len)data = data[buf_len:]data_len = len(data)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。