python版gRpc快速上手

2019年1月24日 03:30 by wst

gRPC

本篇文章思路来源官方文档的quickstart, 英文好的朋友可以看官方的原文。

 

安装环境

这里用的python版本为3.6.8,grpc版本为v1.18.0

1升级pip

保证pip的版本为9.0.1或者更高

python -m pip install --upgrade pip

2安装gRPC

# 安装gRPC本身
python -m pip install grpcio
# 安装gRPC相关工具
python -m pip install grpcio-tools

3下载源码

# Clone the repository to get the example code:
git clone -b v1.18.0 https://github.com/grpc/grpc
# Navigate to the "hello, world" Python example:
cd grpc/examples/python/helloworld

4运行gRPC应用

  1. 运行服务端

     

    python greeter_server.py

    另一个窗口的同样目录下,运行客户端

    python greeter_client.py

     

更新gRPC服务

目的是让大家从感性上理解怎么修改服务

  1. 编辑文件 examples/protos/helloworld.proto,并做如下修改(在服务中多了SayHelloAgain函数):

    syntax = "proto3";
    
    option java_multiple_files = true;
    option java_package = "io.grpc.examples.helloworld";
    option java_outer_classname = "HelloWorldProto";
    option objc_class_prefix = "HLW";
    
    package helloworld;
    
    // The greeting service definition.
    service Greeter {
      // Sends a greeting
      rpc SayHello (HelloRequest) returns (HelloReply) {}
      rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
    }
    
    // The request message containing the user's name.
    message HelloRequest {
      string name = 1;
    }
    
    // The response message containing the greetings
    message HelloReply {
      string message = 1;
    }
  2. 保存后运行此文件生成gRPC代码--供服务端和客户端运行的代码

    cd examples/python/helloworld
    python -m grpc_tools.protoc -I../../protos --python_out=. --grpc_python_out=. ../../protos/helloworld.proto

    这个动作将重新生成文件
    helloworld_pb2.py - --包含请求和响应类,helloworld_pb2_grpc.py - --包含客户端和服务端类

更新并运行应用

以下操作所在目录:examples/python/helloworld

  1. 更新服务端(greeter_server.py)代码

    class Greeter(helloworld_pb2_grpc.GreeterServicer):
    
      def SayHello(self, request, context):
        return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)
    
      def SayHelloAgain(self, request, context):
        return helloworld_pb2.HelloReply(message='Hello again, %s!' % request.name)
  2. 更新客户端(greeter_client.py)代码

    def run():
        # NOTE(gRPC Python Team): .close() is possible on a channel and should be
        # used in circumstances in which the with statement does not fit the needs
        # of the code.
        with grpc.insecure_channel('localhost:50051') as channel:
            stub = helloworld_pb2_grpc.GreeterStub(channel)
            response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
            response = stub.SayHelloAgain(helloworld_pb2.HelloRequest(name='you'))
        print("Greeter client received: " + response.message)
    
  3. 重新运行服务端和客户端

    在一个窗口运行: 

    python greeter_server.py

    在另一个窗口运行:

    python greeter_client.py

     

总结

只是一个对grpc的感性认识。如果要了解更多,可参照官方文档:什么是gRPC 、gRPC基础:python版
或者关注后续博文。

 


Comments(128) Add Your Comment

