---
title: "Writing a Greeter Client"
url: https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client
language: python
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Writing a Greeter Client

Programming language: Python. Other languages: [C++](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/cpp.md), [C#](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/java.md), [JavaScript](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/js.md), [MATLAB](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/matlab.md), [PHP](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/php.md), [Ruby](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/ruby.md), [Swift](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/swift.md).

This page presents a step-by-step guide to writing the client-side of our Python Greeter application.

This client creates a proxy to a remote object that implements the `Greeter` interface and invokes the `greet` operation on this object.

You can find the complete source code for this example in the [ice-demos](https://github.com/zeroc-ice/ice-demos/tree/3.8/python/Ice/greeter) repository on GitHub.

## Compile Slice File with Slice Compiler

The first step when writing a Python application with Ice is to compile its Slice definitions using the Slice to Python compiler (`slice2py`).

The demo is a [uv](https://docs.astral.sh/uv/) project. uv is a Python package and project manager: the demo’s `pyproject.toml` declares a dependency on the `zeroc-ice` package, which provides the Ice for Python runtime and the `slice2py` compiler. `uv run` runs a command in the project’s virtual environment, and creates this environment and installs the dependencies the first time you run it.

Here we compile the `Greeter.ice` Slice file we wrote earlier, from the demo’s `client` directory:

```shell
uv run slice2py ../slice/Greeter.ice
```

Without uv, you install `zeroc-ice` with `pip` in a virtual environment, then run `slice2py ../slice/Greeter.ice` and `python main.py` in that environment.

This compilation generates a Python package named `VisitorCenter`, which matches the Slice module name. Inside this package, you’ll find the generated `Greeter` module corresponding to the `Greeter` interface defined in Slice. This module provides the APIs that our client code will call, so generating it is an essential first step in the development process.

## Client Implementation

The structure of our client is going to look like:

```py
import asyncio
import sys

import Ice

# Slice module VisitorCenter in Greeter.ice maps to Python module VisitorCenter.
import VisitorCenter


async def main():
    ...


if __name__ == "__main__":
    asyncio.run(main())
```

Before anything else, we need to import a few packages:

- `asyncio` To write async code using `async/await` syntax.
- `sys` For accessing the command line arguments.
- `Ice` For accessing the Ice runtime.
- `VisitorCenter` The package generated by `slice2py` from `Greeter.ice`.

Then we get to the interesting part: the `main` coroutine, which `asyncio.run` runs in a new event loop. This logic can be broken down into four pieces:

### 1. Create a Communicator

First, we create a [Communicator](https://code.zeroc.com/ice/3.8/api/python/Ice.Communicator.html) using its constructor:

```py
async def main():
    async with Ice.Communicator(
        sys.argv, eventLoop=asyncio.get_running_loop()) as communicator:
```

The communicator is the main entry point into the Ice runtime. Its responsibilities include establishing connections to servers, caching these connections, and managing configuration properties. We also need a communicator to create a proxy (see next step).

Our client, like most Ice applications, creates a single communicator.

We define main as an `async def` function to build an asynchronous application. By passing the current asyncio event loop to the `Ice.Communicator` constructor, we ensure that the communicator integrates with this loop for all asynchronous operations.

It is important to always destroy the communicator before exiting the application. This guarantees that network connections are properly closed and other clean-up tasks are performed. In an asynchronous application, the simplest way to achieve this is to use the `async with` statement, as shown above.

### 2. Create a Greeter Proxy

Next, we need a way to call on a remote `Greeter` object. In Ice, this is done with *proxies*. Proxies are local constructs that represent remote Ice objects and provide functions to call operations on those objects.

We create a `Greeter` proxy by calling the constructor of the `GreeterPrx` class generated by the Slice compiler:

```py
greeter = VisitorCenter.GreeterPrx(communicator, "greeter:tcp -h localhost -p 4061")
```

The constructor accepts our communicator and a “stringified proxy” with the address of the remote Ice object. Here, our stringified proxy says that the target Ice object is named “greeter” and can be reached via `tcp` on `localhost` on port `4061`.

> **Note**
>
> The name of the interface (`Greeter`) and the identity of the Ice object (`greeter`) are independent. The Ice objects hosted in the server could just as easily have identities like `santa`, `bugsBunny`, etc.

### 3. Make an Invocation

The third step is to call `greet` on the remote Ice object using our proxy and then print the returned greeting:

```py
username = ...
greeting = await greeter.greetAsync(username);
print(greeting);
```

The `greetAsync` function does all the heavy lifting for us: the proxy creates a request with the username string, the communicator establishes a connection to `localhost:4061`, and the request is sent over it. When a response is received, the proxy unmarshals its payload and returns a string (the greeting).

Note that `greetAsync` returns an `Awaitable` object that we await. This allows the event loop thread to do other work while it is waiting for the invocation to complete.

> **Note**
>
> Because the communicator was initialized with an asyncio event loop, the returned Awaitable is an `asyncio.Future`, which can be awaited within the same event loop.

### 4. Cleanup

Finally, at the end of our logic, our communicator goes out of scope and is destroyed automatically (because we used the `async with` statement), and then our application exits.

## Running the Client

After compiling the Slice definitions, you run the client from the demo’s `client` directory with:

```shell
uv run main.py
```

> **Note**
>
> This client won’t work unless you’ve also launched a Greeter server — see [Writing a Greeter Server](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-server/python.md).
