python post请求,如何在Python中发送和接收HTTP POST请求

本文介绍了如何使用Python的requests库发送HTTP POST请求,并使用Flask框架接收并处理POST数据。客户端代码示例展示了如何通过requests库发送带有POST变量的数据,而服务器端的Flask应用则演示了如何读取并打印POST内容。
摘要由CSDN通过智能技术生成

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.

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

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

解决方案

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.

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.

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.

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.

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值