Send 'data' to the server. ``data`` can be a string object, a bytes object, an array object, a file-like object that supports a .read() method, or an iterable object.
(self, data)
| 1051 | response.close() |
| 1052 | |
| 1053 | def send(self, data): |
| 1054 | """Send 'data' to the server. |
| 1055 | ``data`` can be a string object, a bytes object, an array object, a |
| 1056 | file-like object that supports a .read() method, or an iterable object. |
| 1057 | """ |
| 1058 | |
| 1059 | if self.sock is None: |
| 1060 | if self.auto_open: |
| 1061 | self.connect() |
| 1062 | else: |
| 1063 | raise NotConnected() |
| 1064 | |
| 1065 | if self.debuglevel > 0: |
| 1066 | print("send:", repr(data)) |
| 1067 | if hasattr(data, "read") : |
| 1068 | if self.debuglevel > 0: |
| 1069 | print("sending a readable") |
| 1070 | encode = self._is_textIO(data) |
| 1071 | if encode and self.debuglevel > 0: |
| 1072 | print("encoding file using iso-8859-1") |
| 1073 | while datablock := data.read(self.blocksize): |
| 1074 | if encode: |
| 1075 | datablock = datablock.encode("iso-8859-1") |
| 1076 | sys.audit("http.client.send", self, datablock) |
| 1077 | self.sock.sendall(datablock) |
| 1078 | return |
| 1079 | sys.audit("http.client.send", self, data) |
| 1080 | try: |
| 1081 | self.sock.sendall(data) |
| 1082 | except TypeError: |
| 1083 | if isinstance(data, collections.abc.Iterable): |
| 1084 | for d in data: |
| 1085 | self.sock.sendall(d) |
| 1086 | else: |
| 1087 | raise TypeError("data should be a bytes-like object " |
| 1088 | "or an iterable, got %r" % type(data)) |
| 1089 | |
| 1090 | def _output(self, s): |
| 1091 | """Add a line of output to the current request buffer. |