Blame view

simulators/user/userSimulator.py 2.7 KB
3c179769   Jeremy   userSimulator / a...
1
2
3
4
5
6
7
8
9
import sys
import os
import datetime
import time
import json
import requests
from requests.auth import HTTPBasicAuth

class UserSimulator():
605b65e5   Jeremy   Update simulators
10
11
    conf_file = "../config/conf.json"
    conf_path = "../config/"
3c179769   Jeremy   userSimulator / a...
12
    request_path = "../resources/"
4816e86b   Jeremy   removed *.sh *.ba...
13
    login = "http://localhost:8000/user_manager/login"
3c179769   Jeremy   userSimulator / a...
14
15
16
17
18
    url = "http://localhost:8000/routine_manager/import_request"
    get_url = "http://localhost:8000/routine_manager/"
    sims = []
    ended = 0

b6b0fd47   Jeremy   Added parameters ...
19
20
    def userPrint(self, string: str):
        print("UserSimulator : " + string)
3c179769   Jeremy   userSimulator / a...
21

65149de7   Jeremy   Update
22
    def __init__(self, argv):
3c179769   Jeremy   userSimulator / a...
23
24
        if (len(argv) > 1):
            self.conf_file = self.conf_path + argv[1]
65149de7   Jeremy   Update
25
            print("conf_file = " + self.conf_file)
3c179769   Jeremy   userSimulator / a...
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
        else:
            self.userPrint("Using the default configuration file : conf.json")

    def parse(self):
        try:
            json_data = open(self.conf_file, 'r')
        except IOError:
            self.userPrint("Configuration file not found")
            sys.exit(0)
        json_content = json.loads(json_data.read())
        for dic in json_content:
            for key, value in dic.items():
                if (key == "userSimulator"):
                    self.sims.append(dic)
                    self.ended = max(dic["time"], self.ended)
        return (0)

b6b0fd47   Jeremy   Added parameters ...
43
    def sendRequest(self, file_name: str):
3c179769   Jeremy   userSimulator / a...
44
45
46
47
48
49
50
51
52
53
        files = {'request_file' : open(self.request_path + file_name, 'rb')}
        r = self.client.post(self.url, files=files)
        if (r.status_code == 200):
            self.userPrint("The request has been successfully sent")
        else:
            self.userPrint("Sending request failed %d"%(r.status_code))
        return (0)

    def authenticate(self):
        self.client = requests.session()
4816e86b   Jeremy   removed *.sh *.ba...
54
55
56
57
58
59
60
61
62
63
        try:
            self.client.get("http://localhost:8000/user_manager/login")
            csrftoken = self.client.cookies['csrftoken']
            self.logindata = dict(email="pyros", password="DjangoPyros", csrfmiddlewaretoken=csrftoken)
            resp = self.client.post(self.login, data=self.logindata, headers=dict(Referer=self.login))
            self.userPrint("Connection status : %d"%(resp.status_code))
        except Exception as e:
            print(e, file=sys.stderr)
            return 1
        return 0
3c179769   Jeremy   userSimulator / a...
64
65
66
67

    def run(self):
        i = 0
        self.parse()
4816e86b   Jeremy   removed *.sh *.ba...
68
69
        if self.authenticate():
            return 1
3c179769   Jeremy   userSimulator / a...
70
71
72
73
74
75
76
77
78
79
80
        self.userPrint("The simulator will end in %d seconds"%(int(self.ended)))
        while (i < self.ended):
            for dic in self.sims:
                if (int(dic["time"]) == i):
                    self.sendRequest(dic["userSimulator"])
            i += 1
            time.sleep(1)
        return (0)

if (__name__ == "__main__"):
    sim = UserSimulator(sys.argv)
4816e86b   Jeremy   removed *.sh *.ba...
81
    sys.exit(sim.run())