Spsl v unit - final

75
Shell Programming & Scripting Languages Dr. K. Sasidhar

Transcript of Spsl v unit - final

Shell Programming & Scripting Languages

Dr. K. Sasidhar

IntroductionWEB ?A collection of documents inter-

linked by hyperlinks is called a web.A web page contains hyper text

information system

WWW ?World wide web is a global

hypertext system that uses the internet as its transmission medium

Hyper Text ?The text embedded with another units of

text. Hyperlinks are embedded into the text.HyperlinkHyperlink is an underlined or emphasized

word that when clicked displays another document.

Clicking on those links activates the necessary protocols and pulls up the chosen web site.

Browsing ? The process of navigating among the

documents is called as browsing.

Web Browser ? A program that runs on an computer and

provides access to WWW resources

Browser Types ? 1. Text only Browsers 2. Graphical Web Browsers

Text only browsers does not provide GUI features and not user friendly.

Ex: Lynx on Unix shell os

Graphical web browsers provide inline images, fonts and document layouts.

Much user friendly.

Ex: Internet Explorer Mozilla fire fox Google chrome Netscape Navigator

Web SiteA website runs a web server and has

been setup for publishing documents.

Web Server ?Is a program that accepts requests

from users for information framed according to the HTTP.

The server processes these requests and

sends the requested documents.

HTML ?Tim Berners- Lee designed HTML

Hyper Text Markup Language

A markup language is a set of markup tags

The purpose of the tags are to describe page

content

HTML Tags HTML tags are keywords (tag names) surrounded

by angle brackets like <html> HTML tags normally come in pairs like <b> and

</b> The first tag in a pair is the start tag, the second

tag is the end tag The end tag is written like the start tag, with

a forward slash before the tag name Start and end tags are also called opening

tags and closing tags

HTML Page Structure <html> <body> <h1>This a Heading</h1> <p>This is a paragraph.</p> <p>This is another paragraph.</p> </body></html>

Tags list

Heading Elements

<h1>Largest Heading</h1>

<h2> . . . </h2>

<h3> . . . </h3>

<h4> . . . </h4>

<h5> . . . </h5>

<h6>Smallest Heading</h6>

Text Elements<p>This is a paragraph</p>Attributes of p tag are: align, title

<br /> (line break)<hr /> (horizontal rule)<pre>This text is preformatted </pre>

Logical Styles <em>This text is emphasized</em>

<strong>This text is strong</strong><code>This is some computer code </code>

Text Elements continue…..Physical Styles<b>This text is bold</b><i>This text is italic</i> <u>This text is underlined</u><sub>Defines subscripted text

<sup>Defines superscripted

Web programming in python The Common Gateway Interface, or CGI, is a set of

standards that define how information is exchanged

between the web server and a custom script.

The Common Gateway Interface, or CGI, is a standard for

external gateway programs to interface with information

servers such as HTTP servers.

Response from web server through Browsing

Web browser contacts the HTTP web server and demands for the URL, i.e., filename.

Web Server parses the URL and looks for the filename. If it finds that file then sends it back to the browser,

otherwise sends an error message indicating that requested is a wrong file.

Web browser takes response from web server and displays either the received file or error message.

CGI Architecture

First CGI Program

#!/usr/bin/python print "Content-type:text/html\r\n\r\n" print '<html>' print '<head>' print '<title>Hello Word - First CGI

Program</title>' print '</head>‘ print '<body>' print '<h2>Hello Word! This is my first CGI

program</h2>' print '</body>' print '</html>'

Description of CGI program

GET and POST Methods

The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? character

http://www.test.com/cgi-bin/hello.py? key1=value1&key2=value2

The GET method is the default method to pass information from browser to web server and it produces a long string that appears in your browser's Location:

Usage of cgi module example: use cgi module, which makes it very easy to access passed

information: # Import modules for CGI handling import cgi, cgitb # Create instance of Field Storage form = cgi.FieldStorage() # Get data from fields

first_name = form.getvalue('first_name') last_name = form.getvalue('last_name') print "Content-type:text/html\r\n\r\n" print "<html>" print "<head>"

print "<title>Hello - Second CGI Program</title>"

print "</head>" print "<body>" print "<h2>Hello %s %s</h2>" %

(first_name, last_name) print "</body>" print "</html>"

Forms HTML forms are used to pass data to the server. A form can contain the following components 1. Text Field 2. Password field 3. Radio Button 4.

