python3.4.3 调用http接口 解析response xml后插入数据库
工作中需要调用一个http的接口,等不及java组开发,就试着用python去调用。Python版本3.4.3
完整的流程包括:从sqlServer取待调用的合同列表 -> 循环调用http接口 -> 将response写入xml文件 -> 解析xml -> 插入oracle数据库
要去公司旅游了,先把代码放着,以后再详细解释记录一下。
#!/usr/bin/python3 import time import pymssql import urllib.parse import httplib2 import os.path import xml.etree.ElementTree import cx_Oracle v_curr_datetime = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time())) print ('Step1: ' + v_curr_datetime) ##连接sqlServer,获得请求xml,写入txt文件######################### #conn_req = pymssql.connect(host="***",user="***",password="***",database="***",charset = "utf8") conn_req = pymssql.connect(host="***",user="",password="",database="ods",charset = "utf8") cur_req = conn_req.cursor() v_file_name_req = 'requestList.txt' f_req = open(v_file_name_req, 'w') f_req = open(v_file_name_req, 'r+') f_req.truncate() v_sql_req = "SELECT Loan_No,request_xml FROM if_haier_loan_contract_list" cur_req.execute(v_sql_req) rows = cur_req.fetchall() for row_req in rows: #row_req = row_req[0]+'||'+row_req[1]+'\n' row_req = row_req[0] + '||' + 'kfs;serv10000100056;' + row_req[1] + '\n' print(row_req) f_req.write(row_req) f_req.close() cur_req.close() conn_req.close() print ('Step2: ' + time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))) ##循环得到的xml文件,逐个调用还款计划接口,解析后存到数据库############## f_read = open('requestList.txt', 'r') for v_row_read in f_read: v_row_read = v_row_read.split('||') print ('当前调用xml: ' + v_row_read[1]) http = httplib2.Http() url = 'http://***' #body_str ='kfs;serv10000100056; <?xml version="1.0" encoding="gb2312" standalone="yes"?><msgbody><serviceId>serv10000100056</serviceId><GEN_GL_NO>1257393</GEN_GL_NO><LOAN_NO>HCF-CSCP20150708702904001</LOAN_NO><APP_USER_ID>01400515</APP_USER_ID><APP_USER_NAME>xxx</APP_USER_NAME><APP_USER_ORG_NO>0010</APP_USER_ORG_NO><ENQ_TYP>ALL</ENQ_TYP></msgbody>' body_str = v_row_read[1] headers = {'Content-type': 'application/x-www-form-urlencoded'} print ('*'*30) ##请求接口数据################# response, content = http.request(url, 'POST', headers=headers, body=body_str) ##将xml头的gb2312替换成utf-8### print ('*'*20) v_response = content.decode("utf-8").replace('encoding=\"gb2312\"','encoding=\"utf-8\"') ##将response生成xml文件####### v_file_name = v_row_read[0] + '.xml' f = open(v_file_name, 'w') f.truncate() f.write(v_response) f.write('\n') f.close() ##读取xml#################### root=xml.etree.ElementTree.parse(v_file_name) ##连接数据库################## connMAPP = cx_Oracle.connect("user","password","tnsname") cur1 = connMAPP.cursor() ##将数据写入数据库############# #book=root.findall('MX') book=root.iter('MX') for book_list in book: #print ('='*20) v_row = [] for note in book_list: v_temp = v_row.append(note.text) #v_row = v_row.split(',') #print (v_row) v_sql = " insert into temp_py values (%(v0)s,to_date(\'%(v1)s\','YYYY-MM-DD')) "%{'v0':v_row[0],'v1':v_row[1]} #print (v_sql) cur1.execute(v_sql) ##commit后关闭数据库连接 connMAPP.commit() cur1.close() connMAPP.close() print ('Step3: ' + time.strftime('%Y%m%d%H%M%S', time.localtime(time.time())))