-
Notifications
You must be signed in to change notification settings - Fork 0
/
createSession.py
79 lines (62 loc) · 2.58 KB
/
createSession.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from config import WEBLAB_USERNAME, WEBLAB_PASSWORD, APPLICATION_ROOT, PORT
import json, requests
from time import strftime
import os
def main():
ip= 'localhost:'+str(PORT)+APPLICATION_ROOT
username="Tester"
# try:
session_id,url = createSession(username,ip)
option = 0
while option in (0,1,2):
print 'Session ID: '+ session_id
print 'URL: '+ url +'\n'
print '1. Check user status'
print '2. Kick user out'
print 'Other. Exit \n'
option = input('Option: ')
if option == 1: checkStatus(ip, session_id)
elif option == 2: kickOut(ip, session_id)
else: print 'Goodbye'
# except:
# print 'labSessionCreator.py -u <username> -s <server_ip_adress>'
def createSession(user,ip):
client_initial_data = {
'back':'http://weblab.deusto.es',
'url':'http://'+ip+'/weblab/sessions/'
}
server_initial_data = {
'priority.queue.slot.start': strftime("%Y-%m-%d %H:%M:%S")+'.000',
'priority.queue.slot.length': 200,
'request.username': user
}
serialized_client_initial_data = json.dumps(client_initial_data)
serialized_server_initial_data = json.dumps(server_initial_data)
back_url = json.loads(serialized_client_initial_data).get('back','')
url = json.loads(serialized_client_initial_data).get('url','')
data = {
'client_initial_data' : serialized_client_initial_data,
'server_initial_data' : serialized_server_initial_data,
'back' : back_url,
}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain','authorization':WEBLAB_USERNAME+':'+WEBLAB_PASSWORD}
resp = requests.post(url, data=json.dumps(data), headers=headers, auth=(WEBLAB_USERNAME,WEBLAB_PASSWORD))
url=json.loads(resp.content).get('url','')
session_id = json.loads(resp.content).get('session_id','')
os.system('nohup google-chrome '+url+ ' &')
os.system('clear')
return session_id, url
def checkStatus(ip, session_id):
os.system('clear')
url = 'http://'+ ip +'/weblab/sessions/'+ session_id + '/status'
resp = requests.get(url, auth=(WEBLAB_USERNAME,WEBLAB_PASSWORD))
print 'Response: '+ resp.text
def kickOut(ip, session_id):
os.system('clear')
url = 'http://'+ ip +'/weblab/sessions/'+ session_id
data = {'action' : "delete"}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
resp = requests.post(url, data=json.dumps(data), headers=headers, auth=(WEBLAB_USERNAME,WEBLAB_PASSWORD))
print 'Response: '+ resp.text
if __name__ == "__main__":
main()