使用Flask代理到另一个Web服务 [英] Proxying to another web service with Flask

查看:694
本文介绍了使用Flask代理到另一个Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将对Flask应用程序发出的请求代理到计算机上本地运行的另一个Web服务.我宁愿使用Flask而不是使用更高级别的Nginx实例,以便我们可以重复使用内置在应用程序中的现有身份验证系统.我们越能保持单一登录"越好.

是否存在现有模块或其他代码来执行此操作?试图将Flask应用程序桥接到诸如httplib或urllib之类的东西确实很痛苦.

解决方案

我在基于Werkzeug的应用程序中使用httplib实现了代理的实现(如您的情况,我需要使用Webapp的身份验证和授权). /p>

尽管Flask文档未说明如何访问HTTP标头,但您可以使用request.headers(请参见 Werkzeug文档).如果您不需要修改响应,并且代理应用使用的标头是可以预测的,则代理是很简单的.

请注意,如果不需要修改响应,则应使用werkzeug.wsgi.wrap_file来包装httplib的响应流.这样就可以将打开的操作系统级文件描述符传递给HTTP服务器,以实现最佳性能.

I want to proxy requests made to my Flask app to another web service running locally on the machine. I'd rather use Flask for this than our higher-level nginx instance so that we can reuse our existing authentication system built into our app. The more we can keep this "single sign on" the better.

Is there an existing module or other code to do this? Trying to bridge the Flask app through to something like httplib or urllib is proving to be a pain.

解决方案

I have an implementation of a proxy using httplib in a Werkzeug-based app (as in your case, I needed to use the webapp's authentication and authorization).

Although the Flask docs don't state how to access the HTTP headers, you can use request.headers (see Werkzeug documentation). If you don't need to modify the response, and the headers used by the proxied app are predictable, proxying is staightforward.

Note that if you don't need to modify the response, you should use the werkzeug.wsgi.wrap_file to wrap httplib's response stream. That allows passing of the open OS-level file descriptor to the HTTP server for optimal performance.

这篇关于使用Flask代理到另一个Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