当前位置: 首页 > 文档资料 > Tornado 用户手册 >

tornado.iostream — Convenient wrappers for non-blocking sockets

优质
小牛编辑
127浏览
2023-12-01

Utility classes to write to and read from non-blocking files and sockets.

Contents:

  • : Generic interface for reading and writing.
  • : Implementation of BaseIOStream using non-blocking sockets.
  • : SSL-aware version of IOStream.
  • : Pipe-based IOStream implementation.

Base class

class tornado.iostream.BaseIOStream(io_loop=None, max_buffer_size=None, read_chunk_size=None, max_write_buffer_size=None)

A utility class to write to and read from a non-blocking file or socket.

We support a non-blocking write() and a family of read_*() methods. All of the methods take an optional callback argument and return a only if no callback is given. When the operation completes, the callback will be run or the will resolve with the data read (or None for write()). All outstanding Futures will resolve with a when the stream is closed; users of the callback interface will be notified via instead.

When a stream is closed due to an error, the IOStream’s error attribute contains the exception object.

Subclasses must implement , , , , and optionally .

constructor.

参数:
  • io_loop – The to use; defaults to . Deprecated since Tornado 4.1.
  • max_buffer_size – Maximum amount of incoming data to buffer; defaults to 100MB.
  • read_chunk_size – Amount of data to read at one time from the underlying transport; defaults to 64KB.
  • max_write_buffer_size – Amount of outgoing data to buffer; defaults to unlimited.

在 4.0 版更改: Add the max_write_buffer_size parameter. Changed default read_chunk_size to 64KB.

Main interface

BaseIOStream.write(data, callback=None)

Asynchronously write the given data to this stream.

If callback is given, we call it when all of the buffered write data has been successfully written to the stream. If there was previously buffered write data and an old write callback, that callback is simply overwritten with this new callback.

If no callback is given, this method returns a that resolves (with a result of None) when the write has been completed. If is called again before that has resolved, the previous future will be orphaned and will never resolve.

在 4.0 版更改: Now returns a if no callback is given.

BaseIOStream.read_bytes(num_bytes, callback=None, streaming_callback=None, partial=False)

Asynchronously read a number of bytes.

If a streaming_callback is given, it will be called with chunks of data as they become available, and the final result will be empty. Otherwise, the result is all the data that was read. If a callback is given, it will be run with the data as an argument; if not, this method returns a .

If partial is true, the callback is run as soon as we have any bytes to return (but never more than num_bytes)

在 4.0 版更改: Added the partial argument. The callback argument is now optional and a will be returned if it is omitted.

BaseIOStream.read_until(delimiter, callback=None, max_bytes=None)

Asynchronously read until we have found the given delimiter.

The result includes all the data read including the delimiter. If a callback is given, it will be run with the data as an argument; if not, this method returns a .

If max_bytes is not None, the connection will be closed if more than max_bytes bytes have been read and the delimiter is not found.

在 4.0 版更改: Added the max_bytes argument. The callback argument is now optional and a will be returned if it is omitted.

BaseIOStream.read_until_regex(regex, callback=None, max_bytes=None)

Asynchronously read until we have matched the given regex.

The result includes the data that matches the regex and anything that came before it. If a callback is given, it will be run with the data as an argument; if not, this method returns a .

If max_bytes is not None, the connection will be closed if more than max_bytes bytes have been read and the regex is not satisfied.

在 4.0 版更改: Added the max_bytes argument. The callback argument is now optional and a will be returned if it is omitted.

BaseIOStream.read_until_close(callback=None, streaming_callback=None)

Asynchronously reads all data from the socket until it is closed.

If a streaming_callback is given, it will be called with chunks of data as they become available, and the final result will be empty. Otherwise, the result is all the data that was read. If a callback is given, it will be run with the data as an argument; if not, this method returns a .

Note that if a streaming_callback is used, data will be read from the socket as quickly as it becomes available; there is no way to apply backpressure or cancel the reads. If flow control or cancellation are desired, use a loop with instead.

在 4.0 版更改: The callback argument is now optional and a will be returned if it is omitted.

BaseIOStream.close(exc_info=False)

Close this stream.

If exc_info is true, set the error attribute to the current exception from (or if exc_info is a tuple, use that instead of ).

BaseIOStream.set_close_callback(callback)

Call the given callback when the stream is closed.

This is not necessary for applications that use the interface; all outstanding Futures will resolve with a when the stream is closed.

BaseIOStream.closed()

Returns true if the stream has been closed.

BaseIOStream.reading()

Returns true if we are currently reading from the stream.

BaseIOStream.writing()

Returns true if we are currently writing to the stream.

BaseIOStream.set_nodelay(value)

Sets the no-delay flag for this stream.

By default, data written to TCP streams may be held for a time to make the most efficient use of bandwidth (according to Nagle’s algorithm). The no-delay flag requests that data be written as soon as possible, even if doing so would consume additional bandwidth.

This flag is currently defined only for TCP-based IOStreams.

3.1 新版功能.

Methods for subclasses

BaseIOStream.fileno()

Returns the file descriptor for this stream.

BaseIOStream.close_fd()

Closes the file underlying this stream.

close_fd is called by and should not be called elsewhere; other users should call instead.

BaseIOStream.write_to_fd(data)

Attempts to write data to the underlying file.

Returns the number of bytes written.

BaseIOStream.read_from_fd()

Attempts to read from the underlying file.

