根据文本中每一行数据生成json再进行url访问

# -*- coding: utf-8 -*-



Usage:
add-post <session> <inputfile> 
"""

from docopt import docopt
import requests
import json
import os
import time
from datetime import datetime

if __name__ == '__main__':
    # 将绑定交互参数
    arguments = docopt(__doc__)
    session = arguments['<session>']
    inputfile = arguments['<inputfile>']
    url = "http://xxx"

    # print(os.getcwd())
    if os.path.isfile(inputfile) == False:
        print("找不到此文件:{}".format(inputfile))
        exit(0)
    # Content-Type类型需要设置为:application/json
    headers = {"Content-Type": "application/json", "Cookie": "CNAF_SESSIONID_V2017={}".format(session)}
    a = datetime.now()
    with open(inputfile, mode='r', encoding='gbk') as f:
        for line in f:
            # split()默认以空格分隔,此处根据文档中数据进行分隔
            list_line = line.split()
            time.sleep(1)
            data_str = {'name': list_line[0], 'remark': list_line[1]}
            # 需要将str文件转换成json对象
            r = requests.post(url, json.dumps(data_str), headers=headers)
            print(r.text)
    b = datetime.now()
    # print("{}:{}".format(a, b))
    print('Cost {} seconds'.format((b - a).seconds))

浏览器原生态form表单提交application/x-www-form-urlencoded

# -*- coding: utf-8 -*-


"""
Usage:
miguQuery <session> <class_id> <inputfile> <outputfile>
"""

from docopt import docopt
import requests
import json
import os
import time
from datetime import datetime

if __name__ == '__main__':
    # 将绑定交互参数
    arguments = docopt(__doc__)
    session = arguments['<session>']
    class_id = arguments['<class_id>']
    inputfile = arguments['<inputfile>']
    outputfile = arguments['<outputfile>']
    url = "http://xxx"

    if os.path.isfile(inputfile) == False:
        print("找不到此文件:{}".format(inputfile))
        exit(0)
    if os.path.isfile(outputfile) == True:
        print("文件: {} 已经存在,请保存为另一个名字".format(outputfile))
        exit(0)

    headers = {"Cookie": "CNAF_SESSIONID_V2017={}".format(session)}
    a = datetime.now()
    with open(inputfile, mode='r', encoding='utf-8') as f:
        for line in f:
            # split()默认以空格分隔,此处根据文档中数据进行分隔
            time.sleep(1)
            data_str = {'classId': class_id, 'content': line}
            r = requests.post(url, data_str, headers=headers)
            print(r.text)
    b = datetime.now()
    # print("{}:{}".format(a, b))
    print('Cost {} seconds'.format((b - a).seconds))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值