session在Sanic中的使用

2023年8月24日 15:30 by wst

web开发

使用过flask中的session后,感觉还是挺方便的,能够保存用户的状态,又不用关心它是怎么实现的。

那么在sanic中是否有类似的机制呢?经过查找,发现了sanic_session.

在内存中存储数据

安装方式:

pip install sanic_session

使用示例:

from sanic import Sanic
from sanic.response import text
from sanic_session import Session

app = Sanic()
Session(app)

@app.route("/")
async def index(request):
    # interact with the session like a normal dict
    if not request.ctx.session.get('foo'):
        request.ctx.session['foo'] = 0

    request.ctx.session['foo'] += 1

    return text(str(request.ctx.session['foo']))

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000, debug=True)

现象:

每请求一次,得到的数字都会加1.

在redis中存储数据

另外再安装依赖包:

pip install asyncio_redis

使用示例:

import asyncio_redis

from sanic import Sanic
from sanic.response import text
from sanic_session import Session, RedisSessionInterface

app = Sanic()


class Redis:
    """
    A simple wrapper class that allows you to share a connection
    pool across your application.
    """
    _pool = None

    async def get_redis_pool(self):
        if not self._pool:
            self._pool = await asyncio_redis.Pool.create(
                host='localhost', port=6379, poolsize=10
            )

        return self._pool


redis = Redis()

Session(app, interface=RedisSessionInterface(redis.get_redis_pool))


@app.route("/")
async def test(request):
    # interact with the session like a normal dict
    if not request.ctx.session.get('foo'):
        request.ctx.session['foo'] = 0

    request.ctx.session['foo'] += 1

    response = text(str(request.ctx.session['foo']))

    return response

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000, debug=True)

 


Comments(127) Add Your Comment

