librelist archives

« back to archive

Delegate request to one flask app to another one (on different machines)

Delegate request to one flask app to another one (on different machines)

From:
Office
Date:
2014-09-27 @ 11:10
I have a VPN Network with many machines, all running an apache2 wich a 
flask web app in the background. I want now somethink like a master 
flask app on a server. So you connect to this server and after auth you 
get access to a specific web app whis runs in the VPN network behind the 
main server. For this I want to route all incoming requests (GET, POST 
with form data, json and so on) to a specific machine. I found some 
snippets with are already working, but I have somt troubles with the 
sessions. As I run the flask LoginManager on the machines I cannot fire 
everytime a simple get or post. But the following code isn't really 
working. So hopefully somebody can help me. I have just one static IP 
(192.168.1.13) now in my source code for testing.

So all static files and get request seems ok the main problem, are 
session based post request. And json I guess but I could test this at all.

Thanks a lot in advance R



Source Code:


|from  flaskimport  Flask
from  flaskimport  Response,  request,  session
from  flaskimport  stream_with_context
from  flask.ext.loginimport  LoginManager,  login_user,  logout_user,  
UserMixin,  login_required

import  requests


class  SecuredStaticFlask(Flask):
     def  send_static_file(self,  filename):
         url=  u"http://192.168.1.13/"  +  filename
         req=  requests.get(url,  stream=  True)
         return  Response(stream_with_context(req.iter_content()),  
content_type=  req.headers['content-type'])

global  s
s=  requests.Session()
app=  SecuredStaticFlask(__name__,  static_folder="static",  static_path="")


@app.route('/',  defaults={'path':  ''}  ,  methods=["POST","GET"])
@app.route('/<path:path>',  methods=["POST","GET"])
def  catch_all(path):
     global  s
     url=  u"http://192.168.1.13/"  +  path
     if  request.method==  "GET":
         req=  s.get(url)
     else:
         headers=  {"Content-type":  "application/x-www-form-urlencoded",}
         req=  s.post(url,  data=  request.form,  headers=  headers)
     return  Response(stream_with_context(req.iter_content()),  
content_type=  req.headers['content-type'])


|

Re: [flask] Delegate request to one flask app to another one (on different machines)

From:
Office
Date:
2014-09-30 @ 14:12
Sorry do pushing this forward again, but has anyone a hint for me?

Reinhard

Am 27.09.2014 13:10, schrieb Office:
> I have a VPN Network with many machines, all running an apache2 wich a 
> flask web app in the background. I want now somethink like a master 
> flask app on a server. So you connect to this server and after auth 
> you get access to a specific web app whis runs in the VPN network 
> behind the main server. For this I want to route all incoming requests 
> (GET, POST with form data, json and so on) to a specific machine. I 
> found some snippets with are already working, but I have somt troubles 
> with the sessions. As I run the flask LoginManager on the machines I 
> cannot fire everytime a simple get or post. But the following code 
> isn't really working. So hopefully somebody can help me. I have just 
> one static IP (192.168.1.13) now in my source code for testing.
>
> So all static files and get request seems ok the main problem, are 
> session based post request. And json I guess but I could test this at all.
>
> Thanks a lot in advance R
>
>
>
> Source Code:
>
>
> |from  flaskimport  Flask
> from  flaskimport  Response,  request,  session
> from  flaskimport  stream_with_context
> from  flask.ext.loginimport  LoginManager,  login_user,  logout_user,  
UserMixin,  login_required
>
> import  requests
>
>
> class  SecuredStaticFlask(Flask):
>      def  send_static_file(self,  filename):
>          url=  u"http://192.168.1.13/"  +  filename
>          req=  requests.get(url,  stream=  True)
>          return  Response(stream_with_context(req.iter_content()),  
content_type=  req.headers['content-type'])
>
> global  s
> s=  requests.Session()
> app=  SecuredStaticFlask(__name__,  static_folder="static",  static_path="")
>
>
> @app.route('/',  defaults={'path':  ''
> }  ,  methods=["POST","GET"])
> @app.route('/<path:path>',  methods=["POST","GET"])
> def  catch_all(path):
>      global  s
>      url=  u"http://192.168.1.13/"  +  path
>      if  request.method==  "GET":
>          req=  s.get(url)
>      else:
>          headers=  {"Content-type":  "application/x-www-form-urlencoded",}
>          req=  s.post(url<
> /span>,  data=  request.form,  headers=  headers)
>      return  Response(stream_with_context(req.iter_content()),  
content_type=  req.headers['content-type'])
>
>
> |
>


-- 
____________________________
RK Electronic Solutions e.U.
eingetragenes Einzelunternehmen
Inhaber: Ing. Reinhard Kager, BSc.
Sitz: 8572 Bärnbach
E-Mail: office@rkes.at
Internet: www.rkes.at
Telefon: 0664/73133760

FN: 350587w
FB-Gericht: Graz

Re: [flask] Delegate request to one flask app to another one (on different machines)

From:
Matthias Urlichs
Date:
2014-09-30 @ 14:44
Hi,

> I have some troubles with the sessions

What sessions?
I don't see any session-related code in the snippet you posted.

If your problem is to forward the client's session between the client and
the back-end server, you obviously need to do something with the server's
session cookie; your code seems to assume that this happens by magic.
Well, it doesn't. :-P

I.e., either store it in your client session, or forward cookies between
client and server. (Being sure to skip your own. Renaming the intermediate
Flask's session cookie helps; see the docs.)

I would also investigate whether a "real" web frontend like haproxy or an
appropriately configured lighttpd might not be a better solution.

-- 
-- Matthias Urlichs