Skip to content

Commit

Permalink
neat python
Browse files Browse the repository at this point in the history
  • Loading branch information
feuyeux committed Oct 1, 2024
1 parent d446fdf commit 0cd0c03
Show file tree
Hide file tree
Showing 21 changed files with 294 additions and 135 deletions.
6 changes: 3 additions & 3 deletions docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
|:----|:-----------------------------|:---------------------------------------------------------------|:---------------------------------|:---------------------------------|
| 1 | [C++](hello-grpc-cpp) | | feuyeux/grpc_server_cpp:1.0.0 | feuyeux/grpc_client_cpp:1.0.0 |
| 2 | [Rust](hello-grpc-rust) | | feuyeux/grpc_server_rust:1.0.0 | feuyeux/grpc_client_rust:1.0.0 |
| 3 | [Java](hello-grpc-java) | [openjdk:21-jdk-slim](java_grpc.dockerfile) | feuyeux/grpc_server_java:1.0.0 | feuyeux/grpc_client_java:1.0.0 |
| 3 | [Java](hello-grpc-java) | [openjdk:21-jdk-slim](java_grpc.dockerfile) | feuyeux/grpc_server_java:1.0.0 | feuyeux/grpc_client_java:1.0.0 |
| 4 | [Go](hello-grpc-go) | [golang:1.23-alpine](go_grpc.dockerfile) | feuyeux/grpc_server_go:1.0.0 | feuyeux/grpc_client_go:1.0.0 |
| 5 | [C#](hello-grpc-csharp) | [mcr.microsoft.com/dotnet/runtime:8.0](csharp_grpc.dockerfile) | feuyeux/grpc_server_csharp:1.0.0 | feuyeux/grpc_client_csharp:1.0.0 |
| 6 | [Python](hello-grpc-python) | | feuyeux/grpc_server_python:1.0.0 | feuyeux/grpc_client_python:1.0.0 |
| 6 | [Python](hello-grpc-python) | [python:3.11-slim](python_grpc.dockerfile) | feuyeux/grpc_server_python:1.0.0 | feuyeux/grpc_client_python:1.0.0 |
| 7 | [Node.js](hello-grpc-nodejs) | | feuyeux/grpc_server_nodejs:1.0.0 | feuyeux/grpc_client_nodejs:1.0.0 |
| 8 | [Dart](hello-grpc-dart) | | feuyeux/grpc_server_dart:1.0.0 | feuyeux/grpc_client_dart:1.0.0 |
| 9 | [Kotlin](hello-grpc-kotlin) | [openjdk:21-jdk-slim](kotlin_grpc.dockerfile) | feuyeux/grpc_server_kotlin:1.0.0 | feuyeux/grpc_client_kotlin:1.0.0 |
| 9 | [Kotlin](hello-grpc-kotlin) | [openjdk:21-jdk-slim](kotlin_grpc.dockerfile) | feuyeux/grpc_server_kotlin:1.0.0 | feuyeux/grpc_client_kotlin:1.0.0 |
| 10 | [Swift](hello-grpc-swift) | | feuyeux/grpc_server_swift:1.0.0 | feuyeux/grpc_client_swift:1.0.0 |
| 11 | [PHP](hello-grpc-php) | | feuyeux/grpc_server_php:1.0.0 | feuyeux/grpc_client_php:1.0.0 |
| 12 | [TypeScript](hello-grpc-ts) | | feuyeux/grpc_server_ts:1.0.0 | feuyeux/grpc_client_ts:1.0.0 |
Expand Down
9 changes: 0 additions & 9 deletions docker/grpc-client-python.dockerfile

This file was deleted.

10 changes: 0 additions & 10 deletions docker/grpc-server-python.dockerfile

This file was deleted.

13 changes: 8 additions & 5 deletions docker/build_python.sh → docker/python_build.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ cd "$(
pwd -P
)/" || exit
set -e

docker images | grep python | awk '{print $1}'

