"""Abstract Transport class."""__all__ = ('BaseTransport', 'ReadTransport', 'WriteTransport','Transport', 'DatagramTransport', 'SubprocessTransport',)class BaseTransport:"""Base class for transports."""def __init__(self, extra=None):if extra is None:extra = {}self._extra = extradef get_extra_info(self, name, default=None):"""Get optional transport information."""return self._extra.get(name, default)def is_closing(self):"""Return True if the transport is closing or closed."""raise NotImplementedErrordef close(self):"""Close the transport.Buffered data will be flushed asynchronously. No more datawill be received. After all buffered data is flushed, theprotocol's connection_lost() method will (eventually) calledwith None as its argument."""raise NotImplementedErrordef set_protocol(self, protocol):"""Set a new protocol."""raise NotImplementedErrordef get_protocol(self):"""Return the current protocol."""raise NotImplementedErrorclass ReadTransport(BaseTransport):"""Interface for read-only transports."""def is_reading(self):"""Return True if the transport is receiving."""raise NotImplementedErrordef pause_reading(self):"""Pause the receiving end.No data will be passed to the protocol's data_received()method until resume_reading() is called."""raise NotImplementedErrordef resume_reading(self):"""Resume the receiving end.Data received will once again be passed to the protocol'sdata_received() method."""raise NotImplementedErrorclass WriteTransport(BaseTransport):"""Interface for write-only transports."""def set_write_buffer_limits(self, high=None, low=None):"""Set the high- and low-water limits for write flow control.These two values control when to call the protocol'spause_writing() and resume_writing() methods. If specified,the low-water limit must be less than or equal to thehigh-water limit. Neither value can be negative.The defaults are implementation-specific. If only thehigh-water limit is given, the low-water limit defaults to animplementation-specific value less than or equal to thehigh-water limit. Setting high to zero forces low to zero aswell, and causes pause_writing() to be called whenever thebuffer becomes non-empty. Setting low to zero causesresume_writing() to be called only once the buffer is empty.Use of zero for either limit is generally sub-optimal as itreduces opportunities for doing I/O and computationconcurrently."""raise NotImplementedErrordef get_write_buffer_size(self):"""Return the current size of the write buffer."""raise NotImplementedErrordef write(self, data):"""Write some data bytes to the transport.This does not block; it buffers the data and arranges for itto be sent out asynchronously."""raise NotImplementedErrordef writelines(self, list_of_data):"""Write a list (or any iterable) of data bytes to the transport.The default implementation concatenates the arguments andcalls write() on the result."""data = b''.join(list_of_data)self.write(data)def write_eof(self):"""Close the write end after flushing buffered data.(This is like typing ^D into a UNIX program reading from stdin.)Data may still be received."""raise NotImplementedErrordef can_write_eof(self):"""Return True if this transport supports write_eof(), False if not."""raise NotImplementedErrordef abort(self):"""Close the transport immediately.Buffered data will be lost. No more data will be received.The protocol's connection_lost() method will (eventually) becalled with None as its argument."""raise NotImplementedErrorclass Transport(ReadTransport, WriteTransport):"""Interface representing a bidirectional transport.There may be several implementations, but typically, the user doesnot implement new transports; rather, the platform provides someuseful transports that are implemented using the platform's bestpractices.The user never instantiates a transport directly; they call autility function, passing it a protocol factory and otherinformation necessary to create the transport and protocol. (E.g.EventLoop.create_connection() or EventLoop.create_server().)The utility function will asynchronously create a transport and aprotocol and hook them up by calling the protocol'sconnection_made() method, passing it the transport.The implementation here raises NotImplemented for every methodexcept writelines(), which calls write() in a loop."""class DatagramTransport(BaseTransport):"""Interface for datagram (UDP) transports."""def sendto(self, data, addr=None):"""Send data to the transport.This does not block; it buffers the data and arranges for itto be sent out asynchronously.addr is target socket address.If addr is None use target address pointed on transport creation."""raise NotImplementedErrordef abort(self):"""Close the transport immediately.Buffered data will be lost. No more data will be received.The protocol's connection_lost() method will (eventually) becalled with None as its argument."""raise NotImplementedErrorclass SubprocessTransport(BaseTransport):def get_pid(self):"""Get subprocess id."""raise NotImplementedErrordef get_returncode(self):"""Get subprocess returncode.See alsohttp://docs.python.org/3/library/subprocess#subprocess.Popen.returncode"""raise NotImplementedErrordef get_pipe_transport(self, fd):"""Get transport for pipe with number fd."""raise NotImplementedErrordef send_signal(self, signal):"""Send signal to subprocess.See also:docs.python.org/3/library/subprocess#subprocess.Popen.send_signal"""raise NotImplementedErrordef terminate(self):"""Stop the subprocess.Alias for close() method.On Posix OSs the method sends SIGTERM to the subprocess.On Windows the Win32 API function TerminateProcess()is called to stop the subprocess.See also:http://docs.python.org/3/library/subprocess#subprocess.Popen.terminate"""raise NotImplementedErrordef kill(self):"""Kill the subprocess.On Posix OSs the function sends SIGKILL to the subprocess.On Windows kill() is an alias for terminate().See also:http://docs.python.org/3/library/subprocess#subprocess.Popen.kill"""raise NotImplementedErrorclass _FlowControlMixin(Transport):"""All the logic for (write) flow control in a mix-in base class.The subclass must implement get_write_buffer_size(). It must call_maybe_pause_protocol() whenever the write buffer size increases,and _maybe_resume_protocol() whenever it decreases. It may alsooverride set_write_buffer_limits() (e.g. to specify differentdefaults).The subclass constructor must call super().__init__(extra). Thiswill call set_write_buffer_limits().The user may call set_write_buffer_limits() andget_write_buffer_size(), and their protocol's pause_writing() andresume_writing() may be called."""def __init__(self, extra=None, loop=None):super().__init__(extra)assert loop is not Noneself._loop = loopself._protocol_paused = Falseself._set_write_buffer_limits()def _maybe_pause_protocol(self):size = self.get_write_buffer_size()if size <= self._high_water:returnif not self._protocol_paused:self._protocol_paused = Truetry:self._protocol.pause_writing()except Exception as exc:self._loop.call_exception_handler({'message': 'protocol.pause_writing() failed','exception': exc,'transport': self,'protocol': self._protocol,})def _maybe_resume_protocol(self):if (self._protocol_paused andself.get_write_buffer_size() <= self._low_water):self._protocol_paused = Falsetry:self._protocol.resume_writing()except Exception as exc:self._loop.call_exception_handler({'message': 'protocol.resume_writing() failed','exception': exc,'transport': self,'protocol': self._protocol,})def get_write_buffer_limits(self):return (self._low_water, self._high_water)def _set_write_buffer_limits(self, high=None, low=None):if high is None:if low is None:high = 64 * 1024else:high = 4 * lowif low is None:low = high // 4if not high >= low >= 0:raise ValueError(f'high ({high!r}) must be >= low ({low!r}) must be >= 0')self._high_water = highself._low_water = lowdef set_write_buffer_limits(self, high=None, low=None):self._set_write_buffer_limits(high=high, low=low)self._maybe_pause_protocol()def get_write_buffer_size(self):raise NotImplementedError
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。