forked from OpenShiftDemos/os-sample-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
126 lines (116 loc) · 2.93 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from flask import Flask,request
from flask_apscheduler import APScheduler
import os,time,datetime,subprocess
app = Flask(__name__)
workdir='/sdcard/'
def job1():
print('------- start job --------\n')
print(datetime.datetime.now())
f=workdir+'nn.py'
if os.path.exists(f):
ret=subprocess.call('python /sdcard/nn.py',shell=True)
print(ret)
else:
print('err:nn.py not exists')
print(datetime.datetime.now())
print('------ end job -------')
sch = APScheduler()
sch.api_enabled = True
sch.init_app(app)
@app.route("/")
def hello():
return "Hello Worldgggghhhgg!"
@app.route('/addjob')
def addjob():
if not sch.running:
sch.start()
d1=datetime.datetime.now()
d2=d1+datetime.timedelta(seconds=20)
sch.add_job(func=job1,id='job1',trigger='date',run_date=d2)
return 'add job susscessful'
@app.route('/deljob')
def deljob():
jobs=sch.get_jobs()
if len(jobs)>0:
sch.pause_job('job1')
sch.delete_job('job1')
return 'del job susscessful'
@app.route("/file/dir")
def dir():
ret=''
try:
ret=str(os.listdir(workdir))
except:
ret='error'
return ret
@app.route("/file/listdir/<src>")
def listdir(src):
ret=''
zsrc=workdir+src
try:
ret=str(os.listdir(zsrc))
except:
ret='error'
return ret
@app.route('/file/remove/<src>')
def remove(src):
ret=''
try:
os.remove(workdir+src)
ret='ok'
except:
ret='error'
return ret
@app.route('/file/create/<src>')
def create(src):
if not src:
return 'error:no file name'
zsrc=workdir+src
if os.path.exists(zsrc):
return 'error: file has been exists'
f=open(zsrc,'wt')
f.write('')
f.close()
return 'ok'
@app.route('/file/write',methods=['post'])
def write():
ret=''
filedata=request.form['filedata']
src=request.form['filename']
if src=='':
return 'error:no file name'
zsrc=workdir+src
if os.path.exists(zsrc):
os.remove(zsrc)
f=open(zsrc,'wt')
f.write(filedata)
f.close()
ret='ok'
return ret
@app.route('/file/rename/<src>/<dst>')
def rename(src,dst):
zsrc=workdir+src
zdst=workdir+dst
if os.path.exists(zdst):
return 'error: exists:'+dst
if not os.path.exists(zsrc):
return 'error" not exists:'+src
os.rename(zsrc,zdst)
return 'ok'
@app.route('/file/read/<src>')
def read(src):
zsrc=workdir+src
if not os.path.exists(zsrc):
return 'error: file not exists:'+src
f=open(zsrc,'rt')
r=f.read()
f.close()
ret='<form method="post" action="/file/write">\n'
ret+='<textarea name=filedata id=filedata rows=30 cols=60>'
ret+=r
ret+='</textarea><br>'
ret+='<input type=text name=filename id=filename value="'+src+'"></input><br>'
ret+='<input type=submit value=" write "></input><br>'
return ret
if __name__ == "__main__":
app.run(host='0.0.0.0',port=5000)