Alwqax
where to buy lipitor without a prescription <a href="https://lipiws.top/">order lipitor 10mg pill</a> buy lipitor sale
Gtobtl
brand ciprofloxacin 1000mg - <a href="https://cipropro.top/bactrimos/">cost trimethoprim</a> augmentin 1000mg without prescription
Yftewg
baycip online order - <a href="https://metroagyl.top/clindamycin/">trimethoprim without prescription</a> amoxiclav price
Ybfooq
buy ciplox 500 mg - <a href="https://septrim.top/tinidazole/">tindamax pill</a> erythromycin 500mg tablet
Snlqfz
flagyl ca - <a href="https://metroagyl.top/keflexcapsule/">purchase amoxicillin sale</a> azithromycin cheap
Pnzvvw
stromectol buy - <a href="https://keflexin.top/cefixime/">buy cefixime 100mg online cheap</a> sumycin brand
Ptqdzp
valacyclovir 500mg brand - <a href="https://gnantiviralp.com/nemasole/">cheap nemasole online</a> buy acyclovir 400mg pills
Mshahu
ampicillin online buy <a href="https://ampiacil.top/amoxil500/">cheap amoxil for sale</a> cheap amoxil generic
Mylfan
buy flagyl 200mg - <a href="https://metroagyl.top/keflexcapsule/">order amoxicillin pills</a> zithromax 500mg over the counter
Ntttef
lasix usa - <a href="https://antipathogenc.com/capoten/">order captopril 25 mg generic</a> buy captopril without prescription
Gtmqxu
purchase metformin - <a href="https://gnantiacidity.com/baycip/">where can i buy cipro</a> oral lincocin 500mg
Dcmzwu
retrovir 300 mg pills - <a href="https://canadiangnp.com/">metformin usa</a> zyloprim 300mg uk
Frgtrv
buy clozaril 100mg sale - <a href="https://genonlinep.com/">order clozaril 100mg for sale</a> where to buy pepcid without a prescription
Gierap
quetiapine 100mg over the counter - <a href="https://gnkantdepres.com/geodon/">brand ziprasidone</a> eskalith cost
Qncfic
order anafranil 50mg pills - <a href="https://antdeponline.com/">order anafranil 25mg for sale</a> how to get doxepin without a prescription
Lfoset
order atarax 10mg for sale - <a href="https://antdepls.com/endep/">endep for sale</a> order amitriptyline 10mg pill
Xjcadk
buy generic augmentin 625mg - <a href="https://atbioinfo.com/ampicillin/">ampicillin price</a> cipro online
Vtkuww
buy amoxicillin for sale - <a href="https://atbioxotc.com/">order amoxil sale</a> order generic ciprofloxacin 500mg
Sgtrto
order azithromycin 500mg online - <a href="https://gncatbp.com/btetracycline/">purchase sumycin pills</a> order ciplox 500mg sale
Dcgtph
oral cleocin 150mg - <a href="https://cadbiot.com/doxycycline/">acticlate uk</a> cheap chloramphenicol generic
Tazzyk
ivermectin 12 mg over counter - <a href="https://antibpl.com/">stromectol human</a> cefaclor 500mg brand
Ucrgin
order albuterol inhalator sale - <a href="https://antxallergic.com/cvallegra/">order allegra online</a> theophylline pill
Wzpquz
how to get depo-medrol without a prescription - <a href="https://ntallegpl.com/loratadine/">claritin medication</a> buy generic azelastine for sale
Wsyhxr
clarinex 5mg uk - <a href="https://rxtallerg.com/flixotidespray/">purchase flixotide generic</a> buy generic ventolin over the counter
Tvpndr
where to buy micronase without a prescription - <a href="https://prodeprpl.com/">buy glyburide for sale</a> forxiga 10mg cost
Qersra
order metformin online cheap - <a href="https://arxdepress.com/">order generic metformin 1000mg</a> precose 50mg cost
Sfkjwj
buy prandin 2mg generic - <a href="https://depressinfo.com/jardiance25/">empagliflozin tablet</a> empagliflozin sale
Rixcfi
cost semaglutide 14mg - <a href="https://infodeppl.com/ddavpspray/">desmopressin us</a> desmopressin medication
Mfllsn
order terbinafine 250mg - <a href="https://treatfungusx.com/xgrifulvinvs/">grifulvin v price</a> grifulvin v online
Zupxit
oral nizoral 200mg - <a href="https://antifungusrp.com/clotrithasone/">purchase lotrisone generic</a> buy sporanox generic
Nhdnww
famvir without prescription - <a href="https://amvinherpes.com/gnvalaciclovir/">brand valaciclovir 500mg</a> buy valaciclovir 500mg online cheap
Dwzpnp
lanoxin over the counter - <a href="https://blpressureok.com/">digoxin 250mg cost</a> furosemide where to buy
Vdyenx
metoprolol pills - <a href="https://bloodpresspl.com/">buy lopressor 100mg pill</a> adalat buy online
Dlzmmq
order microzide sale - <a href="https://norvapril.com/lisinopril/">order zestril 10mg pills</a> buy zebeta without a prescription
Sxpxdi
purchase nitroglycerin online - <a href="https://nitroproxl.com/">nitroglycerin online buy</a> valsartan 160mg ca
Yolbnu
zocor graceful - <a href="https://canescholest.com/">simvastatin anyway</a> atorvastatin show
Nkblpi
rosuvastatin pills marriage - <a href="https://antcholesterol.com/">rosuvastatin online patch</a> caduet fierce
Lyizvy
viagra professional salt - <a href="https://edsildps.com/superkamagra/">super kamagra now</a> levitra oral jelly online fragment
Zwworf
priligy speak - <a href="https://promedprili.com/suhagra/">suhagra consent</a> cialis with dapoxetine until
Zxswki
cenforce parlour - <a href="https://xcenforcem.com/cialistadalafil/">cialis for men</a> brand viagra online member
Taqfpf
brand cialis easy - <a href="https://probrandtad.com/penisole/">penisole limp</a> penisole grab
Bczfee
brand cialis slave - <a href="https://probrandtad.com/zhewitra/">zhewitra crowd</a> penisole burst
Zkiydh
cialis soft tabs online per - <a href="https://supervalip.com/tadarisepills/">tadarise let</a> viagra oral jelly online cloth
Smrpua
cialis soft tabs tremble - <a href="https://supervalip.com/valifonline/">valif oxford</a> viagra oral jelly rat
Vtdcif
priligy flicker - <a href="https://promedprili.com/zudena/">zudena satisfy</a> cialis with dapoxetine sometime
Zmlnnu
cenforce somewhere - <a href="https://xcenforcem.com/tadaciptadalafil/">tadacip drain</a> brand viagra distress
Mnnfvs
asthma treatment circle - <a href="https://bsasthmaps.com/">asthma treatment slave</a> asthma treatment limp
Yuwsag
acne treatment bald - <a href="https://placnemedx.com/">acne treatment herself</a> acne treatment dusk
Akbjre
prostatitis pills grab - <a href="https://xprosttreat.com/">prostatitis pills nearest</a> prostatitis medications sand
Lwhjnw
treatment for uti dreadful - <a href="https://amenahealthp.com/">uti treatment afterward</a> treatment for uti language
Ikozwf
claritin earn - <a href="https://clatadine.top/">loratadine firebolt</a> claritin heel
Cijwor
valacyclovir pills waist - <a href="https://gnantiviralp.com/">valtrex pills kneel</a> valacyclovir online slight
Vmtlln
priligy then - <a href="https://prilixgn.top/">dapoxetine sport</a> dapoxetine chant
Zencdu
claritin pills overcome - <a href="https://clatadine.top/">claritin pills steer</a> claritin pills gloom
Ocxyyh
ascorbic acid bottle - <a href="https://ascxacid.com/">ascorbic acid shriek</a> ascorbic acid grass
Wdtmri
promethazine basket - <a href="https://prohnrg.com/">promethazine weave</a> promethazine except
Zsdbew
biaxin pills relieve - <a href="https://gastropls.com/">clarithromycin pills enjoy</a> cytotec fred
Txhyyt
florinef course - <a href="https://gastroplusp.com/prlansoprazole/">prevacid neither</a> lansoprazole pills decide
Gkfxmj
buy rabeprazole 20mg pills - <a href="https://gastrointesl.com/metoclopramide/">buy maxolon tablets</a> motilium 10mg oral
Ghpfbp
dulcolax tablet - <a href="https://gastroinfop.com/loperamide/">loperamide online order</a> buy liv52 medication
Dfavqu
purchase zovirax online cheap - <a href="https://danaterone.shop/">buy eukroma cream</a> order dydrogesterone
Nrrdwf
cotrimoxazole 480mg oral - <a href="https://ketiracet.com/">generic keppra</a> tobramycin 10mg sale
Jggbpd
griseofulvin 250 mg sale - <a href="https://dipyrilx.com/">buy dipyridamole no prescription</a> gemfibrozil canada
Bufwmj
order forxiga 10 mg for sale - <a href="https://precarbos.com/">order precose 25mg pills</a> buy precose no prescription
Tipbhh
buy dramamine pills for sale - <a href="https://actodronate.com/">risedronate order</a> cost actonel 35mg
Swolua
enalapril 10mg without prescription - <a href="https://xalanoprost.com/">order generic latanoprost</a> latanoprost over the counter
Apopnh
etodolac 600mg pills - <a href="https://etodograph.com/">order etodolac 600mg generic</a> order cilostazol 100 mg for sale
Unbtow
order feldene 20mg online - <a href="https://rivastilons.com/">cheap rivastigmine</a> buy generic exelon 3mg
Zhewpp
oral hydrea - <a href="https://hydroydrinfo.com/pentoxifylline/">pentoxifylline price</a> robaxin 500mg price
Xtqcfo
how to buy depakote - <a href="https://adepamox.com/aspirindipyridamole/">order aggrenox without prescription</a> topiramate tablet
Rsozag
buy disopyramide phosphate tablets - <a href="https://anorpica.com/chlorpromazine/">purchase chlorpromazine online cheap</a> cost thorazine 50 mg
Hcdlnz
cheap spironolactone 25mg - <a href="https://aldantinep.com/">purchase spironolactone generic</a> revia 50 mg canada
Secvkc
buy generic cytoxan for sale - <a href="https://cycloxalp.com/atomoxetine/">buy atomoxetine generic</a> cheap trimetazidine tablets
Zfmaso
oral cyclobenzaprine - <a href="https://abflequine.com/enalapril/">enalapril for sale online</a> order vasotec 10mg generic
Sivxro
zofran 8mg without prescription - <a href="https://azofarininfo.com/tolterodine/">buy generic tolterodine 2mg</a> requip 1mg ca
Gffvbc
buy ascorbic acid 500 mg for sale - <a href="https://mdacidinfo.com/ciprofloxacin/">buy generic ciprofloxacin</a> buy prochlorperazine pill
Nmhdue
durex gel order online - <a href="https://xalaplinfo.com/">durex gel where to purchase</a> order latanoprost generic
Sjfvuh
rogaine for sale - <a href="https://hairlossmedinfo.com/">buy rogaine generic</a> order finasteride 5mg online
Iiqdtb
buy leflunomide medication - <a href="https://infohealthybones.com/calciumcarbonate/">buy calcium carbonate pills</a> cartidin buy online
Amctsk
calan 240mg pills - <a href="https://infoheartdisea.com/atenolol/">buy generic tenoretic for sale</a> tenoretic brand
Iihjzv
order atenolol sale - <a href="https://heartmedinfox.com/clopidogrel/">buy clopidogrel 150mg</a> buy coreg 6.25mg online
Ekvxqp
atorlip buy online - <a href="https://infoxheartmed.com/lisinopril/">buy lisinopril generic</a> cheap nebivolol 5mg
Rrftgu
gasex sale - <a href="https://herbalinfomez.com/diabecon/">cheap diabecon pill</a> order diabecon online cheap
Xxytrv
buy lasuna sale - <a href="https://infoherbalmz.com/himcolin/">himcolin drug</a> buy generic himcolin
Ddwnru
buy norfloxacin generic - <a href="https://gmenshth.com/">cheap norfloxacin pill</a> confido pills
Jjbroh
cheap generic finax - <a href="https://finmenura.com/kamagra/">kamagra 200mg without prescription</a> uroxatral order
Huxeep
speman for sale online - <a href="https://spmensht.com/himplasia/">himplasia where to buy</a> purchase fincar sale
Pkcvnd
hytrin canada - <a href="https://hymenmax.com/">buy generic hytrin for sale</a> buy dapoxetine medication
Picxcn
order oxcarbazepine for sale - <a href="https://trileoxine.com/">buy cheap generic trileptal</a> purchase levothyroxine online
Vpqmac
how to get cyclosporine without a prescription - <a href="https://asimusxate.com/methotrexate/">methotrexate oral</a> colcrys generic
Ukctca
duphalac us - <a href="https://duphalinfo.com/">purchase duphalac bottles</a> betahistine 16 mg for sale
Yeqwkn
calcort buy online - <a href="https://lazacort.com/brimonidine/">how to get alphagan without a prescription</a> buy generic brimonidine
Tmcgys
besivance brand - <a href="https://besifxcist.com/carbocysteine/">where can i buy carbocisteine</a> cheap sildamax generic
Armtax
cheap neurontin - <a href="https://aneutrin.com/ibuprofen/">buy nurofen</a> sulfasalazine 500 mg uk
Tmbkkw
order probalan sale - <a href="https://bendoltol.com/carbamazepine/">buy carbamazepine tablets</a> cost carbamazepine 200mg
Tbqekw
purchase celecoxib online cheap - <a href="https://celespas.com/flavoxate/">flavoxate online buy</a> indocin 50mg generic
Wiefyk
buy colospa 135 mg online - <a href="https://coloxia.com/etoricoxib/">etoricoxib 60mg price</a> order pletal 100 mg generic
Kuhcaj
purchase cambia generic - <a href="https://dicloltarin.com/aspirin/">aspirin 75 mg drug</a> aspirin for sale
Hcbauo
order rumalaya - <a href="https://rumaxtol.com/">buy rumalaya sale</a> buy generic endep 10mg
Zfydgg
pyridostigmine generic - <a href="https://mestonsx.com/asumatriptan/">order imitrex for sale</a> azathioprine over the counter
Wkzkry
order voveran online - <a href="https://vovetosa.com/">purchase diclofenac online</a> purchase nimodipine
Zelrak
baclofen cost - <a href="https://baclion.com/">order baclofen 25mg for sale</a> order feldene online cheap
Arzzsy
cyproheptadine pills - <a href="https://periheptadn.com/">cost periactin 4 mg</a> cost zanaflex
Gkmczb
how to buy mobic - <a href="https://meloxiptan.com/asrizatriptan/">buy maxalt no prescription</a> ketorolac without prescription
Ilfwuq
buy trihexyphenidyl without a prescription - <a href="https://voltapll.com.com/">order trihexyphenidyl sale</a> purchase diclofenac gel online
Igbxea
oral cefdinir 300 mg - <a href="https://omnixcin.com/">brand omnicef</a> clindamycin price
Cipyyg
purchase isotretinoin pill - <a href="https://aisotane.com/asavlosulfon/">dapsone 100mg for sale</a> buy deltasone 10mg generic
Gvbinu
buy prednisone - <a href="https://apreplson.com/">deltasone 10mg canada</a> buy cheap generic permethrin
Rzxslr
buy acticin generic - <a href="https://actizacs.com/">buy acticin sale</a> buy generic retin
Mqylzn
order betamethasone - <a href="https://betnoson.com/benoquincre/">monobenzone sale</a> monobenzone without prescription
Fymdzf
order metronidazole 400mg without prescription - <a href="https://ametronles.com/">metronidazole 400mg canada</a> cenforce 50mg us
Gdgrte
augmentin without prescription - <a href="https://alevonted.com/">synthroid 100mcg tablet</a> levoxyl tablets
Dwyxsy
brand cleocin - <a href="https://indometnp.com/">cheap indomethacin 50mg</a> buy indomethacin online cheap
Hfiurt
buy losartan 50mg online cheap - <a href="https://acephacrsa.com/">keflex over the counter</a> cost cephalexin 250mg
Viutlk
where can i buy crotamiton - <a href="https://aeuracream.com/caczone/">buy aczone without a prescription</a> aczone online order
Gxcbay
buy cheap generic provigil - <a href="https://sleepagil.com/phenergan/">buy promethazine generic</a> cost melatonin
Invtmc
order zyban without prescription - <a href="https://bupropsl.com/">brand bupropion 150mg</a> where can i buy shuddha guggulu
Nttqzd
order capecitabine online - <a href="https://xelocap.com/mefenamicacid/">buy mefenamic acid without a prescription</a> buy danocrine pills
Ttbiog
order generic prometrium 100mg - <a href="https://apromid.com/clomiphene/">clomiphene 50mg generic</a> oral fertomid
Cfgeug
order fosamax 35mg for sale - <a href="https://pilaxmax.com/medroxyprogesterone/">medroxyprogesterone 5mg pill</a> medroxyprogesterone 5mg sale
Wsaxbu
buy aygestin 5 mg online cheap - <a href="https://norethgep.com/yasmin/">oral yasmin</a> yasmin online buy
Cycalm
estrace 1mg us - <a href="https://festrolp.com/">cheap estradiol 2mg</a> buy arimidex 1mg generic
Khdjsf
order cabergoline 0.5mg sale - <a href="https://adostilin.com/premarin/">buy premarin generic</a> where to buy alesse without a prescription
Kxqsfg
プレドニン通販おすすめ - <a href="https://jpaonlinep.com/jamoxicillin/">アモキシシリンジェネリック 通販</a> アジスロマイシン処方
Esunmi
シルデナフィル 海外通販 - <a href="https://jpedpharm.com/">シルデナフィルの購入</a> 正規品シアリス錠の正しい処方
Puesnk
プレドニン通販 - <a href="https://jpanfarap.com/jpadoxycycline/">ドキシサイクリン通販 安全</a> アキュテイン 値段
Eeeuna
eriacta thought - <a href="https://eriagra.com/szenegrag/">zenegra online heal</a> forzest throw