Charlesnag
“I haven’t seen you in these parts,” the barkeep said, sidling over and above to where I sat. “Repute’s Bao.” He stated it exuberantly, as if low-down of his exploits were shared by means of settlers about assorted a firing in Aeternum. He waved to a unanimated butt beside us, and I returned his token with a nod. He filled a glass and slid it to me across the stained red wood of the court prior to continuing. “As a betting chains, I’d be delighted to wager a honourable piece of silver you’re in Ebonscale Reach for the purpose more than the swig and sights,” he said, eyes glancing from the sword sheathed on my with it to the bow slung across my back. http://www.google.gr/url?q=https://renewworld.ru/otlichie-new-world-versii-deluxe-i-standart-edition/
Kevingaw
http://lehrcare.de/pages/pgs/die_besten_casinos_von_las_vegas_in_downtown__fremont_street_.html http://images.google.at/url?q=http://die-fototasse.de/wp-content/plugins/element/welches_online_casino_ist_zu_empfehlen___1.html http://images.google.com.mx/url?q=https://1xbitapk.com/ko/ https://www.google.co.cr/url?sa=t&source=web&cd=11&ved=0CBQQFjAAOAo&url=http://cloudbetwelcomebonus.com/it/ http://www.google.com.py/url?q=http://tanzschule-schrittfest.de/wp-content/pages/welche_online_casinos_sind_in_deutschland_legal__2.html
Hftged
buy atorvastatin 10mg for sale <a href="https://lipiws.top/">lipitor 20mg canada</a> atorvastatin 80mg for sale
Hjxbwc
cipro 1000mg ca - <a href="https://metroagyl.top/">purchase baycip online</a> how to buy clavulanate
Pqepom
buy ciprofloxacin without a prescription - <a href="https://cipropro.top/augmentin500/">augmentin 1000mg tablet</a> augmentin ca
Iumpze
where to buy metronidazole without a prescription - <a href="https://metroagyl.top/azithromycin/">zithromax 250mg uk</a> azithromycin 250mg pills
Dfcmex
cost ciprofloxacin 500 mg - <a href="https://septrim.top/chloromycetin/">order chloramphenicol pill</a> erythromycin medication
Xjnwnt
buy valacyclovir 1000mg pill - <a href="https://gnantiviralp.com/zovirax/">zovirax 800mg tablet</a> buy acyclovir sale
Lrntoa
stromectol order online - <a href="https://keflexin.top/aczone/">buy aczone cheap</a> brand sumycin 250mg
Nhbnqi
flagyl online buy - <a href="https://metroagyl.top/clindamycin/">how to buy clindamycin</a> buy azithromycin 250mg online cheap
Xlpkts
generic acillin <a href="https://ampiacil.top/amoxil500/">amoxil buy online</a> order amoxil online cheap
Nmlsqb
order furosemide 40mg for sale - <a href="https://antipathogenc.com/minipressm/">prazosin buy online</a> order capoten 25mg generic
Ohehrp
purchase metformin pill - <a href="https://gnantiacidity.com/lincocin/">buy lincocin 500 mg</a> lincocin online
Okxrmr
order retrovir pills - <a href="https://canadiangnp.com/epivir/">buy lamivudine 100 mg for sale</a> buy allopurinol 300mg generic
Nqkwmv
clozaril canada - <a href="https://genonlinep.com/altace/">buy ramipril 5mg pills</a> pepcid 40mg sale
Zvvwmc
seroquel 100mg tablet - <a href="https://gnkantdepres.com/geodon/">geodon over the counter</a> buy eskalith for sale
Svdvtx
buy clomipramine 50mg online - <a href="https://antdeponline.com/abilify/">abilify pill</a> purchase doxepin online cheap
Glmeku
order generic atarax 10mg - <a href="https://antdepls.com/prozac/">generic sarafem 40mg</a> amitriptyline online
Ajvyrn
buy amoxil pills for sale - <a href="https://atbioxotc.com/trimox/">buy generic trimox</a> buy ciprofloxacin 500mg sale
Xvsyuo
cost amoxiclav - <a href="https://atbioinfo.com/ampicillin/">ampicillin price</a> cost ciprofloxacin 1000mg
Oqlvpg
buy cleocin 150mg sale - <a href="https://cadbiot.com/oxytetracycline/">terramycin 250 mg price</a> buy chloromycetin tablets
Tklyqt
buy cheap generic zithromax - <a href="https://gncatbp.com/ctinidazole/">order tinidazole 300mg for sale</a> brand ciprofloxacin 500mg
Jxkqqv
albuterol tablet - <a href="https://antxallergic.com/">order generic ventolin 4mg</a> cheap theophylline
Vgvfng
ivermectin 3mg tablets - <a href="https://antibpl.com/">ivermectin 3mg over counter</a> buy generic cefaclor over the counter
Krrjhp
desloratadine 5mg us - <a href="https://rxtallerg.com/ventolinpill/">purchase ventolin without prescription</a> albuterol for sale online
Oocard
buy medrol cheap - <a href="https://ntallegpl.com/fluorometholone/">buy cheap fluorometholone</a> azelastine 10ml sprayers
Fnjamt
buy generic glyburide for sale - <a href="https://prodeprpl.com/">glyburide sale</a> cheap forxiga
Ssjazp
repaglinide 1mg cheap - <a href="https://depressinfo.com/">prandin price</a> order jardiance online
Rtciat
buy glucophage no prescription - <a href="https://arxdepress.com/acarbose50/">buy precose 50mg generic</a> acarbose without prescription
Flxxsx
lamisil 250mg oral - <a href="https://treatfungusx.com/fulvicin250mg/">fulvicin drug</a> cheap grifulvin v
Gwwmft
rybelsus online - <a href="https://infodeppl.com/glucovance5/">purchase glucovance online</a> cheap DDAVP
Kxoxol
generic nizoral 200mg - <a href="https://antifungusrp.com/">purchase nizoral without prescription</a> buy sporanox online
Zhowpt
purchase digoxin pill - <a href="https://blpressureok.com/dipyridamole100/">buy dipyridamole 25mg generic</a> furosemide pill
Hobems
buy famvir 250mg without prescription - <a href="https://amvinherpes.com/">cheap famvir 250mg</a> buy valcivir pills for sale
Ccubwh
microzide 25 mg brand - <a href="https://norvapril.com/amlodipine/">oral norvasc 10mg</a> bisoprolol online
Ctoqzg
order metoprolol 50mg pills - <a href="https://bloodpresspl.com/hyzaar/">losartan 25mg price</a> cost nifedipine
Qvroor
nitroglycerin - <a href="https://nitroproxl.com/">buy nitroglycerin without a prescription</a> valsartan 160mg without prescription
Kukhnw
rosuvastatin comfortable - <a href="https://antcholesterol.com/">rosuvastatin modern</a> caduet buy magical
Hsuqrm
simvastatin underground - <a href="https://canescholest.com/atorvastatin/">atorvastatin thank</a> lipitor dwell
Hvlqtx
buy viagra professional dwell - <a href="https://edsildps.com/levitraoraljelly/">levitra oral jelly online let</a> levitra oral jelly frequent
Vshfjt
dapoxetine peaceful - <a href="https://promedprili.com/cialiswithdapoxetine/">cialis with dapoxetine steam</a> cialis with dapoxetine wrinkle
Uvxynw
cenforce decide - <a href="https://xcenforcem.com/">cenforce sympathy</a> brand viagra online nearby
Iagzev
brand cialis when - <a href="https://probrandtad.com/alprostadilonline/">alprostadil seven</a> penisole permit
Gwajcg
cialis soft tabs pills splash - <a href="https://supervalip.com/viagraoraljelly/">viagra oral jelly online extra</a> viagra oral jelly online stone
Xyiycy
brand cialis thrust - <a href="https://probrandtad.com/apcalissx/">apcalis longer</a> penisole tool
Fznecs
cialis soft tabs pills like - <a href="https://supervalip.com/viagrasuperactive/">viagra super active pills worm</a> viagra oral jelly online house
Xrqzye
cenforce high - <a href="https://xcenforcem.com/brandviagrasildenafil/">brand viagra grief</a> brand viagra pills pant
Sdeodl
dapoxetine century - <a href="https://promedprili.com/suhagra/">suhagra bet</a> cialis with dapoxetine funeral
Lufqii
acne medication worm - <a href="https://placnemedx.com/">acne medication eye</a> acne medication example
Etzxfi
asthma medication nay - <a href="https://bsasthmaps.com/">inhalers for asthma tomorrow</a> asthma treatment fury
Lpqzmv
uti medication magic - <a href="https://amenahealthp.com/">uti medication keep</a> uti medication proof
Hmgyin
pills for treat prostatitis sole - <a href="https://xprosttreat.com/">prostatitis treatment art</a> prostatitis treatment writ
Spirkp
valacyclovir own - <a href="https://gnantiviralp.com/">valtrex pills bat</a> valacyclovir pills seven
Mjtqoh
claritin forth - <a href="https://clatadine.top/">claritin devil</a> claritin pills hundred
Djtkkh
claritin hall - <a href="https://clatadine.top/">loratadine medication war</a> claritin pills endure
Arcfqn
priligy conscious - <a href="https://prilixgn.top/">dapoxetine journal</a> priligy extraordinary
Hyqkij
promethazine national - <a href="https://prohnrg.com/">promethazine shallow</a> promethazine possess
Wyahoy
ascorbic acid kitchen - <a href="https://ascxacid.com/">ascorbic acid north</a> ascorbic acid rest
Kcxbux
florinef pills commit - <a href="https://gastroplusp.com/prlansoprazole/">lansoprazole pills haste</a> prevacid pills hot
Scnmjk
biaxin cell - <a href="https://gastropls.com/mesalaminepl/">mesalamine last</a> cytotec pills rusty
Ejnqxr
bisacodyl 5 mg canada - <a href="https://gastroinfop.com/loperamide/">buy loperamide 2 mg</a> liv52 20mg generic
Jjvwlg
aciphex uk - <a href="https://gastrointesl.com/">buy rabeprazole 10mg generic</a> domperidone pill
Kspbju
buy cotrimoxazole no prescription - <a href="https://ketiracet.com/">buy keppra sale</a> buy tobramycin online cheap
Unphgk
zovirax uk - <a href="https://cerazestrel.com/">order desogestrel 0.075mg generic</a> duphaston 10 mg brand
Igfvql
buy forxiga 10mg generic - <a href="https://precarbos.com/">precose 50mg generic</a> acarbose 50mg cheap
Gzryvw
buy fulvicin paypal - <a href="https://gemfilopi.com/">order lopid 300mg sale</a> brand gemfibrozil
Hkcfep
buy vasotec sale - <a href="https://vasolapril.com/">enalapril for sale</a> xalatan without prescription
Uswubs
order dramamine 50 mg - <a href="https://prasilx.com/">order prasugrel 10mg pills</a> buy risedronate generic
Jljujp
piroxicam 20 mg brand - <a href="https://feldexicam.com/">buy generic feldene 20 mg</a> buy generic exelon
Zkybzu
order monograph 600mg online cheap - <a href="https://etodograph.com/">oral etodolac 600mg</a> buy pletal online
Fwcjkt
piracetam 800 mg usa - <a href="https://nootquin.com/levofloxacin/">levaquin online order</a> order sinemet 20mg pill
Jmxuoy
hydroxyurea medication - <a href="https://hydroydrinfo.com/pentoxifylline/">trental online buy</a> buy generic robaxin 500mg
Ffvaej
divalproex cost - <a href="https://adepamox.com/acetazolamide/">diamox sale</a> order topamax 100mg pills
Zvlvpm
norpace canada - <a href="https://anorpica.com/">order norpace generic</a> order thorazine 100mg generic
Srrqds
cyclophosphamide tablets - <a href="https://cycloxalp.com/">buy generic cyclophosphamide for sale</a> buy trimetazidine
Bytlvv
cost aldactone 100mg - <a href="https://aldantinep.com/">spironolactone 100mg tablet</a> brand revia 50 mg
Syulrw
cyclobenzaprine 15mg canada - <a href="https://abflequine.com/olanzapine/">order zyprexa without prescription</a> order vasotec 5mg generic
Partgp
purchase ondansetron pills - <a href="https://azofarininfo.com/">zofran 4mg usa</a> ropinirole usa
Vqpidz
ascorbic acid 500 mg brand - <a href="https://mdacidinfo.com/isosorbidedinitrate/">order generic isosorbide dinitrate</a> buy generic prochlorperazine
Rpiyzy
durex gel online order - <a href="https://xalaplinfo.com/latanoprosteyedrops/">buy zovirax online cheap</a> order generic latanoprost
Chzszp
order leflunomide without prescription - <a href="https://infohealthybones.com/">purchase leflunomide online</a> buy cheap generic cartidin
Nvakpx
where can i buy minoxidil - <a href="https://hairlossmedinfo.com/finasteridefinp/">order finpecia for sale</a> order propecia 5mg online cheap
Pxjzkk
buy tenormin 50mg pill - <a href="https://heartmedinfox.com/carvedilol/">carvedilol 25mg pill</a> purchase coreg pill
Ufyezu
purchase verapamil pills - <a href="https://infoheartdisea.com/atenolol/">order tenoretic pill</a> tenoretic pills
Nhmcsi
cheap atorlip pill - <a href="https://infoxheartmed.com/">cheap atorvastatin tablets</a> order nebivolol 20mg online
Kfgvfh
lasuna tablets - <a href="https://infoherbalmz.com/">buy lasuna for sale</a> cheap himcolin generic
Binaji
buy generic gasex - <a href="https://herbalinfomez.com/diabecon/">buy diabecon without prescription</a> buy diabecon for sale
Mjxgzh
purchase speman sale - <a href="https://spmensht.com/finasteride/">cheap finasteride pills</a> finasteride order online
Gcmvor
buy norfloxacin generic - <a href="https://gmenshth.com/confido/">buy confido cheap</a> buy confido
Oksyrw
terazosin for sale online - <a href="https://hymenmax.com/dutasteride/">avodart 0.5mg for sale</a> dapoxetine 90mg oral
Dxlfos
purchase oxcarbazepine online - <a href="https://trileoxine.com/levothyroxine/">cheap synthroid for sale</a> cheap synthroid sale
Izyeow
purchase duphalac online cheap - <a href="https://duphalinfo.com/betahistine/">generic betahistine</a> buy betahistine generic
Kqxzsz
purchase cyclosporine generic - <a href="https://asimusxate.com/">buy cyclosporine generic</a> purchase colcrys sale
Dfcvmj
deflazacort online - <a href="https://lazacort.com/brimonidine/">where can i buy alphagan</a> alphagan price
Fxywpq
buy besivance generic - <a href="https://besifxcist.com/sildamax/">buy sildamax without a prescription</a> sildamax where to buy
Ibivht
probalan over the counter - <a href="https://bendoltol.com/carbamazepine/">order generic carbamazepine</a> carbamazepine 200mg pill
Neosat
buy celecoxib sale - <a href="https://celespas.com/indomethacin/">indocin 75mg pill</a> buy indocin online cheap
Qbhnff
mebeverine 135mg tablet - <a href="https://coloxia.com/etoricoxib/">generic arcoxia 60mg</a> cilostazol 100mg generic
Xylusu
rumalaya pills - <a href="https://rumaxtol.com/">rumalaya generic</a> order elavil 50mg generic
Mgweor
order generic mestinon - <a href="https://mestonsx.com/azathioprine/">order azathioprine 50mg sale</a> imuran drug
Lbvrmy
order baclofen sale - <a href="https://baclion.com/nspiroxicam/">buy piroxicam</a> feldene us
Hwnlcj
diclofenac over the counter - <a href="https://vovetosa.com/animodipine/">nimodipine cost</a> nimotop drug
Mrvsdc
purchase cyproheptadine - <a href="https://periheptadn.com/aszanaflex/">buy tizanidine 2mg generic</a> tizanidine online
Seziwi
order mobic 7.5mg for sale - <a href="https://meloxiptan.com/asrizatriptan/">maxalt online</a> purchase toradol sale
Ileact
buy cefdinir 300mg sale - <a href="https://omnixcin.com/clindamycin/">brand clindamycin</a> purchase cleocin online cheap
Qbekqm
buy artane pills for sale - <a href="https://voltapll.com.com/">order artane generic</a> purchase voltaren gel online
Uucrjd
buy accutane 10mg online - <a href="https://aisotane.com/asavlosulfon/">aczone tablet</a> order deltasone 40mg pill
Jbikfu
buy deltasone pills - <a href="https://apreplson.com/">order prednisone online cheap</a> buy zovirax online cheap
Jwdutc
acticin cheap - <a href="https://actizacs.com/">buy acticin cream</a> retin cream generic
Qkkpfe
buy betnovate cream - <a href="https://betnoson.com/awdifferin/">buy cheap differin</a> buy benoquin without prescription
Ufeuhk
flagyl price - <a href="https://ametronles.com/">buy metronidazole pills for sale</a> buy generic cenforce over the counter
Yjjkpg
cheap augmentin 375mg - <a href="https://alevonted.com/">order levothyroxine online</a> levothroid pill
Mjjbfu
buy losartan 25mg pills - <a href="https://acephacrsa.com/">cephalexin order online</a> buy cephalexin online
Mmyefy
buy generic cleocin 300mg - <a href="https://clinycinpl.com/">order cleocin 300mg without prescription</a> buy indomethacin without a prescription
Hvzjix
eurax order - <a href="https://aeuracream.com/abactroban/">bactroban ointment price</a> purchase aczone for sale
Uwhwvl
provigil 200mg without prescription - <a href="https://sleepagil.com/phenergan/">promethazine 25mg generic</a> buy meloset 3 mg pills
Fvnedk
zyban usa - <a href="https://bupropsl.com/">buy zyban 150 mg pill</a> buy generic shuddha guggulu online
Vtgrzj
cost xeloda 500 mg - <a href="https://xelocap.com/danocrine/">buy danocrine pills</a> danocrine uk
Kzfyio
progesterone pill - <a href="https://apromid.com/clomipheneferto/">purchase fertomid online</a> order generic clomiphene
Fwnusb
fosamax 35mg oral - <a href="https://pilaxmax.com/pilex/">cost pilex</a> purchase medroxyprogesterone online
Zknrbl
aygestin 5mg usa - <a href="https://norethgep.com/lumigan/">lumigan generic</a> buy yasmin cheap
Zsrpfm
purchase yasmin - <a href="https://festrolp.com/ginette-35/">buy ginette 35 online cheap</a> buy generic anastrozole 1 mg
Iznher
buy cabergoline pills for sale - <a href="https://adostilin.com/premarin/">order premarin 0.625mg for sale</a> cheap generic alesse
Moaxav
プレドニン 海外通販 - <a href="https://jpaonlinep.com/jazithromycin/">アジスロマイシン通販おすすめ</a> アジスロマイシン通販で買えますか
Lppikq
バイアグラは薬局で買える? - <a href="https://jpedpharm.com/">シルデナフィルの購入</a> シアリス おすすめ
Cyvfdp
プレドニン 値段 - <a href="https://jpanfarap.com/">プレドニン処方</a> イソトレチノイン どこで買える
Yxgdyl
eriacta therefore - <a href="https://eriagra.com/szenegrag/">zenegra online ear</a> forzest damn
Nuwvbn
valif online big - <a href="https://avaltiva.com/tysecnidazolef/">secnidazole cost</a> buy sinemet 10mg pill