python http请求post参数,json解析
2014-06-09 17:57
811 查看
python的访问网络太简单了
下面是python的测测试 代码
测试结果:
下面是python的测测试 代码
# -*- coding: utf-8 -*- import sys,json import urllib,urllib2 uri = 'http://localhost/test_post_get.php' params = { '_c': 'user',#post参数有两种写法 '_m': 'info', }; print sys.getdefaultencoding() params['dotype']= 1 params['username'] = '全仔' params = urllib.urlencode(params) ret = urllib.urlopen(uri, params) code = ret.getcode() print code #状态码 ret_data = ret.read() print ret_data.decode('utf-8') print json.dumps(ret_data) dic = json.loads(ret_data) print dic print dic['username']附上服务端的PHP代码:(随便写的。。。。)
<?php $from_py_username = $_POST['username']; $dotype = $_POST['dotype']; $u = $_POST['_c']; if($dotype == 0) { $res = array( "id"=> rand(0,100), "username"=>$from_py_username, "time"=>time(), "dotype"=>$dotype, "code"=>100 ); echo JSON($res); }else if($dotype == 1) { $res = array( "id"=> rand(0,100), "username"=>$from_py_username, "time"=>time(), "dotype"=>$dotype, "c"=>$u, "code"=>100 ); echo JSON($res); }else { $res = array( "id"=> rand(0,100), "username"=>$from_py_username, "time"=>time(), "code"=>101 ); echo JSON($res); } function arrayRecursive(&$array, $function, $apply_to_keys_also = false) { foreach ($array as $key => $value) { if (is_array($value)) { arrayRecursive($array[$key], $function, $apply_to_keys_also); } else { $array[$key] = $function($value); } if ($apply_to_keys_also && is_string($key)) { $new_key = $function($key); if ($new_key != $key) { $array[$new_key] = $array[$key]; unset($array[$key]); } } } } function JSON($array) { arrayRecursive($array, 'urlencode', true); $json = json_encode($array); return urldecode($json); } ?>
测试结果:
相关文章推荐
- python实现的json数据以HTTP GET,POST,PUT,DELETE方式页面请求
- python实现的json数据以HTTP GET,POST,PUT,DELETE方式页面请求
- 接口测试类(http,post请求)参数 json格式
- Python发送http请求解析返回json的实例
- Netty4 HTTP请求参数解析(GET, POST)
- Netty4 HTTP请求参数解析(GET, POST)
- python任务-从文件读取参数来发送json格式的post请求,再讲请求结果保存到文档里
- python实现的json数据以HTTP GET,POST,PUT,DELETE方式页面请求
- http get/post请求及json解析
- http post 请求参数格式是json
- python实现的json数据以HTTP GET,POST,PUT,DELETE方式页面请求
- python实现的json数据以HTTP GET,POST,PUT,DELETE方式页面请求
- Netty学习笔记之一(Netty解析简单的Http Post Json 请求)
- Jmeter中通过beanshell脚本解析HTTP请求的返回的json并作为下个请求的输入参数
- 使用apache的httpclient发送post请求传送和解析json
- python实现的json数据以HTTP GET,POST,PUT,DELETE方式页面请求
- Android HttpURLconnection使用POST请求参数是json
- Http Post请求提交json格式数据工具类,解决请求参数中文乱码问题
- python实现的json数据以HTTP GET,POST,PUT,DELETE方式页面请求
- Python发送http请求解析返回json小demo