Python Assignment 2
Python Assignment 2
KHARAR
ASSIGNMENT – II
PROGRAMING IN PYTHON
BTCS510-18
Text Extraction: Regular expressions can be used to extract specific portions of text
from a larger body of text. For example, you can extract all the words from a
document or all the URLs from a web page.
Text Replacement: Regular expressions allow you to search for a pattern and replace
it with another string. This is useful for text cleaning and manipulation tasks.
CGI
CGI, or Common Gateway Interface, is a standard protocol that allows web servers to
execute external programs, typically written in scripting languages like Python, to generate
dynamic web content. In Python, you can use CGI to create web applications that generate
web pages, interact with databases, or perform various tasks in response to web requests.
Here's a brief overview of CGI in Python:
How CGI Works:
o When a web server receives an HTTP request for a resource (e.g., a web page or
a script), it can recognize that the resource is a CGI script based on its file
extension (e.g., .py, .cgi).
o The server launches the specified script as a separate process.
o The script processes the request, generates dynamic content (HTML, JSON,
etc.), and sends the content back to the web server.
o The server then delivers the generated content to the client's web browser for
display.
Using Python for CGI:
o To create a CGI script in Python, you typically write Python code that reads and
processes data from the incoming HTTP request (e.g., form data or query
parameters) and generates an appropriate HTTP response.
o You can use the cgi module in Python to parse and access CGI-related
environment variables and input data. The cgitb module is also useful for
handling errors and exceptions in CGI scripts.
o Make sure the Python script is executable and configured correctly in the web
server's CGI directory.
Security Considerations:
o CGI scripts must be carefully written to prevent security vulnerabilities, such as
code injection or unauthorized access.
o You should validate and sanitize user input and avoid executing external
commands or relying on untrusted data.
Deployment:
o To deploy a Python CGI script, you'll need a web server (e.g., Apache)
configured to support CGI.
o The script should be placed in a designated CGI directory with executable
permissions.
o Proper server configuration is necessary to map URLs to the corresponding CGI
script files.
Alternatives to CGI:
o While CGI is still used in some cases, it's not the most efficient or modern
approach for building web applications. Alternatives like WSGI (Web Server
Gateway Interface) and frameworks such as Flask and Django provide more
robust and efficient solutions for Python web development.
In summary, CGI in Python allows you to create dynamic web applications by
executing Python scripts in response to web requests. However, due to its performance
limitations and security concerns, modern web development in Python often relies on
more advanced approaches, like web frameworks and application servers.