Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/smanonr..../lib64/python3..../asyncio
File: protocols.py
"""Abstract Protocol base classes."""
[0] Fix | Delete
[1] Fix | Delete
__all__ = (
[2] Fix | Delete
'BaseProtocol', 'Protocol', 'DatagramProtocol',
[3] Fix | Delete
'SubprocessProtocol', 'BufferedProtocol',
[4] Fix | Delete
)
[5] Fix | Delete
[6] Fix | Delete
[7] Fix | Delete
class BaseProtocol:
[8] Fix | Delete
"""Common base class for protocol interfaces.
[9] Fix | Delete
[10] Fix | Delete
Usually user implements protocols that derived from BaseProtocol
[11] Fix | Delete
like Protocol or ProcessProtocol.
[12] Fix | Delete
[13] Fix | Delete
The only case when BaseProtocol should be implemented directly is
[14] Fix | Delete
write-only transport like write pipe
[15] Fix | Delete
"""
[16] Fix | Delete
[17] Fix | Delete
__slots__ = ()
[18] Fix | Delete
[19] Fix | Delete
def connection_made(self, transport):
[20] Fix | Delete
"""Called when a connection is made.
[21] Fix | Delete
[22] Fix | Delete
The argument is the transport representing the pipe connection.
[23] Fix | Delete
To receive data, wait for data_received() calls.
[24] Fix | Delete
When the connection is closed, connection_lost() is called.
[25] Fix | Delete
"""
[26] Fix | Delete
[27] Fix | Delete
def connection_lost(self, exc):
[28] Fix | Delete
"""Called when the connection is lost or closed.
[29] Fix | Delete
[30] Fix | Delete
The argument is an exception object or None (the latter
[31] Fix | Delete
meaning a regular EOF is received or the connection was
[32] Fix | Delete
aborted or closed).
[33] Fix | Delete
"""
[34] Fix | Delete
[35] Fix | Delete
def pause_writing(self):
[36] Fix | Delete
"""Called when the transport's buffer goes over the high-water mark.
[37] Fix | Delete
[38] Fix | Delete
Pause and resume calls are paired -- pause_writing() is called
[39] Fix | Delete
once when the buffer goes strictly over the high-water mark
[40] Fix | Delete
(even if subsequent writes increases the buffer size even
[41] Fix | Delete
more), and eventually resume_writing() is called once when the
[42] Fix | Delete
buffer size reaches the low-water mark.
[43] Fix | Delete
[44] Fix | Delete
Note that if the buffer size equals the high-water mark,
[45] Fix | Delete
pause_writing() is not called -- it must go strictly over.
[46] Fix | Delete
Conversely, resume_writing() is called when the buffer size is
[47] Fix | Delete
equal or lower than the low-water mark. These end conditions
[48] Fix | Delete
are important to ensure that things go as expected when either
[49] Fix | Delete
mark is zero.
[50] Fix | Delete
[51] Fix | Delete
NOTE: This is the only Protocol callback that is not called
[52] Fix | Delete
through EventLoop.call_soon() -- if it were, it would have no
[53] Fix | Delete
effect when it's most needed (when the app keeps writing
[54] Fix | Delete
without yielding until pause_writing() is called).
[55] Fix | Delete
"""
[56] Fix | Delete
[57] Fix | Delete
def resume_writing(self):
[58] Fix | Delete
"""Called when the transport's buffer drains below the low-water mark.
[59] Fix | Delete
[60] Fix | Delete
See pause_writing() for details.
[61] Fix | Delete
"""
[62] Fix | Delete
[63] Fix | Delete
[64] Fix | Delete
class Protocol(BaseProtocol):
[65] Fix | Delete
"""Interface for stream protocol.
[66] Fix | Delete
[67] Fix | Delete
The user should implement this interface. They can inherit from
[68] Fix | Delete
this class but don't need to. The implementations here do
[69] Fix | Delete
nothing (they don't raise exceptions).
[70] Fix | Delete
[71] Fix | Delete
When the user wants to requests a transport, they pass a protocol
[72] Fix | Delete
factory to a utility function (e.g., EventLoop.create_connection()).
[73] Fix | Delete
[74] Fix | Delete
When the connection is made successfully, connection_made() is
[75] Fix | Delete
called with a suitable transport object. Then data_received()
[76] Fix | Delete
will be called 0 or more times with data (bytes) received from the
[77] Fix | Delete
transport; finally, connection_lost() will be called exactly once
[78] Fix | Delete
with either an exception object or None as an argument.
[79] Fix | Delete
[80] Fix | Delete
State machine of calls:
[81] Fix | Delete
[82] Fix | Delete
start -> CM [-> DR*] [-> ER?] -> CL -> end
[83] Fix | Delete
[84] Fix | Delete
* CM: connection_made()
[85] Fix | Delete
* DR: data_received()
[86] Fix | Delete
* ER: eof_received()
[87] Fix | Delete
* CL: connection_lost()
[88] Fix | Delete
"""
[89] Fix | Delete
[90] Fix | Delete
__slots__ = ()
[91] Fix | Delete
[92] Fix | Delete
def data_received(self, data):
[93] Fix | Delete
"""Called when some data is received.
[94] Fix | Delete
[95] Fix | Delete
The argument is a bytes object.
[96] Fix | Delete
"""
[97] Fix | Delete
[98] Fix | Delete
def eof_received(self):
[99] Fix | Delete
"""Called when the other end calls write_eof() or equivalent.
[100] Fix | Delete
[101] Fix | Delete
If this returns a false value (including None), the transport
[102] Fix | Delete
will close itself. If it returns a true value, closing the
[103] Fix | Delete
transport is up to the protocol.
[104] Fix | Delete
"""
[105] Fix | Delete
[106] Fix | Delete
[107] Fix | Delete
class BufferedProtocol(BaseProtocol):
[108] Fix | Delete
"""Interface for stream protocol with manual buffer control.
[109] Fix | Delete
[110] Fix | Delete
Important: this has been added to asyncio in Python 3.7
[111] Fix | Delete
*on a provisional basis*! Consider it as an experimental API that
[112] Fix | Delete
might be changed or removed in Python 3.8.
[113] Fix | Delete
[114] Fix | Delete
Event methods, such as `create_server` and `create_connection`,
[115] Fix | Delete
accept factories that return protocols that implement this interface.
[116] Fix | Delete
[117] Fix | Delete
The idea of BufferedProtocol is that it allows to manually allocate
[118] Fix | Delete
and control the receive buffer. Event loops can then use the buffer
[119] Fix | Delete
provided by the protocol to avoid unnecessary data copies. This
[120] Fix | Delete
can result in noticeable performance improvement for protocols that
[121] Fix | Delete
receive big amounts of data. Sophisticated protocols can allocate
[122] Fix | Delete
the buffer only once at creation time.
[123] Fix | Delete
[124] Fix | Delete
State machine of calls:
[125] Fix | Delete
[126] Fix | Delete
start -> CM [-> GB [-> BU?]]* [-> ER?] -> CL -> end
[127] Fix | Delete
[128] Fix | Delete
* CM: connection_made()
[129] Fix | Delete
* GB: get_buffer()
[130] Fix | Delete
* BU: buffer_updated()
[131] Fix | Delete
* ER: eof_received()
[132] Fix | Delete
* CL: connection_lost()
[133] Fix | Delete
"""
[134] Fix | Delete
[135] Fix | Delete
__slots__ = ()
[136] Fix | Delete
[137] Fix | Delete
def get_buffer(self, sizehint):
[138] Fix | Delete
"""Called to allocate a new receive buffer.
[139] Fix | Delete
[140] Fix | Delete
*sizehint* is a recommended minimal size for the returned
[141] Fix | Delete
buffer. When set to -1, the buffer size can be arbitrary.
[142] Fix | Delete
[143] Fix | Delete
Must return an object that implements the
[144] Fix | Delete
:ref:`buffer protocol <bufferobjects>`.
[145] Fix | Delete
It is an error to return a zero-sized buffer.
[146] Fix | Delete
"""
[147] Fix | Delete
[148] Fix | Delete
def buffer_updated(self, nbytes):
[149] Fix | Delete
"""Called when the buffer was updated with the received data.
[150] Fix | Delete
[151] Fix | Delete
*nbytes* is the total number of bytes that were written to
[152] Fix | Delete
the buffer.
[153] Fix | Delete
"""
[154] Fix | Delete
[155] Fix | Delete
def eof_received(self):
[156] Fix | Delete
"""Called when the other end calls write_eof() or equivalent.
[157] Fix | Delete
[158] Fix | Delete
If this returns a false value (including None), the transport
[159] Fix | Delete
will close itself. If it returns a true value, closing the
[160] Fix | Delete
transport is up to the protocol.
[161] Fix | Delete
"""
[162] Fix | Delete
[163] Fix | Delete
[164] Fix | Delete
class DatagramProtocol(BaseProtocol):
[165] Fix | Delete
"""Interface for datagram protocol."""
[166] Fix | Delete
[167] Fix | Delete
__slots__ = ()
[168] Fix | Delete
[169] Fix | Delete
def datagram_received(self, data, addr):
[170] Fix | Delete
"""Called when some datagram is received."""
[171] Fix | Delete
[172] Fix | Delete
def error_received(self, exc):
[173] Fix | Delete
"""Called when a send or receive operation raises an OSError.
[174] Fix | Delete
[175] Fix | Delete
(Other than BlockingIOError or InterruptedError.)
[176] Fix | Delete
"""
[177] Fix | Delete
[178] Fix | Delete
[179] Fix | Delete
class SubprocessProtocol(BaseProtocol):
[180] Fix | Delete
"""Interface for protocol for subprocess calls."""
[181] Fix | Delete
[182] Fix | Delete
__slots__ = ()
[183] Fix | Delete
[184] Fix | Delete
def pipe_data_received(self, fd, data):
[185] Fix | Delete
"""Called when the subprocess writes data into stdout/stderr pipe.
[186] Fix | Delete
[187] Fix | Delete
fd is int file descriptor.
[188] Fix | Delete
data is bytes object.
[189] Fix | Delete
"""
[190] Fix | Delete
[191] Fix | Delete
def pipe_connection_lost(self, fd, exc):
[192] Fix | Delete
"""Called when a file descriptor associated with the child process is
[193] Fix | Delete
closed.
[194] Fix | Delete
[195] Fix | Delete
fd is the int file descriptor that was closed.
[196] Fix | Delete
"""
[197] Fix | Delete
[198] Fix | Delete
def process_exited(self):
[199] Fix | Delete
"""Called when subprocess has exited."""
[200] Fix | Delete
[201] Fix | Delete
[202] Fix | Delete
def _feed_data_to_buffered_proto(proto, data):
[203] Fix | Delete
data_len = len(data)
[204] Fix | Delete
while data_len:
[205] Fix | Delete
buf = proto.get_buffer(data_len)
[206] Fix | Delete
buf_len = len(buf)
[207] Fix | Delete
if not buf_len:
[208] Fix | Delete
raise RuntimeError('get_buffer() returned an empty buffer')
[209] Fix | Delete
[210] Fix | Delete
if buf_len >= data_len:
[211] Fix | Delete
buf[:data_len] = data
[212] Fix | Delete
proto.buffer_updated(data_len)
[213] Fix | Delete
return
[214] Fix | Delete
else:
[215] Fix | Delete
buf[:buf_len] = data[:buf_len]
[216] Fix | Delete
proto.buffer_updated(buf_len)
[217] Fix | Delete
data = data[buf_len:]
[218] Fix | Delete
data_len = len(data)
[219] Fix | Delete
[220] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function