PHP前端开发

python脚本如何模拟接口实现批量用户开通(代码)

百变鹏仔 2个月前 (02-06) #Python
文章标签 批量

本篇文章给大家带来的内容是关于python脚本如何模拟接口实现批量用户开通(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

1、目的

通过模拟接口方法,实现批量用户开通

2、分析

A、接口含body和head部分,其中body中的某些变量为必填字段,包含用户的信息。

立即学习“Python免费学习笔记(深入)”;

B、用户信息清单可以整理成ott_after_check_device文件。

C、将ott_after_check_device文件转换成列表数据类型,将其用户信息对应替换到body.xml文件中。

3、脚本实现

#!/usr/bin/env python# -*- coding: utf-8 -*-import requestsfrom requests_toolbelt.multipart.encoder import MultipartEncoderdef get_txt_after_check_device():    ott_after_check_device = '.\ott_after_check_device.txt'    f = open(ott_after_check_device)    lines = f.readlines()    all_list_device = []    for line in lines:        line = line.replace("", '')        list_device = line.split(',')        all_list_device.append(list_device)    return all_list_devicedef ott_boss(list_device):    sleep_time = 0.001    # print len(list_device)    for i in range(len(list_device)):        print u'新开户数 :', i + 1        time.sleep(sleep_time)        acc_num = list_device[i][0]        stb_id = list_device[i][1]        print 'STBID : ', stb_id        account = list_device[i][2]        url = 'http://10.2.214.133:6600/oss/rest/mango/bossManagement/syncOrder'        mul = MultipartEncoder(                fields={                    'xmlhead': '<?xml  version="1.0" encoding="UTF-8"?> <interboss>'                               '<version>0100</version>'                               '<testflag>0</testflag>'                               '<biptype>'                               '<bipcode>IPTVB412</bipcode>'                               '<activitycode>T2101057</activitycode>'                               '<actioncode>0</actioncode>'                               '</biptype>'                               '<routinginfo>'                               '<origdomain>BOSS</origdomain>'                               '<routetype>00</routetype>'                               '<routing>'                               '<homedomain>OTT</homedomain>'                               '<routevalue>210</routevalue>'                               '</routing>'                               '</routinginfo>'                               '<transinfo>'                               '<sessionid>2018092517323481311686</sessionid>'                               '<transido>2018092517323416388122</transido>'                               '<transidotime>20180211173234</transidotime>'                               '</transinfo> </interboss>',                    'xmlbody': '<?xml  version="1.0" encoding="UTF-8"?> <interboss>     <svccont> <ordsynreq>'                               '<pkgseq>73120180111000007</pkgseq>'                               '<recnum>1</recnum>'                               '<ud1>'                               '<idtype>01</idtype>'                               '<idv>%s</idv>'                               '<brand>09</brand>'                               '<opr>06</opr>'                               '<oprt>20180925171922</oprt>'                               '<biztype>52</biztype>'                               '<seq>8121</seq>'                               '<broadbandid>738815023717</broadbandid>'                               '<zipcode></zipcode>'                               '<address></address>'                               '<username></username>'                               '<usersex></usersex>'                               '<idcardtype></idcardtype>'                               '<idcardnum></idcardnum>'                               '<areacode>K381</areacode>'                               '<groupid></groupid>'                               '<pocont>'                               '<acttype>1</acttype>'                               '<stbid>%s</stbid>'                               '<account>%s</account>'                               '<password>111111</password>'                               '<chrgtype>2</chrgtype>'                               '<effetitime>20180925171922</effetitime>'                               '<channel>08</channel>'                               '<spid>mango</spid>'                               '<bizcode>defaultBasicProduct</bizcode>'                               '<bizkind>01</bizkind>'                               '</pocont>'                               '</ud1> </ordsynreq> '                               ']]&gt;</svccont> </interboss>' % (acc_num, stb_id, account)                }        )        header = {'Content-Type': mul.content_type}        body = mul        response = requests.post(url, data=body, headers=header)        print response.content        print response.status_codeif __name__ == '__main__':    ott_boss(get_txt_after_check_device())