Re: #cherrypy #tornado #sqlalchemy
Sviatoslav Sydorenko <[email protected]> Sun, 6 Jan 2019 15:28:08 +0100
| Newsgroups | gmane.comp.python.cherrypy |
|---|---|
| Message-ID | <CAFYONRAQQ37CivD7YCwMCqfBnMS-kF9LWdjf917WUW82bT2swg@mail.gmail.com> |
--000000000000773e6e057ecaec1e Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable You shouldn't be mixing several web frameworks (especially not sync + async). I think you need to learn more about how the web works now (HTTP, html, templating, etc.) Regarding connecting SA to CherryPy, I've already sent you a repo with such app example. Essential parts are db plugin ( https://github.com/Lawouach/Twiseless/blob/master/lib/plugin/db.py), db tool (https://github.com/Lawouach/Twiseless/blob/master/lib/tool/db.py) and models package ( https://github.com/Lawouach/Twiseless/tree/master/lib/model). Other parts are important too, like this user tool https://github.com/Lawouach/Twiseless/blob/master/lib/tool/user.py#L30-L32 which retrieves a user from db shielding handlers from unauthorized access. You have to learn the concepts of your chosen framework and then learn how to operate them. For CherryPy the process is roughly: 1. Have HTTP handlers which are bound to URLs. CherryPy will then call them whenever incoming HTTP request matches URLs. 2. HTTP handlers should return HTML (in common case). Since you'll probably want to render it from templates instead of hardcoded strings you'll probably need to learn Jinja2 or similar. 3. Since you want to access DB you'll need to use a DB driver or an ORM (SQLAlchemy in your case). You'll want to separate it's initialization code and put it into the plugin which will be your interface to the DB and will provide you a session object to work with it. Plugins are run in separate threads and are accessible via a publish-subscribe bus. 4. To make accessing DB session easier you add a db tool which binds the session to current request object so that it's accessible through something like `cherrypy.request.db`. Tools are callables executed at certain points of request-response cycle, before and after request handler is called. 5. With those things above in place it's possible to access a db from handlers (IRL you might want to separate the logic part into its own entity as well). =D0=BD=D0=B4, 6 =D1=81=D1=96=D1=87. 2019 =D0=BE 14:51 vahid peivandy <tarfa= [email protected]> =D0=BF=D0=B8=D1=88=D0=B5: > yes I write this tornado: https://paste.ubuntu.com/p/3kcj6ZRgFQ/ > and now wanna connect this code: https://paste.ubuntu.com/p/BzmBTnrN54/ > to tornado or cherrypy > > On Sat, Jan 5, 2019 at 4:43 AM Tim Roberts <[email protected]> wrote: > >> vahid peivandy wrote: >> >> I cannot connect mu db on web server tornado or web server cherrypy this >> is my problem and see this page but not help me >> https://tornado-sqlalchemy.readthedocs.io/en/latest/ >> >> Have you ever done a web application of any kind before? To me, it look= s >> like you are trying to jump straight to the final steps of building a we= b >> app, when you haven't taken any of the early steps yet. Don't take on t= oo >> many new things at once. Can you make a basic Tornado or CherryPy >> application work? You start by displaying the basic "hello world" page. >> Then you add a couple of very basic pages with buttons or links to flip >> back and forth between them. Then you substitute in some of your own te= xt, >> to prove you understand the templating scheme. At that point, you'll be >> more familiar with the framework, and the explanation on how to integrat= e >> SQLAlchemy will make more sense. >> >> No one here is going to write your application for you. We can offer >> guidelines, and we can help you work through the problems you encounter, >> but you have to do the work. >> >> -- >> Tim Roberts, [email protected] >> Providenza & Boekelheide, Inc. >> >> -- >> You received this message because you are subscribed to the Google Group= s >> "cherrypy-users" group. >> To unsubscribe from this group and stop receiving emails from it, send a= n >> email to cherrypy-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected] >> To post to this group, send email to cherrypy-users-/JYPxA39Uh5TLH3MbocFF+G/[email protected] >> Visit this group at https://groups.google.com/group/cherrypy-users. >> For more options, visit https://groups.google.com/d/optout. >> > -- > You received this message because you are subscribed to the Google Groups > "cherrypy-users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to cherrypy-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected] > To post to this group, send email to cherrypy-users-/JYPxA39Uh5TLH3MbocFF+G/[email protected] > Visit this group at https://groups.google.com/group/cherrypy-users. > For more options, visit https://groups.google.com/d/optout. > --=20 Cheers, Sviatoslav. --=20 You received this message because you are subscribed to the Google Groups "= cherrypy-users" group. To unsubscribe from this group and stop receiving emails from it, send an e= mail to cherrypy-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected] To post to this group, send email to cherrypy-users-/JYPxA39Uh5TLH3MbocFF+G/[email protected] Visit this group at https://groups.google.com/group/cherrypy-users. For more options, visit https://groups.google.com/d/optout. --000000000000773e6e057ecaec1e Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div di= r=3D"ltr"><div dir=3D"ltr">You shouldn't be mixing several web framewor= ks (especially not sync + async). I think you need to learn more about how = the web works now (HTTP, html, templating, etc.)<div><br></div><div>Regardi= ng connecting SA to CherryPy, I've already sent you a repo with such ap= p example. Essential parts are db=C2=A0plugin (<a href=3D"https://github.co= m/Lawouach/Twiseless/blob/master/lib/plugin/db.py">https://github.com/Lawou= ach/Twiseless/blob/master/lib/plugin/db.py</a>), db=C2=A0tool (<a href=3D"h= ttps://github.com/Lawouach/Twiseless/blob/master/lib/tool/db.py">https://gi= thub.com/Lawouach/Twiseless/blob/master/lib/tool/db.py</a>) and models pack= age (<a href=3D"https://github.com/Lawouach/Twiseless/tree/master/lib/model= ">https://github.com/Lawouach/Twiseless/tree/master/lib/model</a>).</div><d= iv><br></div><div>Other parts are important too, like this user tool=C2=A0<= a href=3D"https://github.com/Lawouach/Twiseless/blob/master/lib/tool/user.p= y#L30-L32">https://github.com/Lawouach/Twiseless/blob/master/lib/tool/user.= py#L30-L32</a> which retrieves a user from db=C2=A0shielding handlers from = unauthorized access.</div><div><br></div><div>You have to learn the concept= s of your chosen framework and then learn how to operate them.</div><div><b= r></div><div>For CherryPy the process is roughly:</div><div>1. Have HTTP ha= ndlers which are bound to URLs. CherryPy will then call them whenever incom= ing HTTP request matches URLs.</div><div>2. HTTP handlers should return HTM= L (in common case). Since you'll probably want to render it from templa= tes instead of hardcoded strings you'll probably need to learn Jinja2 o= r similar.</div><div>3. Since you want to access DB you'll need to use = a DB driver or an ORM (SQLAlchemy in your case). You'll want to separat= e it's initialization code and put it into the plugin which will be you= r interface to the DB and will provide you a session object to work with it= . Plugins are run in separate threads and are accessible via a publish-subs= cribe bus.</div><div>4. To make accessing DB session easier you add a db to= ol which binds the session to current request object so that it's acces= sible through something like `cherrypy.request.db`. Tools are callables exe= cuted at certain points of request-response cycle, before and after request= handler is called.</div><div>5. With those things above in place it's = possible to access a=C2=A0db=C2=A0from handlers (IRL you might want to sepa= rate the logic part into its own entity as well).</div></div></div></div></= div><img src=3D"https://t.sidekickopen76.com/s1t/o/5/f18dQhb0S7ks8dDMPbW2n0= x6l2B9gXrN7sKj6v5dpzzVdDPRz2m64_PW3xgp7472TP1pf2mbxc201?si=3D51806450643107= 84&pi=3D86d75c76-a919-42d8-f980-c8e766b07dcc&ti=3Dnull" style=3D"di= splay:none!important" height=3D"1" width=3D"1"></div><br><div class=3D"gmai= l_quote"><div dir=3D"ltr">=D0=BD=D0=B4, 6 =D1=81=D1=96=D1=87. 2019 =D0=BE 1= 4:51 vahid peivandy <<a href=3D"mailto:[email protected]">tarfand.= [email protected]</a>> =D0=BF=D0=B8=D1=88=D0=B5:<br></div><blockquote clas= s=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid r= gb(204,204,204);padding-left:1ex"><div dir=3D"ltr"><div dir=3D"ltr"><div di= r=3D"ltr"><div style=3D"font-family:"comic sans ms",sans-serif">y= es I write this tornado: <a href=3D"https://paste.ubuntu.com/p/3kcj6ZRgFQ/"= target=3D"_blank">https://paste.ubuntu.com/p/3kcj6ZRgFQ/</a><br></div><div= style=3D"font-family:"comic sans ms",sans-serif">and now wanna c= onnect this code: <a href=3D"https://paste.ubuntu.com/p/BzmBTnrN54/" target= =3D"_blank">https://paste.ubuntu.com/p/BzmBTnrN54/</a>=C2=A0=C2=A0=C2=A0=C2= =A0 to tornado or cherrypy<br></div></div></div></div><br><div class=3D"gma= il_quote"><div dir=3D"ltr">On Sat, Jan 5, 2019 at 4:43 AM Tim Roberts <<= a href=3D"mailto:[email protected]" target=3D"_blank">[email protected]</a>> w= rote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0p= x 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> =20 =20 =20 <div bgcolor=3D"#FFFFFF"> <div class=3D"gmail-m_-8187802126341545521gmail-m_-7333287049090533640m= oz-cite-prefix">vahid peivandy wrote:<br> </div> <blockquote type=3D"cite"> =20 <div dir=3D"ltr"> <div dir=3D"ltr"> <div style=3D"font-family:"comic sans ms",sans-serif">I= cannot connect mu db on web server tornado or web server cherrypy this is my problem and see this page but not help me <br> </div> <div style=3D"font-family:"comic sans ms",sans-serif"><= a href=3D"https://tornado-sqlalchemy.readthedocs.io/en/latest/" target=3D"_= blank">https://tornado-sqlalchemy.readthedocs.io/en/latest/</a><br> </div> </div> </div> </blockquote> <p>Have you ever done a web application of any kind before?=C2=A0 To me= , it looks like you are trying to jump straight to the final steps of building a web app, when you haven't taken any of the early steps yet.=C2=A0 Don't take on too many new things at once.=C2=A0= Can you make a basic Tornado or CherryPy application work?=C2=A0 You start by displaying the basic "hello world" page.=C2=A0 Then you add= a couple of very basic pages with buttons or links to flip back and forth between them.=C2=A0 Then you substitute in some of your own text, to prove you understand the templating scheme.=C2=A0 At that point, you&= #39;ll be more familiar with the framework, and the explanation on how to integrate SQLAlchemy will make more sense. <br> </p> <p>No one here is going to write your application for you.=C2=A0 We can offer guidelines, and we can help you work through the problems you encounter, but you have to do the work.<br> </p> <pre class=3D"gmail-m_-8187802126341545521gmail-m_-7333287049090533640m= oz-signature" cols=3D"72">--=20 Tim Roberts, <a class=3D"gmail-m_-8187802126341545521gmail-m_-7333287049090= 533640moz-txt-link-abbreviated" href=3D"mailto:[email protected]" target=3D"_b= lank">[email protected]</a> Providenza & Boekelheide, Inc. </pre> </div> <p></p> -- <br> You received this message because you are subscribed to the Google Groups &= quot;cherrypy-users" group.<br> To unsubscribe from this group and stop receiving emails from it, send an e= mail to <a href=3D"mailto:cherrypy-users+unsubscribe-/[email protected]" targ= et=3D"_blank">cherrypy-users+unsubscribe-/[email protected]</a>.<br> To post to this group, send email to <a href=3D"mailto:cherrypy-users@googl= egroups.com" target=3D"_blank">cherrypy-users-/[email protected]</a>.<br> Visit this group at <a href=3D"https://groups.google.com/group/cherrypy-use= rs" target=3D"_blank">https://groups.google.com/group/cherrypy-users</a>.<b= r> For more options, visit <a href=3D"https://groups.google.com/d/optout" targ= et=3D"_blank">https://groups.google.com/d/optout</a>.<br> </blockquote></div> <p></p> -- <br> You received this message because you are subscribed to the Google Groups &= quot;cherrypy-users" group.<br> To unsubscribe from this group and stop receiving emails from it, send an e= mail to <a href=3D"mailto:cherrypy-users+unsubscribe-/[email protected]" targ= et=3D"_blank">cherrypy-users+unsubscribe-/[email protected]</a>.<br> To post to this group, send email to <a href=3D"mailto:cherrypy-users@googl= egroups.com" target=3D"_blank">cherrypy-users-/[email protected]</a>.<br> Visit this group at <a href=3D"https://groups.google.com/group/cherrypy-use= rs" target=3D"_blank">https://groups.google.com/group/cherrypy-users</a>.<b= r> For more options, visit <a href=3D"https://groups.google.com/d/optout" targ= et=3D"_blank">https://groups.google.com/d/optout</a>.<br> </blockquote></div><br clear=3D"all"><div><br></div>-- <br><div dir=3D"ltr"= class=3D"gmail_signature"><div dir=3D"ltr">Cheers,<div>Sviatoslav.</div></= div></div></div> <p></p> -- <br /> You received this message because you are subscribed to the Google Groups &= quot;cherrypy-users" group.<br /> To unsubscribe from this group and stop receiving emails from it, send an e= mail to <a href=3D"mailto:cherrypy-users+unsubscribe-/[email protected]">cher= rypy-users+unsubscribe-/[email protected]</a>.<br /> To post to this group, send email to <a href=3D"mailto:cherrypy-users@googl= egroups.com">cherrypy-users-/[email protected]</a>.<br /> Visit this group at <a href=3D"https://groups.google.com/group/cherrypy-use= rs">https://groups.google.com/group/cherrypy-users</a>.<br /> For more options, visit <a href=3D"https://groups.google.com/d/optout">http= s://groups.google.com/d/optout</a>.<br /> --000000000000773e6e057ecaec1e--