好程式設計師Python培訓Python如何呼叫服務介面

好程式設計師發表於2020-11-06

好程式設計師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 = “”
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中即為引數,以空格分隔

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69913864/viewspace-2732694/,如需轉載,請註明出處,否則將追究法律責任。

相關文章