Checkbox 5. Button 6. Submit Button 7. Reset Button 8. Select (list) Box 9.TextArea 10. File type.

Forms continue…. The <form> tag is used to create an HTML form The Input Element is the most important form element. The input element is used to select user information. An input element can vary in many ways, depending on

the type attribute.

Input element An input element can be of type text field, checkbox,

password, radio button, submit button, and more Ex: <form>

First name: <input type="text" name="firstname" /> <br />

Last name: <input type="text" name="lastname" /></form>

Ex2: Password Field <input type="password" /> defines a

password field: <form>

Password: <input type="password" name="pwd" /></form>

Radio Buttons

<form><input type="radio" name=“gender" value="male" /> Male<br /><input type="radio" name=“gender" value="female" /> Female</form>

Checkboxes

<form><input type="checkbox" name="vehicle" value="Bike" /> I have a bike<br/><input type="checkbox" name="vehicle" value="Car" /> I have a car </form>

Submit Button

A submit button is used to send form data to a server. The data is sent to the page specified in the form's action attribute. The file defined in the action attribute usually does something with the received input

<form name="input" action="html_form_action.asp" method="get">Username: <input type="text" name="user" /><input type="submit" value="Submit" /></form>

HTML Form Tags

Tag Description <form> Defines an HTML form for user. input <input/>Defines an input control <textarea>Defines a multi-line text input control <label>Defines a label for an input Element <fieldset>Defines a border around elements in a form <legend>Defines a caption for a fieldset element <select>Defines a select list (drop-down list) <optgroup>Defines a group of related options in a select list <option>Defines an option in a select list <button>Defines a push button

Simple FORM Example: GET Method

<form action="/cgi-bin/hello_get.py" method="get"> First Name: <input type="text" name="first_name"> <br /> Last Name: <input type="text" name="last_name" /> <input

type="submit" value="Submit" /> </form>

Passing Information Using POST Method

More reliable method of passing information to a CGI program is the POST method.

This packages the information in exactly the same way as GET methods,

Instead of sending it as a text string after a ? in the URL it sends it as a separate message.

This message comes into the CGI script in the form of the standard input.

Example script that handles both GET and POST methods.

import cgi, cgitb # Create instance of FieldStorage form = cgi.FieldStorage() # Get data from fields first_name = form.getvalue('first_name') last_name = form.getvalue('last_name') print "Content-type:text/html\r\n\r\n" print "<html>" print "<head>" print "<title>Hello - Second CGI Program</title>"

print "</head>" print "<body>" print "<h2>Hello %s %s</h2>" % (first_name, last_name)

print "</body>" print "</html>" <form action="/cgi-bin/hello_get.py" method="post"> First Name: <input type="text" name="first_name"><br /> Last Name: <input type="text" name="last_name" /> <input type="submit" value="Submit" /> </form>

Passing Checkbox Data to CGI Program

<form action="/cgi-bin/checkbox.cgi" method="POST" target="_blank">

<input type="checkbox" name="maths" value="on" /> Maths <input type="checkbox" name="physics" value="on" />

Physics <input type="submit" value="Select Subject" /> </form>

Using Cookies in CGI

HTTP protocol is a stateless protocol. For a website, it is required to maintain session

information among different pages. For example, one user registration ends after completing

many pages. How to maintain user's session information across all the

web pages?

Server sends some data to the visitor's browser in the form of a cookie.

The browser may accept the cookie. If it does, it is stored as a plain text record on the visitor's

hard drive. Now, when the visitor arrives at another page on your site,

the cookie is available for retrieval. Once retrieved, the server knows/remembers what was

stored. Cookies are a plain text data record of 5 variable-length

fields:

Expires: The date the cookie will expire. If this is blank, the cookie will expire when the visitor quits the browser.

Domain: The domain name of your site. Path: The path to the directory or web page that sets the

cookie. This may be blank if you want to retrieve the cookie from any directory or page.

Secure: If this field contains the word "secure", then the cookie may only be retrieved with a secure server. If this field is blank, no such restriction exists.

Name=Value: Cookies are set and retrieved in the form of key and value pairs.

Setting Cookies #!/usr/bin/python print "Set-Cookie:UserID=XYZ;\r\n"

print "Set-Cookie: Password=XYZ123;\r\n" print "Set-Cookie:Expires=Tuesday, 31-Dec-2007

23:12:40 GMT";\r\n" print "Set-Cookie:Domain=www. Xyz.com;\r\n" print

