Requests Shooter

Shoot requests in multithread-mode and insert results to MongoDB .

13

Votes

import os
import urllib2
import sched
import time
import logging
import requests
import datetime
import os
import sys
from pymongo import MongoClient
import json
from ConfigParser import SafeConfigParser
import random
import string
import urllib2
import threading



#--- Request Class  ---#
class Request:
    def __init__(self,sessionId,ts):
        self.sessionId=sessionId
        self.ts=ts

#--- Shoot the request object ---#

    def shootRequest(self):
        global requests_list
#------- Colors ------------#
        OKGREEN = '\033[92m'
        FAIL = '\033[91m'
        ENDC = '\033[0m'
        BOLD = '\033[1m'
        UNDERLINE = '\033[4m'
#------- Start Shooting ------#
        sessionId=self.sessionId
        ts=self.ts
        LOG_FILE = '/var/log/app.log'
        logging.basicConfig(filename=LOG_FILE,level=logging.DEBUG)
        logging.debug(str(datetime.datetime.now())+' | Runing shoot() func.')
        try:
            start = time.time()
            response=urllib2.urlopen('&ts='+str(ts)+'&clientSessionId='+str(sessionId))
            end = time.time()
            status_code=response.getcode()
            response.close()
            response_time=(end-start)*1000
        except urllib2.URLError,e:
            logging.debug(" ### EXCEPTION = %r "+str(e))
            status_code="408"
            response_time='timeout'
        request_data = '{"sessionId":"'+str(sessionId)+'","TS":"'+str(ts)+'","status_code":"'+str(status_code)+'","response_time":"'+str(response_time)+'","check":"0","found_ts":"0"}'
        logging.debug('-------------------------------------------------------\n'+str(datetime.datetime.now())+' | '+request_data)
        j=json.loads(request_data)
        requests_list.append(j)
        if len(requests_list) > 100:
            insertMongo()
        logging.debug(str(datetime.datetime.now())+str('| INSERTED REQUEST_DATA INTO MONGODB (COLLECTION="REQUESTS")'))
        #t_list()
        return None


#--- Threads List ---#
def t_list():
#------- Colors ------------#
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    UNDERLINE = '\033[4m'
    c=0
    for t in threading.enumerate():
        print (FAIL+str(t)+ENDC)
        c=c+1
    print (FAIL+UNDERLINE+"COUNT = "+str(c)+ENDC)
    return


#--- Insert the response detaild into Mongo ---#

def insertMongo():
    global requests_list
    list_to_insert = requests_list
    requests_list = []
    client = MongoClient('localhost', 27017)
    db = client.shooter
    print list_to_insert
    db.requests.insert_many(list_to_insert)
    client.close()
    return None


#--- Start the thread in scheduler (1 thread per 1 sec) ---#

def startThread(sc):
    sessionId=id_generator()
    nj_req=Request('nj',sessionId,int(time.time()))
    sessionId=id_generator()
    va_req=Request('va',sessionId,int(time.time()))
    nj_t = threading.Thread(target=nj_req.shootRequest)
    #nj_t.setDaemon(True)
    print (nj_t)
    nj_t.start()
    #nj_t.join(1)
    va_t = threading.Thread(target=va_req.shootRequest)
    #va_t.setDaemon(True)
    print (va_t)
    va_t.start()
    #va_t.join(1)
    sc = sched.scheduler(time.time, time.sleep)
    sc.enter(1, 1, startThread, (sc,))
    sc.run()


#--- This func generate the sessionId ---#

def id_generator(size=24, chars=string.ascii_uppercase):
    return ''.join(random.choice(chars) for _ in range(size))

if __name__ == '__main__':
    requests_list = []
    s = sched.scheduler(time.time, time.sleep)
    limit=999999999
    sys.setrecursionlimit(limit)
    startThread(s)
    #s.enter(1, 1, startThread, (s,))
    #s.run()

Vote Here

You must earn at least 1 vote on your snippets to be allowed to vote

Terms Of Use

Privacy Policy

Featured snippets are MIT license

Gears & Masters

Advertise

DevOpsnipp.com © 2020

medium.png