Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Python web development technology

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to understand Python web development technology". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First of all, let's ask a question, how to look at Python web development technology? If you don't know how to answer, let's change the question: how to understand the nature of Python web, which I first used three programs to explain.

The first is Python socket-based programming, which opens port 8000, then listens at the designated port and receives a message return.

Import socket

Def handle_request (client):

Buf = client.recv (1024)

Client.send ("HTTP/1.1 200 OKrnrn")

Client.send ("Hello, Jeanron")

Def main ():

Sock = socket.socket (socket.AF_INET, socket.SOCK_STREAM)

Sock.bind (('localhost', 8000))

Sock.listen (5)

While True:

Connection, address = sock.accept ()

Handle_request (connection)

Connection.close ()

If _ _ name__ = ='_ _ main__':

Main ()

This can be regarded as the most basic communication program, and there is no web direction involved here, there is a most basic point is that this way does not have any norms and requirements, the line will reach. When it comes to the web side, it must be linked to html. For processing requests from web applications and web servers, it is necessary to mention wsgi, which is similar to Java. In Java, this server-side software component technology, called Servlet, is actually located as a lightweight lite. For example, applet in Java has a similar name. Servlet can dynamically expand the function of the web server, to put it simply, it provides an extended interface on the Http server, which is an interface label. Let's simply expand.

According to the JavaEE specification, any servlet needs to directly or indirectly implement the Servlet interface, namely javax.servlet.Servlet, which only defines five methods.

Because servlet is protocol-independent, it is difficult to implement it directly, so the sun used to be a javax.servlet.GenericServlet class in addition to classes that implement the Servlet interface.

This general class can only be an abstract class, in which the core method is service (), so we can see that it is still protocol-independent and still protocol-independent, so there is a new implementation class HttpServlet. When we write web applications, we only need to focus on how to rewrite get and post methods. Originally, it is more inclined to the post method in terms of security and extensibility, but at present, the get method is more popular.

So understand some of the above, and then look at Python will be a lot more clear.

The stand-alone WSGI server provided by the python standard library is called wsgiref

WSGI (Web Server Gateway Interface) is a specification that defines the interface format between web app and web server written in python to realize the decoupling between web app and web server. Python encapsulates a version by default, so we can easily open a simple_server.

In this way, the code has basic specifications and standards, which is a standard web start posture. You can also embed html tags on this basis, so that the front-end display will be very rich.

#! / usr/bin/env python

# coding:utf-8

From wsgiref.simple_server import make_server

Def RunServer (environ, start_response):

Start_response ('200OK', [(' Content-Type', 'text/html')])

Return 'Hello, WSGI'

If _ _ name__ = ='_ _ main__':

Httpd = make_server (', 8000, RunServer)

Print "Serving HTTP on port 8000..."

Httpd.serve_forever ()

But the problem with this approach is also obvious. for example, there are 50 types of requests and how do we handle them on the program side? a natural idea is that we need to decouple. For example, if it is category 1, switch to the logical processing of category 1, and so on. But in this way, a large number of if-else blocks will be embedded in the program, and to be honest, this approach is relatively extensive and not elegant at all. You can imagine how bloated the program would be if there were 200 different requests. So this way needs to be improved, we can manually separate some logic to form different modules, different requests will have different logic processing and return.

So on this basis, we need a third program.

#! / usr/bin/env python

# coding:utf-8

From wsgiref.simple_server import make_server

Def index ():

Return 'index'

Def login ():

Return 'login'

Def routers ():

Urlpatterns = (

('/ index/', index)

('/ login/', login)

)

Return urlpatterns

Def RunServer (environ, start_response):

Start_response ('200OK', [(' Content-Type', 'text/html')])

Url = environ ['PATH_INFO']

Print (url)

Urlpatterns = routers ()

Func = None

For item in urlpatterns:

If item [0] = = url:

Func = item [1]

Break

If func:

Return func ()

Else:

Return '404 not found'

If _ _ name__ = ='_ _ main__':

Httpd = make_server (', 8000, RunServer)

Print "Serving HTTP on port 8000..."

Httpd.serve_forever ()

A routing forwarding role is defined here, which is responsible for handling the jump and return of the request. If you go into another layer of abstraction, you can actually do it.

So so far, we have a basic understanding that we haven't used any web framework yet, but we are able to handle basic requirements.

Using the framework is just an abstraction of some specific functions, which can improve the efficiency of our development, just like we use notepad or IDE development.

Of course, at this time, the return of web is still some relatively basic content, if we need rich content, such as data such as tables, dynamic menus and so on. A series of additional work needs to be done.

A basic graphical representation of Python web is as follows:

Client requests are filtered by url and go into different logical processing, that is, different functions.

This is slightly different from the web processing of the Java stack.

So the front-end technology is emerging one after another. I briefly listed some technical frameworks and directions.

Backend (based on Python):

Django,Tornado,web.py,Flask

Bottle,CherryPy,Quixote

Front end:

Java framework

Angular.Js, React,JQuery

Vue.js, Node.Js

Front-end UI framework

Bootstrap

Pure,EasyUI,AmazeUI,SB-admin 2

Front-end visualization

Echarts,tableau

This is the end of "how to understand Python web Development Technology". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report