利用python编写不同环境下都能运行的测试脚本
利用bash来获取当前电脑的环境变量,可以写一个.sh文件,里面获取当前环境,然后在调用python文件执行
# -*- coding: utf-8 -*- import logging
import urllib
import sys
import json
import string
import os
import time
import getopt
import requests sys.path.append('../') from common.monitor_logging import *
from common.util import *
from common.nagios_status import *
from hongbao_api.baidu_api import *
from common.monitor_config import *
from common.monitor_testcase import *
from common.pinger import * def Usage():
print "exception" DEBUG_FLAG=True
ROOT_DIR='' def get_user_opt():
global DEBUG_FLAG, ROOT_DIR
if len(sys.argv) > 1:
try:
opts, args = getopt.getopt(sys.argv[1:], 'dr:h')
if len(opts) == 0:
return for opt, arg in opts:
if opt in ('-h', '--help'):
Usage()
sys.exit(3)
elif opt == '-d':
DEBUG_FLAG = True
elif opt == '-r':
ROOT_DIR = arg except getopt.GetoptError:
Usage()
sys.exit(-1) ''' '''
class ScoreAuctionDealsUrl():
def __init__(self, testcase_file):
self.config = MonitorConfig()
#获取需要解析的xml的testcase的name
self.testcase = MonitorTestcase(testcase_file)
#日志文件
self.logger = logging.getLogger('baidu') #打印返回值和退出
def print_result_and_exit(self, status, msg):
print msg
sys.exit(status)
#检查主机是否联网
def check_host_is_alive(self):
pinger = Pinger()
ret = pinger.check()
return ret #开始检测url
def test_all_category(self):
#if not self.check_host_is_alive():
# msg = 'Network is not availabe'
# return STATE_UNKNOWN, msg
testcase_name = 'baidu'
url=self.testcase.get_url(testcase_name)
self.logger.debug('testcase=%s' % testcase_name)
self.logger.info('url=%s' % url) #请求数据
deals=GetBaiduDeals(url)
#请求json获取返回的状态
status = deals.get_baidu()
print("status:%s"%status)
#一个个遍历:
json_data=deals.json_data
print(json_data) return STATE_WARNING, "aaaa" if status == 0:
msg0 = 'return status is 0'
return STATE_WARNING, msg0
elif status ==1:
msg1 = 'return status is 1'
return STATE_WARNING,msg1
elif status ==2:
msg2 = 'return status is 2'
return STATE_CRITICAL,msg2
else:
msg3='return status is 3'
return STATE_UNKNOWN,msg3 if __name__ == '__main__': #######################################################
get_user_opt()
print(DEBUG_FLAG)
print(ROOT_DIR)
#
if DEBUG_FLAG:
print 'DEBUG_FLAG is True'
log_console_flag = True
else:
print 'DEBUG_FLAG is False'
log_console_flag = False if ROOT_DIR:
log_file = ROOT_DIR + '/log/monitor_log.txt'
else:
log_file= os.getcwd() + '/../../log/monitor_log.txt'
log_level = logging.DEBUG
set_logging(log_file, log_level, log_console_flag) logger = logging.getLogger("hongbao")
print(logger)
#
########################################################
logger.info('start...')
testcase_file = '../../test/hongbao.xml'
print(testcase_file)
score_auction_deals_url = ScoreAuctionDealsUrl(testcase_file)
print(score_auction_deals_url)
status, msg = score_auction_deals_url.test_all_category()
print(status,msg)
#
status_str = get_status_str(status)
output_msg = '%s, %s' % (status_str, msg)
print output_msg
exit(status)
#!/bin/sh _DEBUG=false # input env
shell_dir=$(cd "$(dirname "$0")"; pwd)
source ${shell_dir}/set_env.sh _DEBUG ROOT_DIR="$(dirname ${shell_dir})"
_log "ROOR_DIR=${ROOT_DIR}" #LOG_FILE=${ROOT_DIR}/log/report.log
#_log "LOG FILE=${LOG_FILE}" cd ${ROOT_DIR}/src/hongbao python baidu_url.py -r ${ROOT_DIR} $*
exit $?
利用python编写不同环境下都能运行的测试脚本的更多相关文章
- Python 利用Python编写简单网络爬虫实例3
利用Python编写简单网络爬虫实例3 by:授客 QQ:1033553122 实验环境 python版本:3.3.5(2.7下报错 实验目的 获取目标网站“http://bbs.51testing. ...
- Python 利用Python编写简单网络爬虫实例2
利用Python编写简单网络爬虫实例2 by:授客 QQ:1033553122 实验环境 python版本:3.3.5(2.7下报错 实验目的 获取目标网站“http://www.51testing. ...
- 利用Python编写Windows恶意代码!自娱自乐!勿用于非法用途!
本文主要展示的是通过使用python和PyInstaller来构建恶意软件的一些poc. 利用Python编写Windows恶意代码!自娱自乐!勿用于非法用途!众所周知的,恶意软件如果影响到了他人的生 ...
- linux环境下使用jmeter进行压力测试
linux环境下使用jmeter进行压力测试 linux环境下使用就meter进行压力测试: linux环境部署: 在Linux服务器先安装jdk: 2.以jdk-8u172-linux-x64.ta ...
- Windows环境下Oracle数据库的自动备份脚本
批处理文件(.bat) @echo off echo ================================================ echo Windows环境下Oracle数据 ...
- ubuntu16.04 ROS环境下配置和运行SVO
ubuntu16.04 ROS环境下配置和运行SVO https://blog.csdn.net/nnUyi/article/details/78005552
- Windows环境下Oracle数据库的自动备份脚本自动删除30天前的备份
@echo off echo ================================================ echo Windows环境下Oracle数据库的自动备份脚本 echo ...
- [Python学习] Linux环境下的Python配置,必备库的安装配置
1.默认Python安装情况 一般情况,Linux会预装Python的,版本较低,比如Ubuntu15的系统一般预装的是Python2.7.10. 使用命令:which python可以查看当前的py ...
- 利用Python编写网络爬虫下载文章
#coding: utf-8 #title..href... str0='blabla<a title="<论电影的七个元素>——关于我对电影的一些看法以及<后会无期 ...
随机推荐
- toast components
toast components https://jossmac.github.io/react-toast-notifications/ https://docs.microsoft.com/en- ...
- spring通过工厂模式解决页面耦合问题
spring通过工厂模式解决页面耦合问题
- 当给属性添加final 时候 则无法进行第二次值的修改
- BZOJ5105 CodePlus2017晨跑
这个题???我WA了两发??? #include<iostream> #include<cstdio> #include<cmath> #include<cs ...
- 怎么解决Xing欲
怎么解决Xing欲 来源:微信号 王路在隐身 这是知乎上的一道问题.原题叫<和尚怎么解决性欲>. 本来由出家人回答更合适,但估计出家人一般不太愿意回答. 我看了几十个答案,几乎都是在调侃出 ...
- Spring点滴二:Spring Bean
Spring Bean: 被称作bean的对象是构成应用程序的支柱,是由Spring Ioc容器管理.bean是一个被实例化,配置.组装并由Spring Ioc容器管理对象. 官网API:A Spri ...
- NOI前总结
最近也就是天天考试,总结一下. 7.1 开场T1T2都是不可做的概率期望,只有T3看起来可做,于是怒干4h+,将题解里面的所有结论都推出来了,大模拟写的一点毛病都没有,可还是因为2-SAT掌握不熟结果 ...
- Luogu 3369 我用线段树骗了一道平衡树题……
这篇博客毫无意义-- 只是表达一下我仍然会写树状数组和线段树-- 题目链接 #include <cstdio> #include <cstring> #include < ...
- bzoj 2300 : [HAOI2011]防线修建
set动态维护凸包 #include<iostream> #include<cstdio> #include<cstring> #include<algori ...
- Linux运维二:CentOS6.6系统安装后的基本配置与优化
CentOS6.6系统安装完成后还需要做一些配置与优化: 一:Linux内核版本号介绍 查看内核版本: [root@Gin scripts]# uname -r 2.6.32-504.el6.x86_ ...