"Set-Cookie:Path=/ print "Content-type:text/html\r\n\r\n" ...........Rest of the HTML Content....

check box.cgi script import cgi, cgitb # cgi trace back manager form = cgi.FieldStorage() # Create instance of FieldStorage if form.getvalue('maths'): # Get data from fields

math_flag = "ON" else: math_flag = "OFF" if form.getvalue('physics'): physics_flag = "ON" else: physics_flag = "OFF"

print "Content-type:text/html\r\n\r\n" print "<html>" print "<head>" print "<title>Checkbox - Third CGI Program</title>" print "</head>" print "<body>" print "<h2> CheckBox Maths is : %s</h2>" % math_flag print "<h2> CheckBox Physics is : %s</h2>" % physics_flag print "</body>" print "</html>"

Introducing…Django “The framework for perfectionists with

deadlines” MVC (Model View Controller) Flexible template language that can be used

to generate HTML, CSV, Email or any other format

Includes ORM that supports many databases – Postgresql, MySQL, Oracle, SQLite

Lots of extras included – middleware, csrf protections, sessions, caching, authentication

Django Concepts/Best Practices Fat models, thin views Keep logic in templates to a minimum Use small, reusable “apps” (app = python

module with models, views, templates, test)

Django Project Layoutdjango-admin.py startproject <PROJECT_ROOT>

manage.py<PROJECT_DIR>

__init__.pysettings.pyurls.pywsgi.py

settings.py Defines settings used by a Django application Referenced by wsgi.py to bootstrap the

project loading Techniques for managing dev vs prod

settings: Create settings-dev.py and settings-prod.py and

use symlink to link settings.py to the correct settings

Factor out common settings into base-settings.py and import. Use conditionals to load correct settings based on DEBUG or other setting

Sample Settings…DEBUG = TrueTEMPLATE_DEBUG = TrueALLOWED_HOSTS = [ ]# Application definitionINSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles',)

Django Apps Reusable modules django-admin.py startapp <app_name> Creates stub layout:

<APP_ROOT>admin.pymodels.pytemplates (directory)tests.pyviews.pyurls.py

Django Models Defined in models.py Typically inherit from django.db.models.ModelExample Model:from django.db import modelsclass TestModel(models.Model): name = models.CharField(max_length = 20) age = models.IntegerField()

Models (cont’d) Default is to set NOT NULL on all fields.

Override by adding null = True to field definition:name = models.CharField(max_length=20, null = True)

Relationships defined through special field types:models.OneToOneField(model)models.ForeignKey(model)models.ManyToManyField(model)

Models (cont’) Need Nulls in a Boolean Field? Use

models.NullBooleanField() Set Default value with “default”: count = models.IntegerField(default = 0) Use a inner Meta class to define additional

options, especially useful for abstract classes:class TestModel(models.Model): class Meta:

abstract = True

Model Methods model.save(self, *args, **kwargs) model.delete(self, *args, **kwargs) model.get_absolute_url(self) model.__str__(self) [Python 3]

model.__unicode__(self) [Python 2] Override with super(MODEL, self).save(*args,

**kwargs)

Activating a Model Add the app to INSTALLED_APPS in settings.py Run manage.py validate Run manage.py syncdb Migrations

Write custom script or manually handle migrations Use South

Selecting Objects Models include a default manager called

objects Manager methods allow selecting all or some

instancesQuestion.objects.all()Question.objects.get(pk = 1)

Use try block, throws DoesNotExist exception if no match

Question.objects.filter(created_date__lt = ‘2014-01-01’)

Returns QuerySet

Introspecting Legacy Models manage.py inspectdb Cut and paste generated code into models.py

– Easy!!

Full Samplefrom django.db import modelsfrom datetime import datetimeclass TimestampedModel(models.Model): created_datetime = models.DateTimeField() updated_datetime = models.DateTimeField() def save(self, *args, **kwargs): if self.id is None: self.created_datetime = datetime.now() updated_datetime = datetime.now() super(TimestampedModel,self).save(*args, **kwargs)

class Meta: abstract = True

Full Sample (cont’d)class Question(TimestampedModel): question_text = models.CharField(max_length = 200) def __str__(self): return self.question_text

Function vs. Class Views Django allows two styles of views – functions

or class based views Functions – take a request object as the first

parameter and must return a response object Class based views – allow CRUD operations

with minimal code. Can inherit from multiple generic view classes (i.e. Mixins)

