Table of contents
1.
Introduction
2.
Keywords
3.
Database Quoting
4.
Making a secure connection
5.
Frequently Asked Questions
5.1.
What is the web2py framework?
5.2.
Can web2py be used with Python 3?
5.3.
What is meant by Web application framework?
5.4.
For what purpose is Web2py used?
5.5.
Which is preferable, Django or web2py?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Keywords, Quoting and Making a Secure Connection in web2py

Author yuvatimankar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

web2py is a free, open-source web framework for  Agile development that implies database-driven web apps; web2py is written and programmable in Python. web2py is a framework for full-stack; it comprises all the components a developer requires to build a fully functional web application.

web2py

web2py follows MVC (Model-View-Controller) pattern for running web applications. The model here is part of the application, which includes the logic of the data, the view here is a part of the application that helps render the data display to end-users, and the controller here is a part of the application that handles user interaction. 

Keywords

As with any other frameworks out there, web2py also has keywords known as reserved keywords. A keyword is known as “check_reserved” tells the constructor to check table and column names against reserved SQL keywords in target backend databases. Check-reserved keyword is by default None. This is a list of strings that store the DB backend adapter names.

The adapter name is similar to the one used in the DAL connection string. So if we want to check against PostgreSQL and MSSQL, then our connection string would look as shown below:

db = DAL('sqlite://storage.sqlite', check_reserved=['postgres', 'mssql'])
Reserved Keywords in Web2py

Now, The DAL will scan the keywords in the same order as the list. There are two more options for us “all” and “common.” If we describe all, it will check against all known SQL keywords. If we describe common, it will only check against basic SQL keywords such as INSERT, SELECT, UPDATE, etc. We have to specify for supported backends if we would also like to check against the nonreserved SQL keywords. In that case, we would add the “_nonreserved” keyword to the name.

check_reserved=['postgres', 'postgres_nonreserved']


Below mentioned database backends support reserved word checking.

  • PostgreSQL = postgres(_nonreserved)
  • MySQL = mysql
  • FIreBird = firebird(_nonreserved)
  • MSSQL = mssql
  • Oracle = oracle

Database Quoting

In web2py, quoting of SQL entities is by default enabled in DAL:

entity_quoting = True

In this manner, identifiers are automatically quoted in SQL generated by DAL. At the SQL level, unquoted identifiers and keywords are case insensitive, so quoting SQL identifiers makes it case sensitive.

Database quoting in web2py

DAL ignores field cases by default; to change this, we have to use:

ignore_field_case = False


To ensure that we can use the same names in Python and the database schema, we must arrange for both settings above. Let’s see the example:

db = DAL(ignore_field_case=False)
db.define_table('table1', Field('column'), Field('COLUMN'))
query = db.table1.COLUMN != db.table1.column

Making a secure connection

Making a secure connection

As we all know, secure connections are built to protect the data sent between two parties, and it masks confidential information from third parties. It is necessary to connect to a database using a secure connection, especially if our Database is on one server and your application is on another. We must pass some additional parameters to the DB driver in that case. 

For connecting PostgreSQL with psychopg2, it should be like the below:

DAL('postgres://user_name:user_password@server_addr/db_name',
    driver_args={'sslmode': 'require', 'sslrootcert': 'root.crt',
                 'sslcert': 'postgresql.crt', 'sslkey': 'postgresql.key'})


In which parameters sslcert, sslrootcert, and sslkey must contain the full path to the files. 

Frequently Asked Questions

What is the web2py framework?

Web2py, written in Python and programmable in Python, is a free, open-source online framework for agile development that involves database-driven web applications.

Can web2py be used with Python 3?

Web2py functions on Python 2.7, Python 3, with CPython (the C implementation), and PyPy (Python written in Python).

What is meant by Web application framework?

A software framework known as a web application framework (WAF) is made to facilitate the creation of web applications, which include web resources, web services, and web APIs.

For what purpose is Web2py used?

Web2py is mainly used to program dynamic websites using Python as a programming language. It contains all the tools, components, and APIs essential to building a fully functional web application.

Which is preferable, Django or web2py?

Due to its smaller size, short learning curve, and lack of project-level configuration files, Web2py is preferred over Django. Compared to PHP-based frameworks and Java-based frameworks, Web2py has a significantly more explicit syntax than PHP-based and Java-based frameworks, making this application easier to comprehend, maintain, and create.

Conclusion

In this article, we discussed the web2py introduction, and then we saw keywords, Quoting, and how to make a secure connection in web2py. We started with the introduction of web2py, then we saw reserved keywords, Database quoting, and how to secure database connections in detail.

Also, visit our Guided Path in  Coding Ninjas Studio to learn about Web2py. See our Interview Experience Section and interview bundle for placement preparations if you are preparing for an interview. Upskill yourself in PythonKivyBackend Web TechnologiesSQL, MongoDB, Data Structures and Algorithms, JavaScript,  System Design, and much more!. Please upvote our blogs if you find them engaging and helpful! 

Here are some useful links Web2pyKivyRuby vs. Python. You can also refer to the Web2py IntroductionWeb2py InstallationWeb2py ApplicationWeb2py ToolWeb2py Capacity Planning, and Troubleshooting in Web2py.   

Wish you all the best for your future adventures and Happy Coding

Thank you
Live masterclass