近期有喜欢Python开发的小伙伴询问扣丁学堂Python培训老师Python如何调用服务接口?关于Python如何调用服务接口这个问题曾经也有不少的小伙伴询问过,本篇文章小编就给读者们实例介绍一下Python如何调用服务接口,希望对小伙伴有帮助。
下面上代码:
#! /usr/bin/env python
# coding=utf-8
######################################################################
# Author: yini.xie
# Create Time: 2016-07-05 16:28:42
# Descriptioin:
######################################################################
import os
import sys
import time
import json
import urllib
import urllib2
from urllib import quote
from datetime import datetime
pathATM = "http://192.168.217.217:4080/invoke.json"
def fmtLen(value, length=7):
fmt = '{0:>%d}' % (length)
return fmt.format(value)
def changeWithdrawRule(customerId, productCode, ruleTemplateId, ruleId, operator):
service_url_s = "http://service.xxx.com/tsService/TSRuleService_1.0.0"
method_s = "bindCustomerWithdrawRuleTemplate"
params_s = "parameters[]=%d¶meters[]=%d¶meters[]=%d¶meters[]=%d¶meters[]=%s" % (customerId, productCode, ruleTemplateId, ruleId, operator)
params_s += "&url=%s&method=%s¶meterTypes[]=int¶meterTypes[]=int¶meterTypes[]=int¶meterTypes[]=int¶meterTypes[]=java.lang.String" % (service_url_s, method_s)
url_s = pathATM + "?" + quote(params_s, safe='&=')
data_s = urllib2.urlopen(url_s).read()
print url_s
return json.loads(data_s)
if __name__ == '__main__':
start = datetime.now()
for line in open(sys.argv[1]).xreadlines():
fields = line.strip().split()
customerId = int(fields[0])
productCode = int(fields[1])
ruleTemplateId = int(fields[2])
ruleId = int(fields[3])
print "start to deal customerId = " + str(customerId)
changeWithdrawRule(customerId, productCode, ruleTemplateId, ruleId, "Case")
end = datetime.now()
运行Python脚本,即可调用相应的接口修改数据库数据。
python ChangeCustomerRule.py text.txt
text.txt中即为参数,以空格分隔
最后想要了解更多关于Python和人工智能方面内容的小伙伴,请关注扣丁学堂Python培训官网、微信等平台,扣丁学堂IT职业在线学习教育平台为您提供权威的Python开发环境搭建视频,Python培训后的前景无限,行业薪资和未来的发展会越来越好的,扣丁学堂老师精心推出的Python视频教程定能让你快速掌握Python从入门到精通开发实战技能。
【关注微信公众号获取更多学习资料】 【扫码进入Python全栈开发免费公开课】
网友评论