IO, streams, and requests
IO stands for input/output, and it broadly refers to the communication between a computer and the outside world. There are several different types of IO, and it is outside the scope of this chapter to explain all of them, but I still want to offer you a couple of examples.
Using an in-memory stream
The first will show you the io.StringIO class, which is an in-memory stream for text IO. The second one instead will escape the locality of our computer, and show you how to perform an HTTP request. Let's see the first example:
# io_examples/string_io.py
import io
stream = io.StringIO()
stream.write('Learning Python Programming.\n')
print('Become a Python ninja!', file=stream)
contents = stream.getvalue()
print(contents)
stream.close()In the preceding code snippet, we import the io module from the standard library. This is a very interesting module that features many tools related to streams and IO. One of them is StringIO, which is an in-memory buffer in which we're...