Convert a string to L{bytes} using ASCII encoding. This is useful for sending text-like bytes that are constructed using string interpolation. For example:: networkString("Hello %d" % (n,)) @param s: A string to convert to bytes. @type s: L{str} @raise UnicodeEr
(s: str)
| 440 | |
| 441 | |
| 442 | def networkString(s: str) -> bytes: |
| 443 | """ |
| 444 | Convert a string to L{bytes} using ASCII encoding. |
| 445 | |
| 446 | This is useful for sending text-like bytes that are constructed using |
| 447 | string interpolation. For example:: |
| 448 | |
| 449 | networkString("Hello %d" % (n,)) |
| 450 | |
| 451 | @param s: A string to convert to bytes. |
| 452 | @type s: L{str} |
| 453 | |
| 454 | @raise UnicodeError: The input string is not ASCII encodable. |
| 455 | @raise TypeError: The input is not L{str}. |
| 456 | |
| 457 | @rtype: L{bytes} |
| 458 | """ |
| 459 | if not isinstance(s, str): |
| 460 | raise TypeError("Can only convert strings to bytes") |
| 461 | return s.encode("ascii") |
| 462 | |
| 463 | |
| 464 | @deprecated(Version("Twisted", 21, 2, 0), replacement="os.environb") |