标签归档:werkzeug

无论内容类型标头如何,都可以在Python Flask中获取原始POST正文

问题:无论内容类型标头如何,都可以在Python Flask中获取原始POST正文

以前,我问过如何获取Flask请求中的数据,因为它request.data是空的。答案解释request.data为原始帖子正文,但如果分析表单数据将为空。我如何无条件获得原始职位?

@app.route('/', methods=['POST'])
def parse_request():
    data = request.data  # empty in some cases
    # always need raw data here, not parsed form data

Previously, I asked How to get data received in Flask request because request.data was empty. The answer explained that request.data is the raw post body, but will be empty if form data is parsed. How can I get the raw post body unconditionally?

@app.route('/', methods=['POST'])
def parse_request():
    data = request.data  # empty in some cases
    # always need raw data here, not parsed form data

回答 0

使用request.get_data()获得的原始数据,而不管内容类型。该数据被缓存,您可以随后访问request.datarequest.jsonrequest.form随意。

如果您request.data首先访问,它将首先调用get_data一个参数以解析表单数据。如果请求具有形式的内容类型(multipart/form-dataapplication/x-www-form-urlencoded,或application/x-url-encoded),则原始数据将被消耗。request.data并且request.json在这种情况下将显示为空。

Use request.get_data() to get the raw data, regardless of content type. The data is cached and you can subsequently access request.data, request.json, request.form at will.

If you access request.data first, it will call get_data with an argument to parse form data first. If the request has a form content type (multipart/form-data, application/x-www-form-urlencoded, or application/x-url-encoded) then the raw data will be consumed. request.data and request.json will appear empty in this case.


回答 1

request.stream是WSGI服务器传递给应用程序的原始数据流。读取时不进行任何解析,尽管通常会这样做request.get_data()

data = request.stream.read()

如果该流先前被request.data或其他属性读取,则将为空。

request.stream is the stream of raw data passed to the application by the WSGI server. No parsing is done when reading it, although you usually want request.get_data() instead.

data = request.stream.read()

The stream will be empty if it was previously read by request.data or another attribute.


回答 2

我创建了一个WSGI中间件,用于存储environ['wsgi.input']流中的原始内容。我将值保存在WSGI环境中,因此可以从request.environ['body_copy']我的应用程序中访问它。

在Werkzeug或Flask中这不是必需的,因为request.get_data()无论内容类型如何,都将获取原始数据,但是可以更好地处理HTTP和WSGI行为。

这会将整个主体读入内存,如果发布了一个大文件,这将是一个问题。如果Content-Length缺少标题,它将不会读取任何内容,因此它将无法处理流式请求。

from io import BytesIO

class WSGICopyBody(object):
    def __init__(self, application):
        self.application = application

    def __call__(self, environ, start_response):
        length = int(environ.get('CONTENT_LENGTH') or 0)
        body = environ['wsgi.input'].read(length)
        environ['body_copy'] = body
        # replace the stream since it was exhausted by read()
        environ['wsgi.input'] = BytesIO(body)
        return self.application(environ, start_response)

app.wsgi_app = WSGICopyBody(app.wsgi_app)
request.environ['body_copy']

I created a WSGI middleware that stores the raw body from the environ['wsgi.input'] stream. I saved the value in the WSGI environ so I could access it from request.environ['body_copy'] within my app.

This isn’t necessary in Werkzeug or Flask, as request.get_data() will get the raw data regardless of content type, but with better handling of HTTP and WSGI behavior.

This reads the entire body into memory, which will be an issue if for example a large file is posted. This won’t read anything if the Content-Length header is missing, so it won’t handle streaming requests.

from io import BytesIO

class WSGICopyBody(object):
    def __init__(self, application):
        self.application = application

    def __call__(self, environ, start_response):
        length = int(environ.get('CONTENT_LENGTH') or 0)
        body = environ['wsgi.input'].read(length)
        environ['body_copy'] = body
        # replace the stream since it was exhausted by read()
        environ['wsgi.input'] = BytesIO(body)
        return self.application(environ, start_response)

app.wsgi_app = WSGICopyBody(app.wsgi_app)
request.environ['body_copy']

回答 3

request.data如果request.headers["Content-Type"]被识别为表单数据,则将为空,并将其解析为request.form。要获取原始数据而不管内容类型如何,请使用request.get_data()

request.data调用request.get_data(parse_form_data=True),这导致表单数据的行为不同。

request.data will be empty if request.headers["Content-Type"] is recognized as form data, which will be parsed into request.form. To get the raw data regardless of content type, use request.get_data().

request.data calls request.get_data(parse_form_data=True), which results in the different behavior for form data.


使用Flask for Python获取访问者的IP地址

问题:使用Flask for Python获取访问者的IP地址

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

我需要获得用户登录时的IP地址(出于记录目的)。有谁知道如何做到这一点?当然有办法用Python做到吗?

I’m making a website where users can log on and download files, using the Flask micro-framework (based on Werkzeug) which uses Python (2.6 in my case).

I need to get the IP address of users when they log on (for logging purposes). Does anyone know how to do this? Surely there is a way to do it with Python?


回答 0

请参阅有关如何访问Request对象,然后从同一Request对象(即attribute)获取文档remote_addr

代码示例

from flask import request
from flask import jsonify

@app.route("/get_my_ip", methods=["GET"])
def get_my_ip():
    return jsonify({'ip': request.remote_addr}), 200

有关更多信息,请参阅Werkzeug文档