Sample – Viewing a List of Questions Function based:from .models import Question

from django.shortcuts import render_to_response

def question_list(request): questions = Question.objects.all() return render_to_response(‘question_list.html’, { ‘questions’:questions})

Quick CRUD Operations with Generic Views (create, update and delete) ListView UpdateView CreateView If Model is specified, automagically creates a

matching ModelForm Form will save the Model if data passes

validation Override form_valid() method to provide

custom logic (i.e sending email or setting additional fields)

Sample – As Class Based Viewfrom .models import Questionfrom django.views.generic import ListView

class QuestionList(ListView): model = Question context_object_name = ‘questions’

Django Templates Very simple syntax:

variables = {{variable_name}}template tags = {%tag%}

Flexible – can be used to render html, text, csv, email, you name it!

Dot notation – template engine attempts to resolve by looking for matching attributes, hashes and methods

Question List Template<!doctype html><html lang=en><head><meta charset=utf-8><title>List of Questions</title></head><body>{%if questions%}<ul>{%for q in questions%}<li>{{q.question_text}}</li>{%endfor%}</ul>{%else%}<p>No questions have been defined</p>{%endif%}</body></html>

urls.py Defines routes to send urls to various views Can use regular expressions Extract parameters from a url and pass to the

view as a named parameter:

r(‘^question/(?P<question_id>\d+)/$’,’views.question_detail’)

Extensible – urls.py can include additional url files from apps: r(‘^question/’,include(question.urls))

Hooking up the Question Listfrom django.conf.urls import patterns, url, includeurlpatterns = patterns(‘’, (r’^questions/$’,’views.QuestionList’))OR:from django.conf.urls import patternsfrom views import QuestionListView

urlpatterns = patterns(‘’, (r’^questions/$’,’views.QuestionList.as_view()))

Forms in Django django.forms provides a class to build HTML

forms and validation. Example:from django import formsclass EditQuestionForm(forms.Form):

question_text = forms.CharField(max_length = 200)

Often redundant when creating forms that work on a single model

ModelForms Automatically generate a form from a model. Handles saving a bound model Can specify fields to be included or excluded in

the form Sample:from django.forms import ModelForm

from .models import Question

class QuestionForm(ModelForm): class Meta: model = Question fields = [‘question_text’]

Using a ModelForm Create an instance of an empty form:

form = QuestionForm() Create a form to update an existing instance of

a model: question = Question.objects.get(pk = 1) form = QuestionForm(instance = question)

Pass the form into the template and use the form methods to render the form: form.as_p form.as_ul form.<field_name> form.<field_name>.errors

Request & Response Request object encapsulate the request and provide

access to a number of attributes and methods for accessing cookies, sessions, the logged in user object, meta data (i.e environment variables),

Response objects are returned to the browser. Can set content type, content length, response does not have to return HTML or a rendered template

Special response types allow for common functionality:HttpResponeRedirectHttp404HttpStreamingResponse

Django Extras CRSF Middleware – enabled by default.

Include template tag in all forms:{%csrf_token%}

Authentication Caching Sessions Messages Email Logging

Authentication Django’s out of the box Auth system uses

database authentication. Changed extensively in Django 1.6 to allow

custom User objects. AUTHENTICATION_BACKENDS setting in

settings.py allows overriding how User objects are authenticated

If using the Authentication middleware and context_processors the current user is available to code as request.user and {{user}} is defined in all templates

Auth Decorators Live in django.contrib.auth.decorators login_required

@login_requireddef function_view(request):….

user_passes_test (can be used with lambda functions for real power) –

@user_passes_test(lambda u: u.is_staff)def function_view(request):…

has_perms – test for user permissions

Decorating CBVs Decorator is applied to the dispatch method Must be converted to a method_decorator –

use django.utils.decorators.method_decorator function:

class MyView(ListView): @method_decorator(login_required) def dispatch(self, *args, **kwargs): super(MyView,self).dispatch(*args, **kwargs)

Custom Auth Backend for the Bubble

Sending Email django.core.mail includes functions and

classes for handling email Set EMAIL_HOST in settings.py to outgoing

mailserver Import send_mail for simple mail:

send_mail(subject, message, from, to_emails) Use django.template.render_to_string to

format a message using a template Use EmailMultiAlternatives to create a text

message and attach a html version as well.

Resources Python – http://www.python.org Django – http://www.djangoproject.com Python Packages – https://pypi.python.org Django Packages –

https://www.djangopackages.com