While
first part focuses on HTML, javascript and CSS code, in this second part, we focus server-side-side, or the code associated with the python
Django framework. # Copyright (C) 2010 Lorenzo Sfarra (lorenzosfarra@ubuntu.com) # # This program is free software, you can Redistribute it and / or # modify it
under the terms of the GNU General Public License as published by
# the Free Software Foundation; Either
# version 2 of the License, or (at your option) any later version.
# # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # from django.conf.urls.defaults import * from django.conf import settings
MEDIA_ROOT = settings.MEDIA_ROOT
urlpatterns = patterns ('',
(r '^ cmd / (? P
.*)$',' terminaljs.terminal.views.cmd '),
(r' ^ media / (?
.* P) $ ',' django.views.static.serve ',
{' DOCUMENT_ROOT ': MEDIA_ROOT}),
(r'^$',' terminaljs.terminal.views.index '),
)
define so that a URL with cmd / cmd view the initial calls with a parameter "command", the command to execute. It also defines the URL with "media /" should be the initial static process, and finally define the default URL that invokes the view index.
Let us now see the code delle view, definito nel file
views.py
:
# Copyright (C) 2010 Lorenzo Sfarra (lorenzosfarra@ubuntu.com)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version. #
# This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #
from django.shortcuts import render_to_response from django.http import HttpResponse from commands import getoutput
import os.path
# global variable
CWD = "/home/lorenzo"
def index(request):
return render_to_response("index.html")
def is_trusted_command(command):
#TODO: check if it's a trusted command
return True
def cmd(request, command):
global CWD
if command.startswith("cd "):
new_path = command.split(" ")[1]
if not new_path.startswith("/"):
# relative path
new_path = os.path.join(CWD, new_path)
if os.path.isdir(new_path):
CWD = new_path
output = ""
else:
output = "cd: " + new_path + ": No such file or directory"
elif is_trusted_command(command):
output = getoutput("cd %s; %s" %(CWD,command))
else:
output = "Untrusted command."
return HttpResponse(output)
be emphasized, as done in the first part, the function
is_trusted_command
which must also be implemented server-side.
Obviously, everything is purely demonstrative, for those interested in improving it is all the code on Google Code
.
In
0 comments:
Post a Comment