# Local Flight server

The PyArrow server listens on `127.0.0.1:8815` and serves a two-row `demo/people` table. It keeps uploaded tables in memory until restart and returns the number of rows written as application metadata.

## Install PyArrow

Install PyArrow in a virtual environment with Python 3.11 or later.

```sh
python -m pip install pyarrow==26.0.1
```

## Start the server

Save the server source to a file.

`server.py`

```python
import pyarrow as pa
from pyarrow import flight


class DemoServer(flight.FlightServerBase):
    def __init__(self):
        super().__init__('grpc://127.0.0.1:8815')
        self.tables = {
            ('demo', 'people'): pa.table({
                'id': pa.array([1, 2], type=pa.int32()),
                'name': ['Ada', 'Lin'],
            }),
        }

    def describe(self, path):
        table = self.tables.get(path)
        if table is None:
            raise flight.FlightInternalError(
                'Unknown table', extra_info=b'The Flight table is not registered'
            )

        descriptor = flight.FlightDescriptor.for_path(*path)
        ticket = flight.Ticket('/'.join(path).encode())
        endpoint = flight.FlightEndpoint(ticket, [])
        return flight.FlightInfo(
            table.schema, descriptor, [endpoint], table.num_rows, table.nbytes
        )

    def list_flights(self, context, criteria):
        for path in tuple(self.tables):
            yield self.describe(path)

    def get_flight_info(self, context, descriptor):
        return self.describe(tuple(part.decode() for part in descriptor.path))

    def do_get(self, context, ticket):
        path = tuple(ticket.ticket.decode().split('/'))
        return flight.RecordBatchStream(self.tables[path])

    def do_put(self, context, descriptor, reader, writer):
        path = tuple(part.decode() for part in descriptor.path)
        table = reader.read_all()
        self.tables[path] = table
        writer.write(pa.py_buffer(str(table.num_rows).encode()))


if __name__ == '__main__':
    DemoServer().serve()
```

Start the server in a separate terminal.

```sh
python server.py
```

Once the server is running, make your [first request](/arrow-flight-client) and try [uploading a table](/arrow-flight-client/examples/upload-table).