See the documentation on how to access the Request object and then get from this same Request object, the attribute remote_addr.

Code example

from flask import request
from flask import jsonify

@app.route("/get_my_ip", methods=["GET"])
def get_my_ip():
    return jsonify({'ip': request.remote_addr}), 200

For more information see the Werkzeug documentation.


回答 1

代理会使这变得有些棘手,如果使用代理,请确保签出ProxyFixFlask docs)。看一下request.environ您的特定环境。使用nginx,有时我会做这样的事情:

from flask import request   
request.environ.get('HTTP_X_REAL_IP', request.remote_addr)   

当代理(例如nginx)转发地址时,它们通常在请求标头中的某处包括原始IP。

更新 请参见flask-security实现。同样,在实施之前,请阅读有关ProxyFix的文档。您的解决方案可能会因您的特定环境而异。

Proxies can make this a little tricky, make sure to check out ProxyFix (Flask docs) if you are using one. Take a look at request.environ in your particular environment. With nginx I will sometimes do something like this:

from flask import request   
request.environ.get('HTTP_X_REAL_IP', request.remote_addr)   

When proxies, such as nginx, forward addresses, they typically include the original IP somewhere in the request headers.

Update See the flask-security implementation. Again, review the documentation about ProxyFix before implementing. Your solution may vary based on your particular environment.


回答 2

实际上,您将发现,仅获取以下内容即可获得服务器的地址:

request.remote_addr

如果要客户端IP地址,请使用以下命令:

request.environ['REMOTE_ADDR']

Actually, what you will find is that when simply getting the following will get you the server’s address:

request.remote_addr

If you want the clients IP address, then use the following:

request.environ['REMOTE_ADDR']

回答 3

可以使用以下代码段检索用户的IP地址:

from flask import request
print(request.remote_addr)

The user’s IP address can be retrieved using the following snippet:

from flask import request
print(request.remote_addr)

回答 4

我有Nginx并且在Nginx Config下面:

server {
    listen 80;
    server_name xxxxxx;
    location / {
               proxy_set_header   Host                 $host;
               proxy_set_header   X-Real-IP            $remote_addr;
               proxy_set_header   X-Forwarded-For      $proxy_add_x_forwarded_for;
               proxy_set_header   X-Forwarded-Proto    $scheme;

               proxy_pass http://x.x.x.x:8000;
        }
}

@ tirtha-r解决方案为我工作

#!flask/bin/python
from flask import Flask, jsonify, request
app = Flask(__name__)

@app.route('/', methods=['GET'])
def get_tasks():
    if request.environ.get('HTTP_X_FORWARDED_FOR') is None:
        return jsonify({'ip': request.environ['REMOTE_ADDR']}), 200
    else:
        return jsonify({'ip': request.environ['HTTP_X_FORWARDED_FOR']}), 200

if __name__ == '__main__':
    app.run(debug=True,host='0.0.0.0', port=8000)

我的要求和回应:

curl -X GET http://test.api

{
    "ip": "Client Ip......"
}

I have Nginx and With below Nginx Config:

server {
    listen 80;
    server_name xxxxxx;
    location / {
               proxy_set_header   Host                 $host;
               proxy_set_header   X-Real-IP            $remote_addr;
               proxy_set_header   X-Forwarded-For      $proxy_add_x_forwarded_for;
               proxy_set_header   X-Forwarded-Proto    $scheme;

               proxy_pass http://x.x.x.x:8000;
        }
}

@tirtha-r solution worked for me

#!flask/bin/python
from flask import Flask, jsonify, request
app = Flask(__name__)

@app.route('/', methods=['GET'])
def get_tasks():
    if request.environ.get('HTTP_X_FORWARDED_FOR') is None:
        return jsonify({'ip': request.environ['REMOTE_ADDR']}), 200
    else:
        return jsonify({'ip': request.environ['HTTP_X_FORWARDED_FOR']}), 200

if __name__ == '__main__':
    app.run(debug=True,host='0.0.0.0', port=8000)

My Request and Response:

curl -X GET http://test.api

{
    "ip": "Client Ip......"
}

回答 5

以下代码始终提供客户端的公共IP(而不是代理后面的私有IP)。

from flask import request

if request.environ.get('HTTP_X_FORWARDED_FOR') is None:
    print(request.environ['REMOTE_ADDR'])
else:
    print(request.environ['HTTP_X_FORWARDED_FOR']) # if behind a proxy

The below code always gives the public IP of the client (and not a private IP behind a proxy).

from flask import request

if request.environ.get('HTTP_X_FORWARDED_FOR') is None:
    print(request.environ['REMOTE_ADDR'])
else:
    print(request.environ['HTTP_X_FORWARDED_FOR']) # if behind a proxy

回答 6

httpbin.org使用以下方法:

return jsonify(origin=request.headers.get('X-Forwarded-For', request.remote_addr))

httpbin.org uses this method:

return jsonify(origin=request.headers.get('X-Forwarded-For', request.remote_addr))

回答 7

如果您在其他平衡器(例如AWS Application Balancer)之后使用Nginx,则HTTP_X_FORWARDED_FOR返回地址列表。可以这样修复:

if 'X-Forwarded-For' in request.headers:
    proxy_data = request.headers['X-Forwarded-For']
    ip_list = proxy_data.split(',')
    user_ip = ip_list[0]  # first address in list is User IP
else:
    user_ip = request.remote_addr  # For local development

