Send data to the server. `data` can be: `str`, `bytes`, an iterable, or file-like objects that support a .read() method.
(self, data: typing.Any)
| 168 | self._headers = [] # Reset headers for the next request. |
| 169 | |
| 170 | def send(self, data: typing.Any) -> None: |
| 171 | """Send data to the server. |
| 172 | `data` can be: `str`, `bytes`, an iterable, or file-like objects |
| 173 | that support a .read() method. |
| 174 | """ |
| 175 | if self._h2_stream is None: |
| 176 | raise ConnectionError("Must call `putrequest` first.") |
| 177 | |
| 178 | with self._h2_conn as conn: |
| 179 | if data_to_send := conn.data_to_send(): |
| 180 | self.sock.sendall(data_to_send) |
| 181 | |
| 182 | if hasattr(data, "read"): # file-like objects |
| 183 | while True: |
| 184 | chunk = data.read(self.blocksize) |
| 185 | if not chunk: |
| 186 | break |
| 187 | if isinstance(chunk, str): |
| 188 | chunk = chunk.encode() |
| 189 | conn.send_data(self._h2_stream, chunk, end_stream=False) |
| 190 | if data_to_send := conn.data_to_send(): |
| 191 | self.sock.sendall(data_to_send) |
| 192 | conn.end_stream(self._h2_stream) |
| 193 | return |
| 194 | |
| 195 | if isinstance(data, str): # str -> bytes |
| 196 | data = data.encode() |
| 197 | |
| 198 | try: |
| 199 | if isinstance(data, bytes): |
| 200 | conn.send_data(self._h2_stream, data, end_stream=True) |
| 201 | if data_to_send := conn.data_to_send(): |
| 202 | self.sock.sendall(data_to_send) |
| 203 | else: |
| 204 | for chunk in data: |
| 205 | conn.send_data(self._h2_stream, chunk, end_stream=False) |
| 206 | if data_to_send := conn.data_to_send(): |
| 207 | self.sock.sendall(data_to_send) |
| 208 | conn.end_stream(self._h2_stream) |
| 209 | except TypeError: |
| 210 | raise TypeError( |
| 211 | "`data` should be str, bytes, iterable, or file. got %r" |
| 212 | % type(data) |
| 213 | ) |
| 214 | |
| 215 | def set_tunnel( |
| 216 | self, |