# Send readings from a Python script

# Send readings from a Python script

Send readings from your own Python code, such as a bench script or a sensor
with no connector. They show as live channels.

## Before you start

- Python 3 on the same computer as Phloem. No extra packages needed.
- The push address from [Send your own data](https://phloem.ai/docs/inputs/push/#the-endpoint).

## Steps

1. Open **Inputs** and press **Add input**.
2. Choose **Push endpoint**.
3. Set **Codec** to `batch`.
4. Press **Add & start**.
5. Save this script as `send_readings.py`:

   ```python
   import json, math, sys, time, urllib.request

   URL = sys.argv[1]  # the push address, with batch as the codec

   for i in range(600):
       t_us = time.time_ns() // 1000
       body = [
           {"path": "/bench/temp_c",
            "samples": [{"t_us": t_us, "value": {"f64": 21.5 + math.sin(i / 10)}}]},
           {"path": "/bench/step",
            "samples": [{"t_us": t_us, "value": {"i64": i}}]},
       ]
       req = urllib.request.Request(
           URL, data=json.dumps(body).encode(),
           headers={"Content-Type": "application/json"})
       with urllib.request.urlopen(req) as r:
           print(r.read().decode())
       time.sleep(0.1)
   ```

6. Run it with the push address, `<codec>` replaced by `batch`:

   ```bash
   python3 send_readings.py <push address>
   ```

## Check it worked

Each POST prints `{"codec":"batch","batches":2,"samples":2}`. On **Inputs**,
the **Push endpoint** row counts samples. On **Live**, `/bench/temp_c` and
`/bench/step` appear in the channel list.

| The script prints | Do |
|---|---|
| `HTTP Error 403` | no **Push endpoint** input is open for `batch` — steps 1–4 |
| `Connection refused` | Phloem is not running on this computer |
| `HTTP Error 400` | the body is not valid `batch` — compare with the script above |

> **Tip:** Ran the script before adding the input? **Inputs** lists it under
> **Sending to this device**. Press **Enable** instead of steps 1–4.

- [Send your own data](https://phloem.ai/docs/inputs/push/): Every codec, body format and response code
- [Name channels and units](https://phloem.ai/docs/inputs/channels/): Name a signal, set its unit
- [Verify that your data is arriving](https://phloem.ai/docs/guides/verify-data/): Read an input's row when nothing shows
