使用Flask,我如何读取HTTP头?我想检查客户端发送的授权头。

我正在做一个网站,用户可以登录和下载文件,使用Flask微框架(基于Werkzeug),它使用Python(在我的情况下是2.6)。

我需要在用户登录时获得用户的IP地址(用于登录目的)。 有人知道怎么做吗?肯定有一种方法可以用Python来实现吗?

如何在SQLAlchemy中执行原始SQL ?

我有一个python web应用程序,运行在flask和接口到数据库通过SQLAlchemy。

我需要一种方法来运行原始SQL。该查询涉及多个表连接和内联视图。

我试过了:

connection = db.session.connection()
connection.execute( <sql here> )

但是我总是得到网关错误。

我有一个Flask服务器通过端口5000运行,它很好。我可以在http://example.com:5000上访问它

但是是否可以通过http://example.com访问呢?我假设这意味着我必须将端口从5000更改为80。但是当我在Flask上尝试时,当我运行它时,我得到这个错误消息。

Traceback (most recent call last):
  File "xxxxxx.py", line 31, in <module>
app.run(host="0.0.0.0", port=int("80"), debug=True)
   File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 772, in run
run_simple(host, port, self, **options)
  File "/usr/local/lib/python2.6/dist-packages/werkzeug/serving.py", line 706, in run_simple
    test_socket.bind((hostname, port))
  File "<string>", line 1, in bind
socket.error: [Errno 98] Address already in use

运行lsof -i:80返回

COMMAND   PID     USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
apache2   467     root    3u  IPv4 92108840      0t0  TCP *:www (LISTEN)
apache2  4413 www-data    3u  IPv4 92108840      0t0  TCP *:www (LISTEN)
apache2 14346 www-data    3u  IPv4 92108840      0t0  TCP *:www (LISTEN)
apache2 14570 www-data    3u  IPv4 92108840      0t0  TCP *:www (LISTEN)
apache2 14571 www-data    3u  IPv4 92108840      0t0  TCP *:www (LISTEN)
apache2 14573 www-data    3u  IPv4 92108840      0t0  TCP *:www (LISTEN)

我需要先终止这些进程吗?这样安全吗?或者有没有另一种方法来保持Flask在端口5000上运行,但有主网站域名以某种方式重定向?

我想做的是类似于回应。重定向作为在c# -即:重定向到一个特定的URL -我怎么去做这个?

这是我的代码:

import os
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello World!'

if __name__ == '__main__':
    # Bind to PORT if defined, otherwise default to 5000.
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)

我不太明白这个烧瓶的用途。jsonify方法。我尝试从这个JSON字符串:

data = {"id": str(album.id), "title": album.title}

而是json得到的。dump不同于我得到的flask.jsonify。

json.dumps(data): [{"id": "4ea856fd6506ae0db42702dd", "title": "Business"}]
flask.jsonify(data): {"id":…, "title":…}

显然,我需要得到一个看起来更像json的结果。转储的回报。我做错了什么?

是否可以直接声明一个烧瓶URL可选参数?

目前我正在进行如下工作:

@user.route('/<userId>')
@user.route('/<userId>/<username>')
def show(userId, username=None):
    pass

我怎么能直接说用户名是可选的?

我正在研究如何用Python开发一个不错的web应用程序。因为我不想让一些高阶结构妨碍我,所以我选择了轻量级的Flask框架。时间会证明这是否是正确的选择。

So, now I've set up an Apache server with mod_wsgi, and my test site is running fine. However, I'd like to speed up the development routine by making the site automatically reload upon any changes in py or template files I make. I see that any changes in site's .wsgi file causes reloading (even without WSGIScriptReloading On in the apache config file), but I still have to prod it manually (ie, insert extra linebreak, save). Is there some way how to cause reload when I edit some of the app's py files? Or, I am expected to use IDE that refreshes the .wsgi file for me?

我读到用cURL发送cookie有用,但不适合我。

我有一个这样的REST端点:

class LoginResource(restful.Resource):
    def get(self):
        print(session)
        if 'USER_TOKEN' in session:
            return 'OK'
        return 'not authorized', 401

当我尝试访问端点时,它拒绝:

curl -v -b ~/Downloads/cookies.txt -c ~/Downloads/cookies.txt http://127.0.0.1:5000/
* About to connect() to 127.0.0.1 port 5000 (#0)
*   Trying 127.0.0.1...
* connected
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.27.0
> Host: 127.0.0.1:5000
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 401 UNAUTHORIZED
< Content-Type: application/json
< Content-Length: 16
< Server: Werkzeug/0.8.3 Python/2.7.2
< Date: Sun, 14 Apr 2013 04:45:45 GMT
<
* Closing connection #0
"not authorized"%

我的~/Downloads/cookies.txt是:

cat ~/Downloads/cookies.txt
USER_TOKEN=in

服务器什么也收不到:

127.0.0.1 - - [13/Apr/2013 21:43:52] "GET / HTTP/1.1" 401 -
127.0.0.1 - - [13/Apr/2013 21:45:30] "GET / HTTP/1.1" 401 -
<SecureCookieSession {}>
<SecureCookieSession {}>
127.0.0.1 - - [13/Apr/2013 21:45:45] "GET / HTTP/1.1" 401 -

我错过了什么?

我试图使用Flask构建一个简单的API,在其中我现在想读取一些post JSON。我用邮递员Chrome扩展做POST, JSON I POST只是{"text":"lalala"}。我尝试使用以下方法读取JSON:

@app.route('/api/add_message/<uuid>', methods=['GET', 'POST'])
def add_message(uuid):
    content = request.json
    print content
    return uuid

在浏览器上,它正确地返回我放入GET中的UUID,但在控制台上,它只是打印出None(我希望它打印出{"text":"lalala"})。有人知道我如何从Flask方法中获得发布的JSON吗?