通信接口测试(json)

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#!/usr/bin/env python
#  -*- coding:utf-8 -*-
# File post.py

import urllib
import urllib2
import json
    
def post(action,values):
	try:
		#此处url自行修改
		url="http://server_path"+action+"suffix"
		jdata = json.dumps(values,sort_keys=True,indent=4,ensure_ascii=False)
		print jdata
		req = urllib2.Request(url, jdata)
		response = urllib2.urlopen(req)
		str = response.read()
		print json.dumps(json.loads(str),sort_keys=True,indent=4,ensure_ascii=False) 
	except:
		print "Error Action:",action

	finally:
		print ""

#使用示例
post(
	"/goods/getPrice",
	{
		"goodsId":"52a255084a824d98014a83f56d2b0000",
		"speValueId1":"52a255084ac3736d014ac39e6ef5015b",
		"speValueId2":"",
		"speValueId3":""
	})