如何在Python中发送和接收HTTP POST请求 [英] How to send and receive HTTP POST requests in Python

查看:5189
本文介绍了如何在Python中发送和接收HTTP POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个简单的客户端方法,它可以在HTTP POST请求中发送布尔值,以及一个侦听的服务器端函数,并且可以将POST内容保存为var。

I need a simple Client-side method that can send a boolean value in a HTTP POST request, and a Server-side function that listens out for, and can save the POST content as a var.

我无法找到有关如何使用 httplib 的信息。

I am having trouble finding information on how to use the httplib.

请给我一个简单的例子,使用localhost作为http连接。

Please show me a simple example, using localhost for the http connection.

推荐答案

对于客户端,你可以做各种各样的使用此python库的请求:请求。它非常直观,易于使用/安装。

For the client side, you can do all sorts of requests using this python library: requests. It is quite intuitive and easy to use/install.

对于服务器端,我建议你使用像 Flask 瓶子龙卷风。这些是非常容易使用和轻量级的。

For the server side, I'll recommend you to use a small web framework like Flask, Bottle or Tornado. These ones are quite easy to use, and lightweight.

例如,一个小的客户端代码发送post变量 foo 使用请求看起来像这样:

For example, a small client-side code to send the post variable foo using requests would look like this:

import requests
r = requests.post("http://yoururl/post", data={'foo': 'bar'})
# And done.
print(r.text) # displays the result body.

使用烧瓶接收和使用POST请求的服务器端代码如下所示:

And a server-side code to receive and use the POST request using flask would look like this:

from flask import Flask, request
app = Flask(__name__)
@app.route('/', methods=['POST'])
def result():
    print(request.form['foo']) # should display 'bar'
    return 'Received !' # response to your request.

这是最简单的&使用python发送/接收POST请求的最快方法。

This is the simplest & quickest way to send/receive a POST request using python.

这篇关于如何在Python中发送和接收HTTP POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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