echo "~~~ build grpc server python ~~~"
mkdir -p hello-grpc-python
cp ../hello-grpc-python/requirements.txt hello-grpc-python
Expand All @@ -12,15 +15,15 @@ cp -R ../hello-grpc-python/server hello-grpc-python
cp ../hello-grpc-python/server_start.sh hello-grpc-python
cp -R ../hello-grpc-python/proto hello-grpc-python
cp ../hello-grpc-python/proto2py.sh hello-grpc-python
docker build -f grpc-server-python.dockerfile -t feuyeux/grpc_server_python:1.0.0 .
rm -rf hello-grpc-python/server
rm -rf hello-grpc-python/server_start.sh
docker build -f python_grpc.dockerfile --target server -t feuyeux/grpc_server_python:1.0.0 .
echo

echo "~~~ build grpc client python ~~~"
cp -R ../hello-grpc-python/conn hello-grpc-python
# cp -R ../hello-grpc-python/conn hello-grpc-python
cp -R ../hello-grpc-python/client hello-grpc-python
cp ../hello-grpc-python/client_start.sh hello-grpc-python
docker build -f grpc-client-python.dockerfile -t feuyeux/grpc_client_python:1.0.0 .
rm -rf hello-grpc-python/server
rm -rf hello-grpc-python/server_start.sh
docker build -f python_grpc.dockerfile --target client -t feuyeux/grpc_client_python:1.0.0 .
rm -rf hello-grpc-python
echo
21 changes: 21 additions & 0 deletions docker/python_grpc.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM python:3.11-slim AS server
# https://hub.docker.com/_/python
COPY hello-grpc-python grpc-server
RUN pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
WORKDIR /grpc-server
RUN pip install --upgrade pip
RUN pip install -r requirements.txt --no-cache-dir
RUN sh proto2py.sh
COPY tls/server_certs /var/hello_grpc/server_certs
COPY tls/client_certs /var/hello_grpc/client_certs
ENTRYPOINT ["sh","server_start.sh"]

FROM python:3.11-slim AS client
COPY hello-grpc-python grpc-client
RUN pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
WORKDIR /grpc-client
RUN pip install --upgrade pip
RUN pip install -r requirements.txt --no-cache-dir
RUN sh proto2py.sh
COPY tls/client_certs /var/hello_grpc/client_certs
CMD ["sh","client_start.sh"]
3 changes: 3 additions & 0 deletions docker/python_push.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
docker push feuyeux/grpc_server_python:1.0.0
docker push feuyeux/grpc_client_python:1.0.0
13 changes: 13 additions & 0 deletions docker/python_run_client.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
cd "$(
cd "$(dirname "$0")" >/dev/null 2>&1
pwd -P
)/" || exit
export CLIENT_NAME=grpc_client_python
export CLIENT_IMG=feuyeux/$CLIENT_NAME:1.0.0
# if there's first argument, it's secure, otherwise insecure
if [ "$1" = "secure" ]; then
sh run_tls_client.sh
else
sh run_insecure_client.sh
fi
13 changes: 13 additions & 0 deletions docker/python_run_server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
cd "$(
cd "$(dirname "$0")" >/dev/null 2>&1
pwd -P
)/" || exit
export SERVER_NAME=grpc_server_python
export SERVER_IMG=feuyeux/$SERVER_NAME:1.0.0
# if there's first argument, it's secure, otherwise insecure
if [ "$1" = "secure" ]; then
sh run_tls_server.sh
else
sh run_insecure_server.sh
fi
61 changes: 17 additions & 44 deletions hello-grpc-python/README.md
Original file line number Diff line number Diff line change
@@ -1,77 +1,50 @@
## grpc python demo
# grpc python demo

### 1 Setup
## 1 Setup

```bash
```sh
# 1. Aliyun's mirror of the Python Package Index (PyPI)
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple
# 2. Tsinghua University's mirror of PyPI
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
# 3. University of Science and Technology of China's mirror of PyPI
pip config set global.index-url https://pypi.mirrors.ustc.edu.cn/simple
```

#### python2

```bash
python -m pip install --upgrade pip
pip install virtualenv

which virtualenv
/Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenv
```

#### python3

```bash
python3 -m pip install --upgrade pip
export PATH="/Users/han/Library/Python/3.8/bin:$PATH"
pip3 install virtualenv

which virtualenv
/Users/han/Library/Python/3.8/bin/virtualenv
```sh
sh init.sh
```

```bash
virtualenv venv
/Users/han/Library/Python/3.8/bin/virtualenv venv

source venv/bin/activate
python -m pip install --upgrade pip

```sh
(
#generate requirements.txt with dependencies
pip install pipreqs
pipreqs --encoding utf-8 . --force
)

