Python flask框架post接口调用示例

作者:xujiang 时间:2021-01-24 17:06:47 

本文实例讲述了Python flask框架post接口调用。分享给大家供大家参考,具体如下:


from flask import Flask,render_template,request
app = Flask(__name__)
@app.route("/login",methods = ['POST','GET'])
def login():
 if request.method == "POST":
   username = request.form.get('username')
   password = request.form.get('password')
   print username
   print password
   return u'POST'+'+'+username+'+'+password
 if request.method == "GET":
   print 'call get now'
   username = request.args.get('username')
   password = request.args.get('password')
   print username
   print password
   return username
if __name__ == '__main__':
app.run(host='0.0.0.0',port=6000,debug=True)

中国[root@node01 flask]# curl 'http://192.168.137.1:6000/login?username=中国&password=密码'
中国[root@node01 flask]#
192.168.137.2 - - [13/Nov/2017 09:55:35] "GET /login?username=中国&password=密码 HTTP/1.1" 200 -
call get now
中国
密码

POST 调用:


use JSON;
my $ua = LWP::UserAgent->new;
$ua->agent("Mozilla/5.0 (Windows NT 6.1; rv:30.0) Gecko/20100101 Firefox/30.0");
my $cookie_jar = HTTP::Cookies->new(
  file=>'lwp_cookies.txt',
  autosave=>1,
  ignore_discard=>1);
  $ua->cookie_jar($cookie_jar);
 my $token_url= ' http://192.168.137.1:6000/login';
 my $res = $ua->post($token_url,
       {
       'username'=>'99999@xxxxx.com',
       'password'=>'1234567'
       });
 print $res->content();
 print "\n";

[root@node01 ~]#
[root@node01 ~]# perl flask.pl
POST+99999@xxxxx.com+1234567


from flask import Flask,render_template,request
app = Flask(__name__)
@app.route("/login",methods = ['POST','GET'])
def login():
 if request.method == "POST":
   username = request.form.get('username')
   password = request.form.get('password')
   print 'call post now'
   print username
   print password
   return u'POST'+'+'+username+'+'+password
 if request.method == "GET":
   print 'call get now'
   username = request.args.get('username')
   password = request.args.get('password')
   print username
   print password
   return username
if __name__ == '__main__':
app.run(host='0.0.0.0',port=6000,debug=True)

call post now
99999@xxxxx.com
1234567
192.168.137.2 - - [13/Nov/2017 10:03:56] "POST /login HTTP/1.1" 200 -

希望本文所述对大家基于flask框架的Python程序设计有所帮助。

来源:https://blog.csdn.net/zhaoyangjian724/article/details/78517437

标签:Python,flask,post
0
投稿

猜你喜欢

  • Centos7安装 mysql5.6.29 shell脚本

    2024-01-15 22:17:41
  • 解决python文件字符串转列表时遇到空行的问题

    2021-06-27 09:18:25
  • Python ORM框架Peewee用法详解

    2022-11-08 03:00:24
  • python实现矩阵和array数组之间的转换

    2022-03-19 16:31:21
  • 详解PHP中数组函数的知识点

    2023-05-29 10:59:11
  • 设定php简写功能的方法

    2024-05-13 09:25:21
  • Oracle密码文件的使用和维护第1/3页

    2010-07-30 12:43:00
  • Python 按比例获取样本数据或执行任务的实现代码

    2023-01-10 09:48:16
  • python3 使用openpyxl将mysql数据写入xlsx的操作

    2024-01-25 14:58:23
  • Python两台电脑实现TCP通信的方法示例

    2023-09-07 04:17:34
  • python之import机制详解

    2022-11-29 02:47:40
  • python实现ID3决策树算法

    2023-04-13 09:35:28
  • Python 使用with上下文实现计时功能

    2022-08-20 02:43:21
  • 什么是python的必选参数

    2022-12-04 07:28:36
  • python中defaultdict用法实例详解

    2022-08-09 17:01:10
  • Windows Server2008 R2 MVC 环境安装配置教程

    2024-01-17 06:45:24
  • MySQL 基础常用命令总结

    2024-01-22 16:35:40
  • 简单谈谈Python流程控制语句

    2023-03-12 12:34:25
  • python实现自动重启本程序的方法

    2022-07-18 14:16:19
  • Python实现JSON反序列化类对象的示例

    2023-09-03 19:29:51
  • asp之家 网络编程 m.aspxhome.com