This repository was archived by the owner on Jul 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcmdresources.py
More file actions
61 lines (54 loc) · 2.4 KB
/
cmdresources.py
File metadata and controls
61 lines (54 loc) · 2.4 KB
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
from twisted.internet import protocol
from twisted.internet import reactor
from twisted.web import resource
from twisted.web.server import NOT_DONE_YET
import re
import cgi
class ShellHTTP(protocol.ProcessProtocol):
def __init__(self, request):
self.request = request
self.data = ""
self.css = "<style>body pre {margin: 10px; padding-top: 10px; font-family: 'Monaco', 'Deja Vu Sans Mono', 'Inconsolata' ,'Consolas',monospace; background:#111 none repeat scroll 0 0; color:#fff; font-size:10px;}</style>"
def connectionMade(self):
print "connectionMade!"
def outReceived(self, data):
# Write stdout data from process to HTTP request
self.request.write(data)
def errReceived(self, data):
print "errReceived! with %d bytes!" % len(data)
def inConnectionLost(self):
print "inConnectionLost! stdin is closed! (we probably did it)"
def outConnectionLost(self):
print "outConnectionLost! The child closed their stdout!"
self.request.write('\nDone\n')
self.request.finish()
def errConnectionLost(self):
print "errConnectionLost! The child closed their stderr."
def processExited(self, reason):
print "processExited, status %d" % (reason.value.exitCode,)
def processEnded(self, reason):
print "processEnded, status %d" % (reason.value.exitCode,)
print "quitting"
class ShellResource(resource.Resource):
def _responseFailed(self, err, process):
process.signalProcess('KILL')
def render_POST(self, request):
request.setHeader("content-type", "text/plain")
cmd = cgi.escape(request.args["tool"][0])
filename = cgi.escape(request.args["file"][0])
term = cgi.escape(request.args["term"][0])
shell = ShellHTTP(request)
if cmd == 'tail':
process = reactor.spawnProcess(shell, "tail", ["tail", "-f", filename], {})
if cmd == 'grep':
process = reactor.spawnProcess(shell, "grep", ["grep", term, filename], {})
request.notifyFinish().addErrback(self._responseFailed, process)
return NOT_DONE_YET
class Root(resource.Resource):
def __init__(self, wsgi_resource):
resource.Resource.__init__(self)
self.wsgi_resource = wsgi_resource
def getChild(self, path, request):
path0 = request.prepath.pop(0)
request.postpath.insert(0, path0)
return self.wsgi_resource