# https://pypi.org/project/grpcio-tools/
# https://pypi.org/project/protobuf/
# https://pypi.org/project/futures/
# enum34-1.1.10 futures-3.3.0 grpcio-1.41.1 grpcio-tools-1.41.1 protobuf-3.18.0 six-1.16.0
#pip install grpcio-tools
pip install -r requirements.txt

(python -m pip install grpcio)
```

### 2 Generate
## 2 Generate

```bash
conda activate grpc_env
sh proto2py.sh
```

### 3 Run
## 3 Run

```bash
conda activate grpc_env
sh server_start.sh
```

```bash
source venv/bin/activate

conda activate grpc_env
sh client_start.sh
```

#### UT
### UT

```sh
python3 -m unittest tests/test_utils.py
conda activate grpc_env
python -m unittest tests/test_utils.py
```
6 changes: 1 addition & 5 deletions hello-grpc-python/client/protoClient.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
# encoding: utf-8
import logging
import os
import random

import time

from conn import connection, utils

from landing import landing_pb2
from landing import landing_pb2_grpc
from conn import connection, utils, landing_pb2_grpc,landing_pb2

logger = logging.getLogger('grpc-client')
logger.setLevel(logging.INFO)
Expand Down
8 changes: 5 additions & 3 deletions hello-grpc-python/client_start.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#!/bin/bash
# shellcheck disable=SC2155

cd "$(
cd "$(dirname "$0")" >/dev/null 2>&1
pwd -P
)/" || exit

export PYTHONPATH=$(pwd)
export PYTHONPATH=$PYTHONPATH:$(pwd)/landing_pb2
alias python=python3
python -V

export PYTHONPATH=$(pwd)
export PYTHONPATH=$PYTHONPATH:$(pwd)/landing
echo "PYTHONPATH=${PYTHONPATH}"
echo "starting client..."
python3 client/protoClient.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions hello-grpc-python/conn/landing_pb2.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from google.protobuf.internal import containers as _containers
from google.protobuf.internal import enum_type_wrapper as _enum_type_wrapper
from google.protobuf import descriptor as _descriptor
from google.protobuf import message as _message
from typing import ClassVar as _ClassVar, Iterable as _Iterable, Mapping as _Mapping, Optional as _Optional, Union as _Union

DESCRIPTOR: _descriptor.FileDescriptor

class ResultType(int, metaclass=_enum_type_wrapper.EnumTypeWrapper):
__slots__ = ()
OK: _ClassVar[ResultType]
FAIL: _ClassVar[ResultType]
OK: ResultType
FAIL: ResultType

class TalkRequest(_message.Message):
__slots__ = ("data", "meta")
DATA_FIELD_NUMBER: _ClassVar[int]
META_FIELD_NUMBER: _ClassVar[int]
data: str
meta: str
def __init__(self, data: _Optional[str] = ..., meta: _Optional[str] = ...) -> None: ...

class TalkResponse(_message.Message):
__slots__ = ("status", "results")
STATUS_FIELD_NUMBER: _ClassVar[int]
RESULTS_FIELD_NUMBER: _ClassVar[int]
status: int
results: _containers.RepeatedCompositeFieldContainer[TalkResult]
def __init__(self, status: _Optional[int] = ..., results: _Optional[_Iterable[_Union[TalkResult, _Mapping]]] = ...) -> None: ...

class TalkResult(_message.Message):
__slots__ = ("id", "type", "kv")
class KvEntry(_message.Message):
__slots__ = ("key", "value")
KEY_FIELD_NUMBER: _ClassVar[int]
VALUE_FIELD_NUMBER: _ClassVar[int]
key: str
value: str
def __init__(self, key: _Optional[str] = ..., value: _Optional[str] = ...) -> None: ...
ID_FIELD_NUMBER: _ClassVar[int]
TYPE_FIELD_NUMBER: _ClassVar[int]
KV_FIELD_NUMBER: _ClassVar[int]
id: int
type: ResultType
kv: _containers.ScalarMap[str, str]
def __init__(self, id: _Optional[int] = ..., type: _Optional[_Union[ResultType, str]] = ..., kv: _Optional[_Mapping[str, str]] = ...) -> None: ...
Loading

0 comments on commit 0cd0c03

Please sign in to comment.