# gRPC - Python Server 만들기

## 🔗  Link

* gRPC 공식 문서 (python): <https://grpc.io/docs/quickstart/python/>
* gRPCio Github: <https://github.com/grpc/grpc/tree/master/src/python/grpcio>
* gRPCio pypi: <https://pypi.org/project/grpcio/>

## Step

1\) pf 정보 가져오는 서비스를 python grpc 서버로 wrapping (이때 proto buf 정의)

## Pre

```bash
$ pyenv versions
$ pyenv 3.7.0
$ source env/bin/activate
$ pip install --upgrade pip
```

### Installation

```bash
$ python -m pip install grpcio
$ pip install grpcio-tools
```

### Clone Example App

```bash
$ git clone -b v1.22.0 https://github.com/grpc/grpc
$ cd grpc/examples/python/helloworld
```

### Run a gRPC App

```bash
$ python greeter_server.py

## 다른 탭에서
$ python greeter_client.py
```

### Demo code

```
// pfservice.proto

syntax = "proto3";

option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";



// helloworld.poroto 복사해왔고
packagep lusfriend.service;


//
service PlusfriendService {
  // Sends a greeting
  rpc GetProfiles (ReqProfiles) returns (ResProfiles) {}
}


message Profile {
    uint32 id = 1;
    uint32 friend_count = 2;
}

// 프로필 조회하는 request 라서, id 리스트를 받을것이므로 Profile 메세지를 새로만들고 그것을 여러개 보내기로.
message ReqProfiles {
  repeated string ids = 1;
}

// The response message containing the greetings
message ResProfiles {
  repeated Profile profiles = 1;
}

```

### Generate gRPC code

```
$ python -m grpc_tools.protoc -I../../protos --python_out=. --grpc_python_out=. ../../protos/helloworld.proto
```

* grpc client 가 있어서 curl 처럼 테스트해볼 수 있음. (-> 에디 참고)
  * zshrc 에 #grpc\_tools ? 머 그런거 있음/
* reflection 무엇

\----

gRPC 의 http request 클라이언트 찾아보기

* aiohttp 등을 사용해야하지만,
* 일단은 블록킹 되더라도 일반 리퀘스트로 만들어보자.

```
$ pip install requests
```

/server.py

```
ids = ','.join(request.ids)

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://haleyryu.gitbook.io/engineer/service-mesh/grpc-python-server.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