If you use Nginx behind other balancer, for instance AWS Application Balancer, HTTP_X_FORWARDED_FOR returns list of addresses. It can be fixed like that:

if 'X-Forwarded-For' in request.headers:
    proxy_data = request.headers['X-Forwarded-For']
    ip_list = proxy_data.split(',')
    user_ip = ip_list[0]  # first address in list is User IP
else:
    user_ip = request.remote_addr  # For local development

回答 8

如果您使用的是Gunicorn和Nginx环境,则以下代码模板适用于您。

addr_ip4 = request.remote_addr

If You are using Gunicorn and Nginx environment then the following code template works for you.

addr_ip4 = request.remote_addr

回答 9

这应该做的工作。它提供客户端IP地址(远程主机)。

请注意,此代码在服务器端运行。

from mod_python import apache

req.get_remote_host(apache.REMOTE_NOLOOKUP)

This should do the job. It provides the client IP address (remote host).

Note that this code is running on the server side.

from mod_python import apache

req.get_remote_host(apache.REMOTE_NOLOOKUP)

将Flask开发服务器配置为在网络上可见

问题:将Flask开发服务器配置为在网络上可见

我不确定这是否是Flask专用的,但是当我在开发模式(http://localhost:5000)下运行应用程序时,无法从网络上的其他计算机(使用http://[dev-host-ip]:5000)访问它。例如,在开发模式下使用Rails时,它可以正常工作。我找不到有关Flask开发服务器配置的任何文档。任何想法应该配置为启用此功能吗?

I’m not sure if this is Flask specific, but when I run an app in dev mode (http://localhost:5000), I cannot access it from other machines on the network (with http://[dev-host-ip]:5000). With Rails in dev mode, for example, it works fine. I couldn’t find any docs regarding the Flask dev server configuration. Any idea what should be configured to enable this?


回答 0

尽管这是可行的,但您不应在生产中使用Flask dev服务器。Flask开发服务器的设计并非特别安全,稳定或高效。有关正确的解决方案,请参阅有关部署的文档。


将参数添加到中app.run()。默认情况下,它在本地主机上运行,​​将其更改app.run(host= '0.0.0.0')为在您的计算机IP地址上运行。

快速入门页上的“外部可见的服务器”下的Flask网站上记录

外部可见服务器

如果运行服务器,您会注意到该服务器仅可用于您自己的计算机,而不能用于网络中的任何其他服务器。这是默认设置,因为在调试模式下,应用程序的用户可以在计算机上执行任意Python代码。如果禁用了调试或信任网络上的用户,则可以使服务器公开可用。

只需将run()方法的调用更改为如下所示:

app.run(host='0.0.0.0')

这告诉您的操作系统侦听公共IP。

While this is possible, you should not use the Flask dev server in production. The Flask dev server is not designed to be particularly secure, stable, or efficient. See the docs on deploying for correct solutions.


Add a parameter to your app.run(). By default it runs on localhost, change it to app.run(host= '0.0.0.0') to run on your machines IP address.

Documented on the Flask site under “Externally Visible Server” on the Quickstart page:

Externally Visible Server

If you run the server you will notice that the server is only available from your own computer, not from any other in the network. This is the default because in debugging mode a user of the application can execute arbitrary Python code on your computer. If you have debug disabled or trust the users on your network, you can make the server publicly available.

Just change the call of the run() method to look like this:

app.run(host='0.0.0.0')

This tells your operating system to listen on a public IP.


回答 1

如果使用flask可执行文件启动服务器,则可以使用flask run --host=0.0.0.0更改默认值,从127.0.0.1并将其打开到非本地连接。其他答案描述的config和app.run方法可能是更好的做法,但这也很方便。

外部可见服务器如果运行服务器,您将注意到只能从您自己的计算机访问该服务器,而不能从网络中的任何其他服务器访问该服务器。这是默认设置,因为在调试模式下,应用程序的用户可以在计算机上执行任意Python代码。

如果禁用了调试器或信任网络上的用户,则只需在命令行中添加–host = 0.0.0.0,即可使服务器公开可用:

flask run –host = 0.0.0.0这告诉您的操作系统侦听所有公用IP。

参考:http//flask.pocoo.org/docs/0.11/quickstart/

If you use the flask executable to start your server, you can use flask run --host=0.0.0.0 to change the default from 127.0.0.1 and open it up to non local connections. The config and app.run methods that the other answers describe are probably better practice but this can be handy as well.

Externally Visible Server If you run the server you will notice that the server is only accessible from your own computer, not from any other in the network. This is the default because in debugging mode a user of the application can execute arbitrary Python code on your computer.

If you have the debugger disabled or trust the users on your network, you can make the server publicly available simply by adding –host=0.0.0.0 to the command line:

flask run –host=0.0.0.0 This tells your operating system to listen on all public IPs.

Reference: http://flask.pocoo.org/docs/0.11/quickstart/


回答 2

如果0.0.0.0方法不起作用,请尝试此操作

无聊的东西

我亲自进行了很多努力,以使我的应用可以通过本地服务器访问其他设备(笔记本电脑和手机)。我尝试了0.0.0.0方法,但是没有运气。然后,我尝试更改端口,但没有成功。因此,在尝试了一堆不同的组合之后,我找到了这个组合,它解决了我在本地服务器上部署应用程序的问题。

脚步

  1. 获取计算机的本地IPv4地址。这可以通过ipconfig在Windows以及ifconfiglinux和Mac上键入来完成。

请注意:以上步骤将在提供该应用程序的计算机上执行,而不是在您正在访问该应用程序的计算机上执行。另请注意,如果断开连接并重新连接到网络,IPv4地址可能会更改。

  1. 现在,只需使用获取的IPv4地址运行flask应用程序即可。

    flask run -h 192.168.X.X

    例如,就我而言(参见图片),我将其运行为:

    flask run -h 192.168.1.100

在我的移动设备上

可选的东西

如果您正在Windows上执行此过程,并使用Power Shell作为CLI,但仍然无法访问该网站,请在运行该应用程序的Shell中尝试使用CTRL + C命令。Power Shell有时会冻结,因此需要一点点恢复。这样做甚至可能终止服务器,但有时可以解决问题。

而已。如果您觉得有帮助,请竖起大拇指。😉

一些其他可选的东西

我创建了一个简短的Powershell脚本,可以在需要时为您提供IP地址:

$env:getIp = ipconfig
if ($env:getIp -match '(IPv4[\sa-zA-Z.]+:\s[0-9.]+)') {
    if ($matches[1] -match '([^a-z\s][\d]+[.\d]+)'){
        $ipv4 = $matches[1]
    }
}
echo $ipv4

将其保存到扩展名为.ps1的文件中(对于PowerShell),然后在启动您的应用程序之前对其运行。您可以将其保存在项目文件夹中,并以以下方式运行:

.\getIP.ps1; flask run -h $ipv4

注意:我将上面的shell代码保存在getIP.ps1中。

酷👌

Try this if the 0.0.0.0 method doesn’t work

Boring Stuff

I personally battled a lot to get my app accessible to other devices(laptops and mobile phones) through a local-server. I tried the 0.0.0.0 method, but no luck. Then I tried changing the port, but it just didn’t work. So, after trying a bunch of different combinations, I arrived to this one, and it solved my problem of deploying my app on a local-server.

Steps

  1. Get the local IPv4 address of your computer. This can be done by typing ipconfig on Windows and ifconfig on linux and Mac.

Please note: The above step is to be performed on the machine you are serving the app on, and on not the machine on which you are accessing it. Also note, that the IPv4 address might change if you disconnect and reconnect to the network.

  1. Now, simply run the flask app with the acquired IPv4 address.

    flask run -h 192.168.X.X

    E.g. In my case (see the image), I ran it as:

    flask run -h 192.168.1.100

On my mobile device

Optional Stuff

If you are performing this procedure on Windows, and using Power Shell as the CLI, and you still aren’t able to access the website, try a CTRL + C command in the shell that’s running the app. Power Shell get frozen up sometimes and it needs a pinch to revive. Doing this might even terminate the server, but it sometimes does the trick.

That’s it. Give a thumbs up if you found this helpful.😉

Some more optional stuff

I have created a short Powershell script that will get you your IP address whenever you need one:

$env:getIp = ipconfig
if ($env:getIp -match '(IPv4[\sa-zA-Z.]+:\s[0-9.]+)') {
    if ($matches[1] -match '([^a-z\s][\d]+[.\d]+)'){
        $ipv4 = $matches[1]
    }
}
echo $ipv4

Save it to a file with .ps1 extenstion (for PowerShell), and run it on before starting your app. You can save it in your project folder and run it as:

.\getIP.ps1; flask run -h $ipv4

Note: I saved the above shell code in getIP.ps1.

Cool.👌


回答 3

如果您的cool应用程序的配置是从外部文件加载的,如以下示例所示,请不要忘记使用HOST =“ 0.0.0.0”更新相应的配置文件

cool.app.run(
    host=cool.app.config.get("HOST", "localhost"),
    port=cool.app.config.get("PORT", 9000)
)            

If your cool app has it’s configuration loaded from an external file, like in the following example, then don’t forget to update the corresponding config file with HOST=”0.0.0.0″

cool.app.run(
    host=cool.app.config.get("HOST", "localhost"),
    port=cool.app.config.get("PORT", 9000)
)            

回答 4

在您的项目中添加以下几行

if __name__ == '__main__':
    app.debug = True
    app.run(host = '0.0.0.0',port=5005)

Add below lines to your project

if __name__ == '__main__':
    app.debug = True
    app.run(host = '0.0.0.0',port=5005)

回答 5

检查服务器上是否打开了特定端口以服务于客户端?

在Ubuntu或Linux发行版中

sudo ufw enable
sudo ufw allow 5000/tcp //allow the server to handle the request on port 5000

配置应用程序以处理远程请求

app.run(host='0.0.0.0' , port=5000)


python3 app.py & #run application in background

Check whether the particular port is open on the server to serve the client or not?

in Ubuntu or Linux distro

sudo ufw enable
sudo ufw allow 5000/tcp //allow the server to handle the request on port 5000

Configure the application to handle remote requests

app.run(host='0.0.0.0' , port=5000)


python3 app.py & #run application in background

回答 6

转到CMD(命令提示符)上的项目路径,然后执行以下命令:

设置FLASK_APP = ABC.py

SET FLASK_ENV =开发

烧瓶运行-h [yourIP] -p 8080

您将在CMD上获得以下o / p:-

  • 正在投放Flask应用“ expirement.py”(延迟加载)

现在,您可以使用http:// [yourIP]:8080 / url 在另一台计算机上访问flask应用程序

Go to your project path on CMD(command Prompt) and execute the following command:-

set FLASK_APP=ABC.py

SET FLASK_ENV=development

flask run -h [yourIP] -p 8080

you will get following o/p on CMD:-

  • Serving Flask app “expirement.py” (lazy loading)
    • Environment: development
    • Debug mode: on
    • Restarting with stat
    • Debugger is active!
    • Debugger PIN: 199-519-700
    • Running on http://[yourIP]:8080/ (Press CTRL+C to quit)

Now you can access your flask app on another machine using http://[yourIP]:8080/ url


回答 7

如果您在访问使用PyCharm部署的Flask服务器时遇到问题,请考虑以下因素:

PyCharm不会直接运行您的主.py文件,因此if __name__ == '__main__':不会执行其中的任何代码,并且任何更改(例如app.run(host='0.0.0.0', port=5000))都不会生效。

相反,您应该使用“运行配置”配置Flask服务器,尤其是将其放置--host 0.0.0.0 --port 5000在“ 其他选项”字段中。

有关在PyCharm中配置Flask服务器的更多信息

If you’re having troubles accessing your Flask server, deployed using PyCharm, take the following into account:

PyCharm doesn’t run your main .py file directly, so any code in if __name__ == '__main__': won’t be executed, and any changes (like app.run(host='0.0.0.0', port=5000)) won’t take effect.

Instead, you should configure the Flask server using Run Configurations, in particular, placing --host 0.0.0.0 --port 5000 into Additional options field.

More about configuring Flask server in PyCharm


回答 8

我遇到了同样的问题,我使用PyCharm作为编辑器,并且在创建项目时,PyCharm创建了Flask Server。我所做的就是通过以下方式用Python创建服务器;

基本上我所做的是创建一个新服务器,但如果不是python,则使用flask

希望对您有帮助

I had the same problem, I use PyCharm as an editor and when I created the project, PyCharm created a Flask Server. What I did was create a server with Python in the following way;

basically what I did was create a new server but flask if not python

I hope it helps you


回答 9

这个答案不仅与flask有关,而且应适用于所有无法连接来自另一个主机的服务

  1. 用于netstat -ano | grep <port>查看地址是0.0.0.0还是::。如果是127.0.0.1,则仅适用于本地请求。
  2. 使用tcpdump查看是否有任何数据包丢失。如果显示明显的不平衡,请通过iptables检查路由规则。

今天,我像往常一样运行烧瓶应用程序,但是我发现它无法从其他服务器连接。然后运行netstat -ano | grep <port>,本地地址为::or 0.0.0.0(我都尝试过,并且我知道127.0.0.1仅允许来自本地主机的连接)。然后我用了telnet host port,结果就像connect to ...。这很奇怪。然后我想我最好跟查一下tcpdump -i any port <port> -w w.pcap。我注意到这一切都是这样的:

然后通过检查iptables --listOUTPUT部分,我可以看到一些规则:

这些规则禁止握手时输出tcp重要数据包。通过删除它们,问题消失了。

This answer is not solely related with flask, but should be applicable for all cannot connect service from another host issue.

  1. use netstat -ano | grep <port> to see if the address is 0.0.0.0 or ::. If it is 127.0.0.1 then it is only for the local requests.
  2. use tcpdump to see if any packet is missing. If it shows obvious imbalance, check routing rules by iptables.

Today I run my flask app as usual, but I noticed it cannot connect from other server. Then I run netstat -ano | grep <port>, and the local address is :: or 0.0.0.0 (I tried both, and I know 127.0.0.1 only allows connection from the local host). Then I used telnet host port, the result is like connect to .... This is very odd. Then I thought I would better check it with tcpdump -i any port <port> -w w.pcap. And I noticed it is all like this:

Then by checking iptables --list OUTPUT section, I could see several rules:

these rules forbid output tcp vital packets in handshaking. By deleting them, the problem is gone.


回答 10

对我来说,我遵循上面的答案并对其进行了一些修改:

  1. 只需在命令提示符下使用ipconfig来获取您的ipv4地址
  2. 转到存在烧瓶代码的文件
  3. 在主函数中编写app.run(host =’您的ipv4地址’)

例如:

For me i followed the above answer and modified it a bit:

  1. Just grab your ipv4 address using ipconfig on command prompt
  2. Go to the file in which flask code is present
  3. In main function write app.run(host= ‘your ipv4 address’)

Eg:


回答 11

转到项目路径set FLASK_APP = ABC.py SET FLASK_ENV = development

flask run -h [yourIP] -p 8080,您将在CMD上遵循o / p:-*正在服务的Flask应用程序“ expirement.py”(延迟加载)*环境:开发*调试模式:on *以stat重新启动*调试器处于活动状态!*调试器PIN:199-519-700 *在http:// [yourIP]:8080 /上运行(按CTRL + C退出)

go to project path set FLASK_APP=ABC.py SET FLASK_ENV=development

flask run -h [yourIP] -p 8080 you will following o/p on CMD:- * Serving Flask app “expirement.py” (lazy loading) * Environment: development * Debug mode: on * Restarting with stat * Debugger is active! * Debugger PIN: 199-519-700 * Running on http://[yourIP]:8080/ (Press CTRL+C to quit)


回答 12

您还可以通过环境变量设置主机(将其暴露在面向IP地址的网络上)和端口。

$ export FLASK_APP=app.py
$ export FLASK_ENV=development
$ export FLASK_RUN_PORT=8000
$ export FLASK_RUN_HOST=0.0.0.0

$ flask run
 * Serving Flask app "app.py" (lazy loading)
 * Environment: development
 * Debug mode: on
 * Running on https://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 329-665-000

请参见如何获取所有可用的命令选项来设置环境变量?

You can also set the host (to expose it on a network facing IP address) and port via environment variables.

$ export FLASK_APP=app.py
$ export FLASK_ENV=development
$ export FLASK_RUN_PORT=8000
$ export FLASK_RUN_HOST=0.0.0.0

$ flask run
 * Serving Flask app "app.py" (lazy loading)
 * Environment: development
 * Debug mode: on
 * Running on https://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 329-665-000

See How to get all available Command Options to set environment variables?


回答 13

.flaskenv在项目根目录中创建文件。

该文件中的参数通常为:

FLASK_APP=app.py
FLASK_ENV=development
FLASK_RUN_HOST=[dev-host-ip]
FLASK_RUN_PORT=5000

如果您有虚拟环境,请激活它并执行pip install python-dotenv

该软件包将使用该.flaskenv文件,并且其中的声明将在终端会话之间自动导入。

那你可以做 flask run

Create file .flaskenv in the project root directory.

The parameters in this file are typically:

FLASK_APP=app.py
FLASK_ENV=development
FLASK_RUN_HOST=[dev-host-ip]
FLASK_RUN_PORT=5000

If you have a virtual environment, activate it and do a pip install python-dotenv .

This package is going to use the .flaskenv file, and declarations inside it will be automatically imported across terminal sessions.

Then you can do flask run


获取烧瓶请求中收到的数据

问题:获取烧瓶请求中收到的数据

我希望能够将数据发送到我的Flask应用。我尝试访问,request.data但它是一个空字符串。您如何访问请求数据?

from flask import request

@app.route('/', methods=['GET', 'POST'])
def parse_request():
    data = request.data  # data is empty
    # need posted data here

这个问题的答案使我提出了在Python Flask中获取原始POST正文的问题,而不管接下来的Content-Type标头如何,这都是关于获取原始数据而不是已解析数据的问题。

I want to be able to get the data sent to my Flask app. I’ve tried accessing request.data but it is an empty string. How do you access request data?

from flask import request

@app.route('/', methods=['GET', 'POST'])
def parse_request():
    data = request.data  # data is empty
    # need posted data here

The answer to this question led me to ask Get raw POST body in Python Flask regardless of Content-Type header next, which is about getting the raw data rather than the parsed data.


回答 0

文档描述的要求提供的属性。在大多数情况下,request.data由于它用作后备广告,因此将为空:

request.data 如果传入的请求数据带有mimetype Flask无法处理,则将其包含为字符串。

  • request.args:URL查询字符串中的键/值对
  • request.form:正文中的键/值对,来自HTML帖子形式或非JSON编码的JavaScript请求
  • request.files:Flask与正文分开的正文文件form。必须使用HTML表单,enctype=multipart/form-data否则将不会上传文件。
  • request.values:组合argsformargs如果键重叠则首选
  • request.json:解析的JSON数据。该请求必须具有application/json内容类型,或用于request.get_json(force=True)忽略内容类型。

所有这些都是MultiDict实例(除外json)。您可以使用以下方法访问值:

  • request.form['name']:如果您知道密钥存在,请使用索引
  • request.form.get('name')get如果密钥可能不存在,则使用
  • request.form.getlist('name')getlist如果键被多次发送并且需要值列表,则使用该键。get仅返回第一个值。

The docs describe the attributes available on the request. In most common cases request.data will be empty because it’s used as a fallback:

request.data Contains the incoming request data as string in case it came with a mimetype Flask does not handle.

  • request.args: the key/value pairs in the URL query string
  • request.form: the key/value pairs in the body, from a HTML post form, or JavaScript request that isn’t JSON encoded
  • request.files: the files in the body, which Flask keeps separate from form. HTML forms must use enctype=multipart/form-data or files will not be uploaded.
  • request.values: combined args and form, preferring args if keys overlap
  • request.json: parsed JSON data. The request must have the application/json content type, or use request.get_json(force=True) to ignore the content type.

All of these are MultiDict instances (except for json). You can access values using:

  • request.form['name']: use indexing if you know the key exists
  • request.form.get('name'): use get if the key might not exist
  • request.form.getlist('name'): use getlist if the key is sent multiple times and you want a list of values. get only returns the first value.

回答 1

要获取原始数据,请使用request.data。这仅在无法将其解析为表单数据时才有效,否则它将为空request.form并将具有解析后的数据。

from flask import request
request.data

To get the raw data, use request.data. This only works if it couldn’t be parsed as form data, otherwise it will be empty and request.form will have the parsed data.

from flask import request
request.data

回答 2

对于URL查询参数,请使用request.args

search = request.args.get("search")
page = request.args.get("page")

对于张贴的表单输入,请使用request.form

email = request.form.get('email')
password = request.form.get('password')

对于以内容类型发布的JSON application/json,请使用request.get_json()

data = request.get_json()

For URL query parameters, use request.args.

search = request.args.get("search")
page = request.args.get("page")

For posted form input, use request.form.

email = request.form.get('email')
password = request.form.get('password')

For JSON posted with content type application/json, use request.get_json().

data = request.get_json()

回答 3

这是解析发布的JSON数据并将其回显的示例。

from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/foo', methods=['POST']) 
def foo():
    data = request.json
    return jsonify(data)

要使用curl发布JSON:

curl -i -H "Content-Type: application/json" -X POST -d '{"userId":"1", "username": "fizz bizz"}' http://localhost:5000/foo

或使用邮递员:

Here’s an example of parsing posted JSON data and echoing it back.

from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/foo', methods=['POST']) 
def foo():
    data = request.json
    return jsonify(data)

To post JSON with curl:

curl -i -H "Content-Type: application/json" -X POST -d '{"userId":"1", "username": "fizz bizz"}' http://localhost:5000/foo

Or to use Postman:


回答 4

如果您发布内容类型为的JSON,请在Flask中application/json使用request.get_json()它。如果内容类型不正确,None则返回。如果数据不是JSON,则会引发错误。

@app.route("/something", methods=["POST"])
def do_something():
    data = request.get_json()

If you post JSON with content type application/json, use request.get_json() to get it in Flask. If the content type is not correct, None is returned. If the data is not JSON, an error is raised.

@app.route("/something", methods=["POST"])
def do_something():
    data = request.get_json()

回答 5

要获取原始帖子正文,而不管内容类型如何,请使用request.get_data()。如果使用request.data,它将调用request.get_data(parse_form_data=True),它将填充request.form MultiDictdata留空。

To get the raw post body regardless of the content type, use request.get_data(). If you use request.data, it calls request.get_data(parse_form_data=True), which will populate the request.form MultiDict and leave data empty.


回答 6

request.form使用普通字典,请使用request.form.to_dict(flat=False)

要返回API的JSON数据,请将其传递给jsonify

本示例将表单数据作为JSON数据返回。

@app.route('/form_to_json', methods=['POST'])
def form_to_json():
    data = request.form.to_dict(flat=False)
    return jsonify(data)

这是带有curl的POST表单数据的示例,返回为JSON:

$ curl http://127.0.0.1:5000/data -d "name=ivanleoncz&role=Software Developer"
{
  "name": "ivanleoncz", 
  "role": "Software Developer"
}

To get request.form as a normal dictionary , use request.form.to_dict(flat=False).

To return JSON data for an API, pass it to jsonify.

This example returns form data as JSON data.

@app.route('/form_to_json', methods=['POST'])
def form_to_json():
    data = request.form.to_dict(flat=False)
    return jsonify(data)

Here’s an example of POST form data with curl, returning as JSON:

$ curl http://127.0.0.1:5000/data -d "name=ivanleoncz&role=Software Developer"
{
  "name": "ivanleoncz", 
  "role": "Software Developer"
}

回答 7

使用request.get_json()得到张贴JSON数据。

data = request.get_json()
name = data.get('name', '')

使用request.formPOST方法提交表单时用于获取数据。

name = request.form.get('name', '')

使用request.args得到的网址,用GET方法提交表单的时候喜欢的查询字符串传递的数据。

request.args.get("name", "")

request.form等类似dict,get如果未通过默认值,请使用方法获取值。

Use request.get_json() to get posted JSON data.

data = request.get_json()
name = data.get('name', '')

Use request.form to get data when submitting a form with the POST method.

name = request.form.get('name', '')

Use request.args to get data passed in the query string of the URL, like when submitting a form with the GET method.

request.args.get("name", "")

request.form etc. are dict-like, use the get method to get a value with a default if it wasn’t passed.


回答 8

要发布不包含application/json内容类型的JSON ,请使用request.get_json(force=True)

@app.route('/process_data', methods=['POST'])
def process_data():
    req_data = request.get_json(force=True)
    language = req_data['language']
    return 'The language value is: {}'.format(language)

To get JSON posted without the application/json content type, use request.get_json(force=True).

@app.route('/process_data', methods=['POST'])
def process_data():
    req_data = request.get_json(force=True)
    language = req_data['language']
    return 'The language value is: {}'.format(language)

回答 9

原始数据从WSGI服务器传递到Flask应用程序request.stream。流的长度在Content-Length标题中。

length = request.headers["Content-Length"]
data = request.stream.read(length)

通常,使用它更安全request.get_data()

The raw data is passed in to the Flask application from the WSGI server as request.stream. The length of the stream is in the Content-Length header.

length = request.headers["Content-Length"]
data = request.stream.read(length)

It is usually safer to use request.get_data() instead.


回答 10

要在JavaScript中使用jQuery发布JSON,请使用JSON.stringify转储数据并将内容类型设置为application/json

var value_data = [1, 2, 3, 4];

$.ajax({
    type: 'POST',
    url: '/process',
    data: JSON.stringify(value_data),
    contentType: 'application/json',
    success: function (response_data) {
        alert("success");
    }   
});

使用解析在Flask中request.get_json()

data = request.get_json()

To post JSON with jQuery in JavaScript, use JSON.stringify to dump the data, and set the content type to application/json.

var value_data = [1, 2, 3, 4];

$.ajax({
    type: 'POST',
    url: '/process',
    data: JSON.stringify(value_data),
    contentType: 'application/json',
    success: function (response_data) {
        alert("success");
    }   
});

Parse it in Flask with request.get_json().

data = request.get_json()

回答 11

要解析JSON,请使用request.get_json()

@app.route("/something", methods=["POST"])
def do_something():
    result = handle(request.get_json())
    return jsonify(data=result)

To parse JSON, use request.get_json().

@app.route("/something", methods=["POST"])
def do_something():
    result = handle(request.get_json())
    return jsonify(data=result)

回答 12

这是一个发布表单数据以将用户添加到数据库的示例。检查request.method == "POST"表单是否已提交。使用键request.form来获取表单数据。使用<form>其他方式呈现HTML模板。表单中的字段应具有name与中的键匹配的属性request.form

from flask import Flask, request, render_template

app = Flask(__name__)

@app.route("/user/add", methods=["GET", "POST"])
def add_user():
    if request.method == "POST":
        user = User(
            username=request.form["username"],
            email=request.form["email"],
        )
        db.session.add(user)
        db.session.commit()
        return redirect(url_for("index"))

    return render_template("add_user.html")
<form method="post">
    <label for="username">Username</label>
    <input type="text" name="username" id="username">
    <label for="email">Email</label>
    <input type="email" name="email" id="email">
    <input type="submit">
</form>

Here’s an example of posting form data to add a user to a database. Check request.method == "POST" to check if the form was submitted. Use keys from request.form to get the form data. Render an HTML template with a <form> otherwise. The fields in the form should have name attributes that match the keys in request.form.

from flask import Flask, request, render_template

app = Flask(__name__)

@app.route("/user/add", methods=["GET", "POST"])
def add_user():
    if request.method == "POST":
        user = User(
            username=request.form["username"],
            email=request.form["email"],
        )
        db.session.add(user)
        db.session.commit()
        return redirect(url_for("index"))

    return render_template("add_user.html")
<form method="post">
    <label for="username">Username</label>
    <input type="text" name="username" id="username">
    <label for="email">Email</label>
    <input type="email" name="email" id="email">
    <input type="submit">
</form>

回答 13

如果内容类型被识别为表单数据,request.data则将其解析为表单request.form并返回一个空字符串。

要获取原始数据,而不管内容类型如何,请调用request.get_data()request.data直接调用get_data(parse_form_data=True),而默认值为False直接调用。

If the content type is recognized as form data, request.data will parse that into request.form and return an empty string.

To get the raw data regardless of content type, call request.get_data(). request.data calls get_data(parse_form_data=True), while the default is False if you call it directly.


回答 14

如果将正文识别为表单数据,它将在中request.form。如果是JSON,它将位于中request.get_json()。否则,原始数据将在中request.data。如果不确定如何提交数据,可以使用or链来获取第一个包含数据的链。

def get_request_data():
    return (
        request.args
        or request.form
        or request.get_json(force=True, silent=True)
        or request.data
    )

request.args包含从查询字符串中解析出的args,无论主体是什么,因此get_request_data()如果它和主体都应同时进行数据处理,则可以将其删除。

If the body is recognized as form data, it will be in request.form. If it’s JSON, it will be in request.get_json(). Otherwise the raw data will be in request.data. If you’re not sure how data will be submitted, you can use an or chain to get the first one with data.

def get_request_data():
    return (
        request.args
        or request.form
        or request.get_json(force=True, silent=True)
        or request.data
    )

request.args contains args parsed from the query string, regardless of what was in the body, so you would remove that from get_request_data() if both it and a body should data at the same time.


回答 15

使用HTML表单发布表单数据时,请确保input标签具有name属性,否则它们将不会出现在中request.form

@app.route('/', methods=['GET', 'POST'])
def index():
    print(request.form)
    return """
<form method="post">
    <input type="text">
    <input type="text" id="txt2">
    <input type="text" name="txt3" id="txt3">  
    <input type="submit">
</form>
"""
ImmutableMultiDict([('txt3', 'text 3')])

只有txt3输入中有一个name,因此它是中唯一的键request.form

When posting form data with an HTML form, be sure the input tags have name attributes, otherwise they won’t be present in request.form.

@app.route('/', methods=['GET', 'POST'])
def index():
    print(request.form)
    return """
<form method="post">
    <input type="text">
    <input type="text" id="txt2">
    <input type="text" name="txt3" id="txt3">  
    <input type="submit">
</form>
"""
ImmutableMultiDict([('txt3', 'text 3')])

Only the txt3 input had a name, so it’s the only key present in request.form.


Flask-用于构建Web应用程序的Python微框架

Flask

Flask是一个轻量级的WSGI Web应用程序框架。它旨在使入门变得快速而简单,并且能够向上扩展到复杂的应用程序。它最初只是Werkzeug和JJJA_的一个简单包装器,现在已经成为最流行的Python Web应用程序框架之一

Fask提供建议,但不强制执行任何依赖项或项目布局。由开发人员选择他们想要使用的工具和库。社区提供了许多扩展,使添加新功能变得容易

 

一个简单的例子

使用pip_安装和更新:

$ pip install -U Flask

_PIP:https://pip.pypa.io/en/stable/quickstart/

贡献

代码-挡路::蟒蛇

# save this as app.py
from flask import Flask

app = Flask(__name__)

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

有关设置开发环境以及如何为Flask做出贡献的指导,请参阅贡献指南_

$ flask run
  * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

捐赠

_投稿指南:https://github.com/pallets/flask/blob/main/CONTRIBUTING.rst

托盘组织开发并支持Flask及其使用的库。为了扩大贡献者和用户的社区,让维护人员有更多的时间在项目上投入更多的时间,请今天就捐款。

链接

  • Documentation: https://flask.palletsprojects.com/
  • Changes: https://flask.palletsprojects.com/changes/
  • PyPI Releases: https://pypi.org/project/Flask/
  • Source Code: https://github.com/pallets/flask/
  • Issue Tracker: https://github.com/pallets/flask/issues/
  • Website: https://palletsprojects.com/p/flask/
  • Twitter: https://twitter.com/PalletsTeam
  • Chat: https://discord.gg/pallets