Returns None if there was nothing to read (the socket returned or equivalent), otherwise returns the data. When possible, should return no more than self.read_chunk_size bytes at a time.

BaseIOStream.get_fd_error()

Returns information about any error on the underlying file.

This method is called after the has signaled an error on the file descriptor, and should return an Exception (such as with additional information, or None if no such information is available.

Implementations

class tornado.iostream.IOStream(socket, *args, **kwargs)

Socket-based implementation.

This class supports the read and write methods from plus a method.

The socket parameter may either be connected or unconnected. For server operations the socket is the result of calling . For client operations the socket is created with , and may either be connected before passing it to the or connected with .

A very simple (and broken) HTTP client using this class:

import tornado.ioloop
import tornado.iostream
import socket

def send_request():
    stream.write(b"GET / HTTP/1.0\r\nHost: friendfeed.com\r\n\r\n")
    stream.read_until(b"\r\n\r\n", on_headers)

def on_headers(data):
    headers = {}
    for line in data.split(b"\r\n"):
       parts = line.split(b":")
       if len(parts) == 2:
           headers[parts[0].strip()] = parts[1].strip()
    stream.read_bytes(int(headers[b"Content-Length"]), on_body)

def on_body(data):
    print(data)
    stream.close()
    tornado.ioloop.IOLoop.current().stop()

if __name__ == '__main__':
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    stream = tornado.iostream.IOStream(s)
    stream.connect(("friendfeed.com", 80), send_request)
    tornado.ioloop.IOLoop.current().start()
connect(address, callback=None, server_hostname=None)

Connects the socket to a remote address without blocking.

May only be called if the socket passed to the constructor was not previously connected. The address parameter is in the same format as for for the type of socket passed to the IOStream constructor, e.g. an (ip, port) tuple. Hostnames are accepted here, but will be resolved synchronously and block the IOLoop. If you have a hostname instead of an IP address, the class is recommended instead of calling this method directly. will do asynchronous DNS resolution and handle both IPv4 and IPv6.

If callback is specified, it will be called with no arguments when the connection is completed; if not this method returns a (whose result after a successful connection will be the stream itself).

In SSL mode, the server_hostname parameter will be used for certificate validation (unless disabled in the ssl_options) and SNI (if supported; requires Python 2.7.9+).

Note that it is safe to call while the connection is pending, in which case the data will be written as soon as the connection is ready. Calling read methods before the socket is connected works on some platforms but is non-portable.

在 4.0 版更改: If no callback is given, returns a .

在 4.2 版更改: SSL certificates are validated by default; pass ssl_options=dict(cert_reqs=ssl.CERT_NONE) or a suitably-configured to the constructor to disable.

start_tls(server_side, ssl_options=None, server_hostname=None)

Convert this to an .

This enables protocols that begin in clear-text mode and switch to SSL after some initial negotiation (such as the STARTTLS extension to SMTP and IMAP).

This method cannot be used if there are outstanding reads or writes on the stream, or if there is any data in the IOStream’s buffer (data in the operating system’s socket buffer is allowed). This means it must generally be used immediately after reading or writing the last clear-text data. It can also be used immediately after connecting, before any reads or writes.

The ssl_options argument may be either an object or a dictionary of keyword arguments for the function. The server_hostname argument will be used for certificate validation unless disabled in the ssl_options.

This method returns a whose result is the new . After this method has been called, any other operation on the original stream is undefined.

If a close callback is defined on this stream, it will be transferred to the new stream.

4.0 新版功能.

在 4.2 版更改: SSL certificates are validated by default; pass ssl_options=dict(cert_reqs=ssl.CERT_NONE) or a suitably-configured to disable.

class tornado.iostream.SSLIOStream(*args, **kwargs)

A utility class to write to and read from a non-blocking SSL socket.

If the socket passed to the constructor is already connected, it should be wrapped with:

ssl.wrap_socket(sock, do_handshake_on_connect=False, **kwargs)

before constructing the . Unconnected sockets will be wrapped when is finished.

The ssl_options keyword argument may either be an object or a dictionary of keywords arguments for

wait_for_handshake(callback=None)

Wait for the initial SSL handshake to complete.

If a callback is given, it will be called with no arguments once the handshake is complete; otherwise this method returns a which will resolve to the stream itself after the handshake is complete.

Once the handshake is complete, information such as the peer’s certificate and NPN/ALPN selections may be accessed on self.socket.

This method is intended for use on server-side streams or after using ; it should not be used with (which already waits for the handshake to complete). It may only be called once per stream.

4.2 新版功能.

class tornado.iostream.PipeIOStream(fd, *args, **kwargs)

Pipe-based implementation.

The constructor takes an integer file descriptor (such as one returned by ) rather than an open file object. Pipes are generally one-way, so a can be used for reading or writing but not both.

Exceptions

exception tornado.iostream.StreamBufferFullError

Exception raised by methods when the buffer is full.

exception tornado.iostream.StreamClosedError(real_error=None)

Exception raised by methods when the stream is closed.

Note that the close callback is scheduled to run after other callbacks on the stream (to allow for buffered data to be processed), so you may see this error before you see the close callback.

The real_error attribute contains the underlying error that caused the stream to close (if any).

在 4.3 版更改: Added the real_error attribute.

exception tornado.iostream.UnsatisfiableReadError

Exception raised when a read cannot be satisfied.

Raised by read_until and read_until_regex with a max_bytes argument.