欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python调用服务接口的实例

程序员文章站 2022-05-18 22:37:10
如下所示: #! /usr/bin/env python # coding=utf-8 ###################################...

如下所示:

#! /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&parameters[]=%d&parameters[]=%d&parameters[]=%d&parameters[]=%s" % (customerid, productcode, ruletemplateid, ruleid, operator)
  params_s += "&url=%s&method=%s&parametertypes[]=int&parametertypes[]=int&parametertypes[]=int&parametertypes[]=int&parametertypes[]=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调用服务接口的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。