运行Python脚本的脚本

定时任务脚本
一 先启动crontab服务

service crond restart

二 写定时器(time.txt)

 crontab -e
 */1 * * * *  /opt/log_generator.sh

三 运行脚本
运行Python脚本的脚本

python /opt/generate_log.py

下面是Python脚本:

#coding=UTF-8
import random
import time

url_paths = [
    "class/112.html",
    "class/128.html",
    "class/145.html",
    "class/146.html",
    "class/131.html",
    "class/130.html",
    "learn/821",
    "course/list"
]

http_referers = [
    "http://www.baidu.com/s?wd={query}",
    "http://www.sogou.com/web?wd={query}",
    "http://cn.bing.com/search?wd={query}",
    "http://search.yahoo.com/search?wd={query}"
]

search_keyword = [
    "Spark Sql实战",
    "Hadoop 基础",
    "Storm 实战",
    "Spark Streaming 实战",
    "大数据面试"
]



ip_slices = [123, 156, 124, 10, 31, 29, 12, 167, 143, 187, 30, 45, 55, 63, 72, 87]

status_codes = ["200" , "404" , "500"]

def sample_url():
    return random.sample(url_paths, 1)[0]


def sample_ip():
    slice = random.sample(ip_slices, 4)
    return ".".join([str(item) for item in slice])

def sample_status():
    return random.sample(status_codes , 1)[0]

def sample_referer():
    if random.uniform(0,1) > 0.2:
        return "-"
    refer_str = random.sample(http_referers,1)
    query_str = random.sample(search_keyword,1)
    return refer_str[0].format(query=query_str[0])


def generate_log(count=10):
    time_str = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
    f = open("/opt/access.log","w+")
    while count >= 1:
        query_log = "{ip}\t{local_time}\t\"GET /{url} HTTP/1.1 \"\t{referer}\t{status}".format(url=sample_url(), ip=sample_ip(),referer=sample_referer(),status=sample_status(),local_time=time_str)
        print(query_log)
        f.write(query_log + '\n')
        count = count - 1


if __name__ == '__main__':
    generate_log()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值