Server-Side Scripting
Server-Side Scripting
Vikas Chandra Sharma, Swarrnim School of Computing & IT, Swarrnim Startup and
Innovation University
CHAPTER-1
Example: PHP is often used for tasks like processing forms and managing user
authentication.
3. Web Services
- Web services enable communication between different software applications over the
internet using standard protocols like HTTP.
- They are essential for building distributed and interconnected web systems.
Example: Django is used to build web applications like content management systems (e.g.,
Django CMS) and e-commerce platforms.
if x > 5:
print("x is greater than 5")
name = "John"
age = 30
is_student = False
4. Operators
- Python supports operators for arithmetic, comparison, logical operations, and more.
- Example:
result = 10 + 5
is_equal = (x == y)
Statement
print("Hello, World!")
Expression
total = x + y
6. Decision Making
- Python uses if, elif, and else statements for decision-making.
- Example:
for i in range(5):
print(i)
8. Data Structures
- Python provides various data structures like strings, tuples, lists, and dictionaries for
organizing and manipulating data.
- Example:
9. Recursion
- Recursion is a programming technique where a function calls itself to solve a problem.
- Example:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
11. Functions
- Functions are reusable blocks of code that perform specific tasks.
- Example:
def greet(name):
return f"Hello, {name}!"
import math
import random
13. Files I/O
- Python can read from and write to files using built-in functions like `open()`.
- Example:
with open('data.txt', 'r') as file:
content = file.read()
14. Exceptions
- Exceptions are used to handle errors gracefully in Python.
- Example:
try:
result = 10 / 0
except ZeroDivisionError:
print("Division by zero is not allowed.")
Chapter 3: CGI and GUI Programming in Python
Creating objects
person1 = Person("John", 30)
person2 = Person("Alice", 25)
```
2. Regular Expressions:
- Definition: Regular expressions (regex) are powerful tools for pattern matching in strings.
They define search patterns for text processing.
- Example:
```python
import re
3. CGI Programming:
- Definition: CGI programming enables the creation of dynamic web applications by
executing scripts on a web server in response to user requests.
- Example:
```python
CGI script for processing form data
print("Content-type: text/html\r\n\r\n")
print("<html><body>")
print("<h2>Hello, {}!</h2>".format(form_data['name']))
print("</body></html>")
```
5. Sending Email:
- Definition: Sending email using Python involves utilizing libraries like `smtplib` to
connect to an email server and send messages programmatically.
- Example:
```python
import smtplib
from email.mime.text import MIMEText
Sending email
server = smtplib.SMTP('smtp.example.com', 587)
server.starttls()
server.login("[email protected]", "password")
6. Multithreading:
- Definition: Multithreading enables concurrent execution of multiple threads within a
Python program, enhancing performance.
- Example:
```python
import threading
def print_numbers():
for i in range(5):
print(i)
def print_letters():
for letter in 'ABCDE':
print(letter)
Creating threads
thread1 = threading.Thread(target=print_numbers)
thread2 = threading.Thread(target=print_letters)
Starting threads
thread1.start()
thread2.start()
```
7. XML Processing:
- Definition: XML processing involves parsing and manipulating XML data in Python
using libraries like `xml.etree.ElementTree`.
- Example:
```python
import xml.etree.ElementTree as ET
8. GUI Programming:
- Definition: GUI programming in Python involves creating visual interfaces for
applications using libraries like Tkinter or PyQt.
- Example:
```python
from tkinter import Tk, Label, Button
class GUIApp:
def __init__(self, master):
self.master = master
master.title("GUI Application")
def print_message(self):
print("Button clicked!")
root = Tk()
app = GUIApp(root)
root.mainloop()
```
int main() {
Py_Initialize();
PyRun_SimpleString("print('Hello from C!')");
Py_Finalize();
return 0;
}
```
- *Embedding Python:*
```c
include <Python.h>
int main() {
Py_Initialize();
PyRun_SimpleString("print('Hello from embedded Python!')");
Py_Finalize();
return 0;
}
Chapter 4: Introduction to Ruby on Rails
1. MVC Architecture:
- Definition: MVC (Model-View-Controller) is a software design pattern used in Ruby on
Rails to structure code into three interconnected components for better organization and
scalability.
- Example:
In a blog application:
- Model: Manages data structures, such as Post and Comment models.
- View: Displays information, presenting posts and comments to users.
- Controller: Handles user interactions, managing requests and updating models
accordingly.
2. How to Install:
- Steps:
1. Install Ruby using RVM (Ruby Version Manager).
2. Install Rails using the gem package manager.
3. Verify the installation using terminal commands.
- Example:
```bash
Install RVM
\curl -sSL https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
Install Ruby
rvm install ruby
Install Rails
gem install rails
Verify installation
ruby -v
rails -v
```