werkzeug

Parse X-Forwarded-For to get ip with werkzeug on Heroku

旧时模样 提交于 2019-11-30 20:27:50
Heroku proxies requests from a client to server, so you have to parse the X-Forwarded-For to find the originating IP address. The general format of the X-Forwarded-For is: X-Forwarded-For: client1, proxy1, proxy2 Using werkzeug on flask, I'm trying to come up with a solution in order to access the originating IP of the client. Does anyone know a good way to do this? Thank you! Werkzeug (and Flask) store headers in an instance of werkzeug.datastructures.Headers . You should be able to do something like this: provided_ips = request.headers.getlist("X-Forwarded-For") # The first entry in the list

exception for non existing parameter in FLASK

淺唱寂寞╮ 提交于 2019-11-30 13:12:21
I have a form that sends parameters. In my form I have a checkbox. If my checkbox is not checked then I will not get any parameters. If in my module I have : var = request.form['mycheckbox'] and if my checkbox is not checked (the parameter is not passed) Then in debug mode I get the error message : Bad Request The browser (or proxy) sent a request that this server could not understand. Nothing tells me about what the error is. I prevented the exception by using : try: var=request.form['checkbox'] except: var=None But can I not change the behavior of how Flask handles this case ? The reason for

使用Python搭建http服务器

故事扮演 提交于 2019-11-30 11:18:42
David Wheeler有一句名言:“计算机科学中的任何问题,都可以通过加上另一层间接的中间层解决。”为了提高Python网络服务的可移植性,Python社区在PEP 333中提出了Web服务器网关接口(WSGI,Web Server Gateway Interface)。 为了提高Python网络服务的可移植性,Python社区在PEP 333中提出了Web服务器网关接口(WSGI,Web Server Gateway Interface)。 WSGL标准就是添加了一层中间层。通过这一个中间层,用Python编写的HTTP服务就能够与任何Web服务器进行交互了。现在,WSGI已经成为了使用Python进行HTTP操作的标准方法。 按照标准的定义,WSGI应用程序是可以被调用的,并且有两个输入参数。 1、WSGI 下面是第一段代码,第一个参数是environ,用于接收一个字典,字典中提供的键值对是旧式的CGI环境集合的拓展。第二个参数本身也是可以被调用的,习惯上会将其命名为start_response(),WSGI应用程序通过这个参数来声明响应头信息。 # 用WSGI应用形式编写的简单HTTP服务。 #!/usr/bin/env python3 # A simple HTTP service built directly against the low-level WSGI

Get IP Address when testing flask application through nosetests

时间秒杀一切 提交于 2019-11-30 08:16:57
My application depends on request.remote_addr which is None when i run tests through nosetests which uses app.test_client().post('/users/login', ....) . How can I emulate an IP (127.0.0.1 works fine) when I run tests? I've tried setting environment variables, sent in headers with the post() method and I've digged through nosetests, werkzeugs and flasks documentation but nothing I've tried has worked. You can set options for the underlying Werkzeug environment using environ_base : from flask import Flask, request import unittest app = Flask(__name__) app.debug = True app.testing = True @app

Flask/Werkzeug, how to return previous page after login

安稳与你 提交于 2019-11-30 06:02:56
I am using the Flask micro-framework which is based on Werkzeug, which uses Python. Before each restricted page there is a decorator to ensure the user is logged in, currently returning them to the login page if they are not logged in, like so: # Decorator def logged_in(f): @wraps(f) def decorated_function(*args, **kwargs): try: if not session['logged_in']: flash('Please log in first...', 'error') return redirect(url_for('login')) else: return f(*args, **kwargs) except KeyError: flash('Please log in first...', 'error') return redirect(url_for('login')) return decorated_function # Login

How do I safely get the user's real IP address in Flask (using mod_wsgi)?

家住魔仙堡 提交于 2019-11-29 23:21:41
I have a flask app setup on mod_wsgi/Apache and need to log the IP Address of the user. request.remote_addr returns "127.0.0.1" and this fix attempts to correct that but I've found that Django removed similar code for security reasons. Is there a better way to safely get the user's real IP address? EDIT: Maybe I'm missing something obvious. I applied werkzeug's/Flask's fix but it doesn't seem to make a difference when I try a request with altered headers: run.py: from werkzeug.contrib.fixers import ProxyFix app.wsgi_app = ProxyFix(app.wsgi_app) app.run() view.py: for ip in request.access_route

exception for non existing parameter in FLASK

元气小坏坏 提交于 2019-11-29 18:17:39
问题 I have a form that sends parameters. In my form I have a checkbox. If my checkbox is not checked then I will not get any parameters. If in my module I have : var = request.form['mycheckbox'] and if my checkbox is not checked (the parameter is not passed) Then in debug mode I get the error message : Bad Request The browser (or proxy) sent a request that this server could not understand. Nothing tells me about what the error is. I prevented the exception by using : try: var=request.form[

Match an arbitrary path, or the empty string, without adding multiple Flask route decorators

[亡魂溺海] 提交于 2019-11-29 15:33:47
I want to capture all urls beginning with the prefix /stuff , so that the following examples match: /users , /users/ , and /users/604511/edit . Currently I write multiple rules to match everything. Is there a way to write one rule to match what I want? @blueprint.route('/users') @blueprint.route('/users/') @blueprint.route('/users/<path:path>') def users(path=None): return str(path) It's reasonable to assign multiple rules to the same endpoint. That's the most straightforward solution. If you want one rule, you can write a custom converter to capture either the empty string or arbitrary data

Capture a list of integers with a Flask route

一曲冷凌霜 提交于 2019-11-29 14:55:50
I'm trying to implement a basic calculator in Flask. I define two url parameters, which is manageable when I only want to add two values. However, I want to add any number of values. How can I get a list of integers without writing an infinitely long route? @app.route('/add/<int:n1>,<int:n2>') def add(n1,n2): sum = n1+n2 return "%d" % (sum) I tried to solve my problem with this code, but it's not working integer_list = [] @app.route('/add/integer_list') def fun (integer_list): sum = 0 for item in integer_list: sum = sum + item return '%d' % sum Create a custom url converter that matches comma

flask入门

匆匆过客 提交于 2019-11-29 14:38:25
1.flask介绍     Flask是Python编写的一款轻量级Web应用框架。其 WSGI 工具箱采用 Werkzeug ,模板引擎则使用 Jinja2。   Flask使用 BSD 授权。其中两个环境依赖是Werkzeug和jinja2,这意味着它不需要依赖外部库,正因如此,我们将其称为轻量级框架。    Werkzeug是一个WSGI工具集, 作为一个 Web 框架的底层库,它封装好了很多 Web 框架的东西,例如 Request,Response 等。 flask: 轻,短小精悍 快,三行代码开启服务 自带组件少,大多数来源第三方,flask-admin,flask-session flask大版本更新,组件更新速度慢 django: 大而全,admin,models,Form,中间件 一个框架解决所有问题 一旦启动,所有资源全部加载 太大,结构复杂 所有组件都有django组件自身控制  tornado: 原生websocket 异步IO 非阻塞 几乎无三方及原生组件,连session都不支持 flask常用扩展包 Flask-SQLalchemy:操作数据库; Flask-script:插入脚本; Flask-migrate:管理迁移数据库; Flask-Session:Session存储方式指定; Flask-WTF:表单; Flask-Mail:邮件;