server side scripting - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs275/servscript.pdf ·...
TRANSCRIPT
Server Side Scripting
1 a simple Web Calculator
the web interface
html form and Python script
2 A Login Form
the web interface
html form and Python script
3 Passing Text and Code
textarea element
running given Python code
MCS 275 Lecture 22
Programming Tools and File Management
Jan Verschelde, 1 March 2017
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 1 / 30
Server-Side Scripting
1 a simple Web Calculator
the web interface
html form and Python script
2 A Login Form
the web interface
html form and Python script
3 Passing Text and Code
textarea element
running given Python code
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 2 / 30
The Web Interfacefor a simple calculator
1 two input elements for two operands
2 select element for the operator
3 checkbox for converting operands to float
4 submit and reset buttons
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 3 / 30
Result of the Calculationoutput of Python script
Python script writes result inside an h1 header.
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 4 / 30
Server-Side Scripting
1 a simple Web Calculator
the web interface
html form and Python script
2 A Login Form
the web interface
html form and Python script
3 Passing Text and Code
textarea element
running given Python code
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 5 / 30
html form for calculator
<html>
<head>
<title> MCS 275 : a calculator </title>
</head>
<body>
<form method="post"
action="http://localhost/cgi-bin/calculate.py">
<table width = "100%" border = "3">
<tr>
<th> 1st operand </th>
<th> operator </th>
<th> 2nd operand </th>
</tr>
...
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 6 / 30
html form continued
the table continued:
<tr>
<td><input type = "text"
name = "operand1" size="45%"></td>
<td><select name = "operator">
<option value = "+">+
<option value = "-">-
<option value = "*">*<option value = "/">/
</td>
<td><input type = "text"
name = "operand2" size="45%"></td>
</table>
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 7 / 30
a Checkbox – html form continued
<p>
<input name = "convert" type = checkbox
value = True>
convert operands to floats
<input type="submit"
value = "submit calculation">
<input type="reset"
value = "reset the form">
</p>
</form>
</body>
</html>
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 8 / 30
Python script calculate.py
#!/usr/bin/python
import cgi
FORM = cgi.FieldStorage()
print("Content-Type: text/html\n")
ERROR = False
try:
XVAL = FORM[’operand1’].value
except KeyError:
print("please enter first operand")
ERROR = True
try:
YVAL = FORM[’operand2’].value
except KeyError:
print("please enter second operand")
ERROR = True
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 9 / 30
the script continued
if not ERROR:
try:
CONV = eval(FORM[’convert’].value)
except:
CONV = False
OPER = FORM[’operator’].value
if not CONV:
EXPR = XVAL + ’ ’ + OPER + ’ ’ + YVAL
else:
EXPR = str(float(XVAL)) + ’ ’ + OPER \
+ ’ ’ + str(float(YVAL))
RESULT = EXPR + ’ = ’ + str(eval(EXPR))
print("<html><body>")
print("<h1>%s</h1>" % RESULT)
print("</body></html>")
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 10 / 30
Server-Side Scripting
1 a simple Web Calculator
the web interface
html form and Python script
2 A Login Form
the web interface
html form and Python script
3 Passing Text and Code
textarea element
running given Python code
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 11 / 30
The Web Interface
1 two input elements for name and password2 radio button for new or returning user
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 12 / 30
Result of the Submitaction by Python script login_form.py
Python script checks whether new or returning user
and confirms the name.
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 13 / 30
Server-Side Scripting
1 a simple Web Calculator
the web interface
html form and Python script
2 A Login Form
the web interface
html form and Python script
3 Passing Text and Code
textarea element
running given Python code
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 14 / 30
html form in the file login_form.html
<html>
<head>
<title> MCS 275 : login form </title>
</head>
<body>
<h1> please register </h1>
<form
action="http://localhost/cgi-bin/login_form.py">
<p>
enter your name:
<input type="text" name="login" size ="20">
</p>
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 15 / 30
a radio button – html continued
<input type="radio" name="new"
value = True checked> new user
<input type="radio" name="new"
value = False> returning user
<p> enter your password:
<input type="password"
name="password" size ="16">
</p>
<p> <input type="submit">
<input type="reset">
</p>
</body>
</html>
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 16 / 30
the Python script in the file login_form.py
#!/usr/bin/python
"""
Gets login name and password.
"""
from ast import literal_eval
import cgi
FORM = cgi.FieldStorage()
print("Content-Type: text/plain\n")
ERROR = False
try:
NAME = FORM[’login’].value
except KeyError:
print("please enter your name")
ERROR = True
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 17 / 30
script continued
try:
WORD = FORM[’password’].value
except KeyError:
print("please enter a password")
ERROR = True
if not ERROR:
if literal_eval(FORM[’new’].value):
print(’welcome new user ’ + NAME)
else:
print(’welcome back ’ + NAME)
For new user, check if name does not already exist.
If okay, add to name and password file.
For returning user, check if name already exists on file.
If okay, check if password matches.
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 18 / 30
Server-Side Scripting
1 a simple Web Calculator
the web interface
html form and Python script
2 A Login Form
the web interface
html form and Python script
3 Passing Text and Code
textarea element
running given Python code
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 19 / 30
The Web Interfacefor submitting a text paragraph
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 20 / 30
Result of the Submitoutput of Python script
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 21 / 30
the element textarea in give_words.html
<html>
<head>
<title> MCS 275 : enter some text </title>
</head>
<body>
<h1> write a paragraph </h1>
<form method="post"
action="http://localhost/cgi-bin/give_words.py">
<p>
<textarea name = "sometext"
rows = 4 cols = 20></textarea>
</p>
<p>
<input type="submit"> <input type="reset">
</p>
</form>
</body>
</html>
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 22 / 30
getting the textarea in give_words.py
#!/usr/bin/python
"""
Illustration of processing a textarea.
"""
import cgi
FORM = cgi.FieldStorage()
print("Content-Type: text/plain\n")
try:
TEXT = FORM[’sometext’].value
print("your paragraph is \n" + TEXT)
except KeyError:
print("please enter some words")
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 23 / 30
Server-Side Scripting
1 a simple Web Calculator
the web interface
html form and Python script
2 A Login Form
the web interface
html form and Python script
3 Passing Text and Code
textarea element
running given Python code
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 24 / 30
The Web Interfacefor submitting Python code
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 25 / 30
Result of the Submitoutput of Python script
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 26 / 30
accepting Python code in run_python.html
<html>
<head>
<title> MCS 275 : run python </title>
</head>
<body>
<h1> run python </h1>
<form method="post"
action="http://localhost/cgi-bin/run_python.py">
give some Python code below:
<p>
<textarea name = "code"
rows = 4 cols = 20></textarea>
</p>
<p> <input type="submit"> <input type="reset">
</p>
</form> </body> </html>
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 27 / 30
getting the Python code in run_python.py
#!/usr/bin/python
"""
Takes the textarea from run_python.html
and runs the code in that element.
"""
import cgi
from os import system
FORM = cgi.FieldStorage()
print("Content-Type: text/plain\n")
ERROR = False
try:
CODE = FORM[’code’].value
print("your code is \n\n" + CODE)
except KeyError:
print("please enter code")
ERROR = True
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 28 / 30
running the Python code – script continued
if not ERROR:
FILE = open(’/tmp/code.py’, ’w’)
FILE.write(CODE)
FILE.close()
print("\n... running the code ...\n")
CMD = ’python /tmp/code.py > /tmp/out’
RETVAL = system(CMD)
if RETVAL != 0:
print("the code has an error")
else:
FILE = open(’/tmp/out’, ’r’)
OUTP = FILE.readlines()
print("the output is \n\n", OUTP[0])
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 29 / 30
Summary and Exercises
Because of security issues, keep working offline.
1 Modify the script give_words.py so it writes the words typed in
the textarea in reverse order.
2 Replace the radio buttons in login_form.html by one
CheckBox "new user".
3 Add to login_form.html a select element offering the options:
instructor, student, and staff.
4 Extend the script login_form.py so it maintains a password
file. For every user there is one line: two strings (name and
password) separated by a colon.
Programming Tools (MCS 275) server side scripting L-22 1 March 2017 30 / 30