forked from Ehco1996/aioshadowsocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrpc_client.py
More file actions
46 lines (34 loc) · 1.18 KB
/
grpc_client.py
File metadata and controls
46 lines (34 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import asyncio
from grpclib.client import Channel
from shadowsocks.protos.aioshadowsocks_pb2 import (
UserIdReq,
UserReq,
User,
HealthCheckReq,
HealthCheckRes,
)
from shadowsocks.protos.aioshadowsocks_grpc import ssStub
class Client:
def __init__(self, loop):
self.channel = Channel("127.0.0.1", 5000, loop=loop)
self.stub = ssStub(self.channel)
async def get_user(self, user_id: int):
user = await self.stub.GetUser(UserIdReq(user_id=1))
print(f"user: {user}")
async def list_user(self, tcp_conn_num):
res = await self.stub.ListUser(UserReq(tcp_conn_num=tcp_conn_num))
print(f"list_user: {res}")
async def healcheck(self, url: str):
res = await self.stub.HealthCheck(HealthCheckReq(url=url))
print(f"health: {res}")
def close(self):
self.channel.close()
if __name__ == "__main__":
loop = asyncio.get_event_loop()
client = Client(loop)
url = "https://www.zhihu.com/"
# job = loop.create_task(client.healcheck(url))
job = loop.create_task(client.get_user(1))
# job = loop.create_task(client.list_user(0))
loop.run_until_complete(job)
client.close()