Why don Twisteds network methods support Unicode objects as well as strings?
In general, such methods (e.g. FileDescriptor’s write) are designed to send bytes over the network. These methods use non-Unicode string objects as a container for the bytes that they send and receive. Unicode objects are not byte-based and are an abstraction used for representing strings of human readable text. In order to send Unicode strings using these methods, you should explicitly specify a byte-based encoding for them, for example: s.encode(“utf-8”) and explicitly decode them at the receiving end. Twisted cannot choose an encoding for you at this level: your encoding choice will be protocol specific and may need to be specified in the message you send (for example, HTTP headers include a encoding specification). For a more complete discussion of the distinction between Unicode strings and specific encodings of Unicode strings, see the following articles: • Glyph’s Implicit encoding is bad example • Dan Sugalski’s What the heck is: A string; • Joel Spolsky’s The Absolute Minimum