测试api是否可用

回复 收藏

shell2.jpg

2017-01-11 13:47 举报
已邀请:
0

北辰星

赞同来自:

学习

0

ggangelo

赞同来自:

import requests
from concurrent.futures import ThreadPoolExecutor

import time

def get_list(config: str) -> list:
    urlpattens = []
    with open(config, mode='rt', encoding='utf8') as f:
        url, port = f.readline().strip('\n').split()
        for testing in f:
            urlpattens.append((url, port, testing.rstrip('\n')))
    return urlpattens

def load_url(pattens):
    url, port, patten = pattens
    paths, method, kname, vname, kpasswd, vpasswd = patten.split()
    reques_url = url + ':' + port if port else url
    if method.lower() == 'get':
        q = requests.get(reques_url + paths)
    else:
        parms = {kname: vname, kpasswd: vpasswd}
        q = requests.post(reques_url + paths, data=parms)
    if q.status_code != 200:
        times = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        return f"{times} {paths} {method} {q.status_code} {kname} {vname} {kpasswd} {vpasswd}"
    return True

def write_err(log_file: str, logs_table: list) -> None:
    with open(log_file, mode='w', encoding='utf8') as f:
        for line in logs_table:
            f.writelines(line + '\n')

if __name__ == '__main__':
    cfg = './config'
    check_table = get_list(cfg)
    err_list = []
    with ThreadPoolExecutor(max_workers=10) as executor:
        threads = [executor.submit(load_url, patten) for patten in check_table]
        for thread in threads:
            try:
                date = thread.result()
            except Exception as exc:
                err_list.append(exc)
            else:
                if date:
                    err_list.append(date)
    write_err('./error-log', err_list)

使用futures 库来增加多线程操作。接收并生成错误列表后再由主线程统一写入error-log文件,python 3.6.2

0

ggangelo

赞同来自:

#!/bin/bash

url=$(grep http ./config|awk '{print $1}')
port=$(grep http ./config|awk '{print $2}')
POST_LIST=($(grep POST ./config))
GET_LIST=($(grep GET ./config))

if [ -z ${port} ];then
    reqs_url="${url}:${port}/"
else
    reqs_url="${url}/"
fi

for lines in ${POST_LIST};do
    paths=$(echo ${lines}|awk '{print $1}')
    kuser=$(echo ${lines}|awk '{print $3}')
    vuser=$(echo ${lines}|awk '{print $4}')
    kpass=$(echo ${lines}|awk '{print $5}')
    vpass=$(echo ${lines}|awk '{print $6}')
    headers=$(curl -sI -H "Content-Type: application/x-www-form-urlencoded" -X POST -d "${kuser}=${vuser}&${kpass}=${vpass}" "${reqs_url}${paths}")
    status=$(echo "${headers}"|awk '/HTTP/{print $2}')
    if [ ${status} != "200" ];then
        now=$(date +"%F %T")
        echo "${now} ${paths} POST ${status} ${kuser} ${vuser} ${kpass} ${vpass}" >> error-log
    fi

for lines in ${GET_LIST};do
    paths=$(echo ${lines}|awk '{print $1}')
    headers=$(curl -sI "${reqs_url}${paths}")
    status=$(echo "${headers}"|awk '/HTTP/{print $2}')
    if [ ${status} != "200" ];then
        now=$(date +"%F %T")
        echo "${now} ${paths} GET ${status}" >> error-log
    fi

shell 的代码。想法是用grep 过滤出需要的行来进行分组,GET分为一组,POST分为一组。并保存到不同的元组中,然后使用两个for循环来处理。

0

t0ny1988

赞同来自:

for 循环遍历的时候会有问题吧。

#!/bin/bash

###定义变量

url=`grep 'http' /tmp/config.txt | awk '{print $1}'`

port=`grep 'http' /tmp/config.txt | awk '{print $2}'`

###chek port

if [ -z $port ];then

  request_url="$url/"

else

  request_url="$url:$port/"

fi

###post

grep 'POST' /tmp/config.txt | while read pl;do

  location=`echo $pl | awk '{print $1}'`

  name=`echo $pl | awk '{print $3}'`

  vname=`echo $pl | awk '{print $4}'`

  passwd=`echo $pl | awk '{print $5}'`

  vpasswd=`echo $pl | awk '{print $6}'`

  post=`curl -sI -d \"$name=$vname\&$passwd=$vpasswd\" \"$request_url$location\"`

  post_code=`echo $post | awk '/HTTP/{print $2}'`

if [ "$post_code" != "200" ];then

  date=`date +"%F %T"`

  echo "$date $localtion POST $post_code $name $vname $passwd $vpasswd" >> /tmp/error-log

fi

done

###get

grep 'GET' /tmp/config.txt | while read gl;do

  location=`echo $gl | awk '{print $1}'`

  get=`curl -sI "$request_url$location"`

  get_code=`echo $get | awk '/HTTP/{print $2}'`

if [ "$get_code" != "200" ];then

  date=`date +"%F %T"`

  echo "$date $location GET $get_code" >> /tmp/error-log

fi

done

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: