/
app.py
101 lines (88 loc) · 3.3 KB
/
app.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from flask import Flask, render_template, json, request, Response, jsonify
from flask.ext.pymongo import PyMongo
from mongoengine import connect
from flask.ext.mongoengine import MongoEngine
import subprocess
app = Flask(__name__)
# app.config['MONGO_HOST'] = '127.10.85.2'
# app.config['MONGO_PORT'] = 27017
# app.config['MONGO_DBNAME'] = 'runlinux'
app.config['MONGO_URI'] = 'mongodb://127.10.85.2:27017/runlinux'
app.config['MONGO_USERNAME'] = 'admin'
app.config['MONGO_PASSWORD'] = 'dp53nstHebqE'
mongo = PyMongo(app, config_prefix='MONGO')
# mongo = PyMongo(app) #for local MongDB
@app.route("/")
def main():
return render_template('index.html')
@app.route('/showSignUp')
def showSignUp():
return render_template('signup.html')
@app.route('/showSignIn')
def showSignIn():
return render_template('signin.html')
@app.route('/linuxCommand')
def linuxCommand():
return render_template('linuxCommand.html')
@app.route('/command', methods=['POST'])
def command():
if request.headers['Content-Type'] == 'application/json':
json = request.json
_linuxCommand = json.get("linuxCommand")
# validate the received values
if _linuxCommand:
comm = _linuxCommand.split(' ')
p = subprocess.Popen(comm,shell=False,stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE)
out,err = p.communicate()
if(out):
js = {'commandResponse' : out}
else:
js = {'commandResponse' : err}
return jsonify(js)
else:
return jsonify({'commandResponse' : 'Enter the required fields'})
else:
return jsonify({'commandResponse' : 'Cannot Find JSON type'})
@app.route('/signUp',methods=['POST'])
def signUp():
if request.headers['Content-Type'] == 'application/json':
json = request.json
_name = json.get("inputName")
_email = json.get("inputEmail")
_password = json.get("inputPassword")
reqJson = {"name" : _name,"email" : _email, "password" : _password}
if _email and _password and _name:
res = mongo.db.linuxCommand.find({'email' : _email})
if(res.count()>0):
return jsonify({'commandResponse' : 'user already exists'})
else:
abc = mongo.db.linuxCommand.save(reqJson)
return jsonify({'commandResponse' : 'user successfully registered','flag' : 1})
else:
return jsonify({'commandResponse' : 'Enter the required fields'})
else:
return jsonify({'commandResponse' : 'Cannot Find JSON type'})
@app.route('/signIn',methods=['POST'])
def signIn():
if request.headers['Content-Type'] == 'application/json':
json = request.json
_email = json.get("inputEmail")
_password = json.get("inputPassword")
if _email and _password:
res = mongo.db.linuxCommand.find({'email' : _email})
if(res.count()>0):
if(res[0].get("email") == _email and res[0].get("password") == _password):
return jsonify({'commandResponse' : 'user loggedin successfully', 'flag' : 1})
else:
return jsonify({'commandResponse' : 'Incorrect Credentials'})
else:
return jsonify({'commandResponse' : 'No User Found'})
else:
return jsonify({'commandResponse' : 'Enter the required fields'})
else:
return jsonify({'commandResponse' : 'Cannot Find JSON type'})
@app.route('/logout', methods=['GET'])
def logout():
return render_template('signin.html')
if __name__ == "__main__":
app.run()