In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to use flask to easily build mock services", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use flask to easily build mock services.
Students who have done interface testing must have heard of mock Server, we will think it is very mysterious, very high-end! the reason for the emergence of mock Server is that today's business systems rarely exist in isolation, and they more or less need to use services provided by brother teams or other companies, which causes trouble for our joint debugging and testing. So there's a solution-- set up a temporary server, simulate those services, and provide data for joint debugging and testing. The following figure well explains what Mock server is.
Flask
Flask is a Web microframework written by Python that allows us to quickly implement a website or Web service in Python.
Installation of Falsk
Pip install flask
Flask is simply too fast to build a web service, which can be done in seven lines of code, as shown in the helloword code:
From flask import Flask app = Flask (_ _ name__) @ app.route ('/') def index (): return 'Hello World' if _ _ name__ =' _ _ main__': app.run ()
Then run the py file in ide, which looks like this:
Enter http://127.0.0.1:5000/ in the browser to see the Hello World displayed in the browser
The code analysis is as follows:
@ app.route ('/') indicates that the url,'/' you want to access represents the root directory
Return 'Hello World' represents the Hello World displayed in the browser, that is, the return value of the request http://127.0.0.1:5000/
App.run () means to start the mock service.
If we need to build a get method with parameters, we can use the following code:
@ app.route ('/ check/') def check_version (version=None): if version=='1': return 'version=1.0' elif version=='2': return' version=2.0' else: return 'bad version'
This is equivalent to the parameters entered in url
Entering 127.0.0.1:5000/check/2 in the browser will output version=2.0
Entering 127.0.0.1:5000/check/1 in the browser will output version=1.0
Entering 127.0.0.1:5000/check/111 in the browser will output bad version
This example easily simulates the request of the get method we need, determines the specific method, and then returns the corresponding value.
If we need to create a post method, we can use the following code:
From flask import Flask,request @ app.route ('/ login',methods= ['POST']) def login (): if request.method = =' POST': val=str (request.data, encoding = "utf8") print (val) if val.find ('admin') = =-1: return' status 400 No such user!' Else: return 'status 200 admin login' else: return' status 403 Legend login need post method'
The build request through RESTclient (Firefox plug-in) is as follows:
Output admin login
The code is parsed as follows:
Methods= ['POST'] limits the http methods that can be used and can only make POST
The method of obtaining request by request.method
Request.data gets the value of data in request
Str (request.data, encoding = "utf8") converts the value of request.data from bytes type to str type.
Well, through the example above, we can easily simulate POST or GET methods to build Mock Server!
The choice of Flask and Django
Django is also the mainstream framework for python web development. The differences between Django and flask are as follows:
Flask provides flexibility, simplicity and fine-grained control.
Flask is unrestricted and allows you to decide how to implement the application.
Django provides a comprehensive experience of the administrative panel, database interface, directory structure and ORM for our Web application development.
If we only need to develop a lightweight website or a specific microservice (such as mock server), we don't need the large and comprehensive components and features that come with Django at all, so we should not hesitate to choose Flask. If we are developing traditional enterprise-class websites with a variety of functions (such as e-commerce, news content management, social networking sites, office OA), using Django can save us a lot of energy in finding or developing third-party extensions.
At this point, I believe you have a deeper understanding of "how to use flask to easily build mock services". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.