# Django Extension --- # Django X MongoDB ![](https://i.imgur.com/97K2BLQ.png) Django built-in support these followings SQL: PostgreSQL、 MariaDB 、MySQL 、Oracle 、SQLite。 ---- ### install MongoEngine ```cmd= pip install mongoengine ``` ---- ### install Django Rest Framework MongoEngine ```cmd= pip install django-rest-framework-mongoengine ``` ---- ### create a new requirement.txt ```cmd= pip freeze >> requirements.txt ``` ---- ### add app in INSTALLED_APPS put papckage module in Django settings.py the parameter INSTALLED_APPS add package name ```python= INSTALLED_APPS = ( ... 'rest_framework', 'rest_framework_mongoengine', ... ) ``` ---- ### Create a MongoDB >now we should create a MongoDB for this project [Download MongoDB](https://www.mongodb.com/try/download/community) **Online Mongo Server MongoDB Atlas** [Link](https://www.mongodb.com/cloud/atlas) you can rent a server online --- # PostgreSQL X Django ---- Download PostgreSQL [Link](https://www.postgresql.org/download/) ---- ##### install psycopg2 ```cmd pip install psycopg2 ``` ---- ##### add engine in settings ```python= DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', #PostgreSQL 'NAME': 'BOOKSTORE', #資料庫名稱 'USER': 'postgres', #資料庫帳號 'PASSWORD': '****', #資料庫密碼 'HOST': 'localhost', #Server(伺服器)位址 'PORT': '5432' #PostgreSQL Port號 } } ``` ---- ##### tranfer database ```cmd python manage.py migrate ``` --- # Django X unittest ---- Django default use Python built-in standard library **unittest** to debug ![](https://i.imgur.com/9MUtQBe.png) ---- ### unittest framework there are two misunderstand about unittest 1. we can coding unittest without unittest,unittest essence is test a program by another program 2. unittest framework not only can use on unit level testing, it also can use on UI testing,interface automatically testing and mobile APP automatically testing ---- ##### unittest offer features 1. offter use instance edit specification and execution - . unittest framework can unify edit instance specification 2. offer more profession compare method - . testing should comparte real outcome with expected outcome that is we called [assertion] 3. offer rich testing log - . Ex: statistic failed log,success instance and execution log ---- ##### We can according unittest framework offered feature to divide to different class 1. unittest:[unittest] 2. HTTP interface automatically test [unittest+Requests] 3. Web UI automatically test [unittest+Selenium] 4. Mobile App automatically test [unittest+Appium] ---- ##### Coding unittest instance develop a simple calculator [module.py] ```python= class Calculator(): def __init__(self,a,b): self.a=int(a) self.b=int(b) def add(self): return self.a+self.b def sub(self): return self.a-self.b def mul(self): return self.a*self.b def div(self): return self.a/self.b ``` ---- [test.py] ```python= import unittest from module import class ModuleTest(unittest.Testcase): def setUp(self): del.cal=Calculator(8,4) def tearDown(self): pass def test_add(self): result=self.cal.add() self.assertEqual(result,12) def test_sub(self): result=self.cal.sub() self.assertEqual(result,4) def test_mul(self): result=self.cal.mul() self.assertEqual(result,32) def test_div(self): result=self.cal.div() self.assertEqual(result,2) if __name__ == "__main__": suite=unittest.TestSuite() suite.addTest(ModuleTest("test_add")) suite.addTest(ModuleTest("test_sub")) suite.addTest(ModuleTest("test_mul")) suite.addTest(ModuleTest("test_div")) runner=unittest.TextTestRunner() runner.run(suite) ``` ---- just see appearance test instance by unittest is more specfication and clearly. The way of use unittest 1. import unittest and create ModuleTest class to inherit unittest.TestCase class 2. SetUp() and TearDown() these two method is more special.they are execute when startup and endup test instance. - SetUp():init work , init variable,produce database testdata open browser - TearDown(): clear testdata in database close file close browser - PS: unittest asked us when we use method ,method should start with [test] Ex;test_add 3. call unittest.TestSuite() class addTest() method to produce test instance 4. test instance with unittest.TextTestRunner() class run() method ---- ### Django Test Django,s unittest called [django.test.TestCase] it is inherit from unittest.TestCase ---- ##### a simple example [test.py] ```python= from django.test import TestCase from sign.models import Event,Guest class ModelTest(TestCase): def setUp(self): Event.objects.create(id=1,name='oneplus 3 event',status=True,limit=2000,address='shenzhen',start_time='2016-08-31 02:18:22') Guest.objects.create(id=1,event_id=1,realname='alen',phone='13711001101',email='alan@mail.com',sign=False) def test_event_models(self): result =Event.objects.get(name="oneplus 3 event") self.assertEqual(result.address,'shenzhen') self.assertTrue(result.status) def test_guest_models(self): result=Guest.objects.get(phone="13711001101") self.assertEqual(result.realname,"alen") self.assertFalse(result.sign) ``` ---- Django has offer [test] command to test unittest and you must not execute single test.py ```bash= python3 manage.py test ``` PS: setUp() won,t insert new data in real database you won,t worry that! ---- ##### execute test instance the test instance will be more and more ,test time will more longer,we can choose which we want to execute test instance ```cmd // execute sign app all test python3 manage.py test sign // execute sign app >test.py testfile python3 manage.py test sign.test // execute sign app test.py testfile ModelTest testclass python3 manage.py test sign.tests.ModelTest // execute ModelTest testclass test_event_models method python3 manage.py test sign.tests.ModelTest_event_models // we can add -p (--pattern)parameter to diff testfile python3 manage.py manage.py test -p test*.py to specify testfile :test*.py meaning which file is startwith test and endwith .py * is any char ``` ---- ### Client Side testing In django. django.test.Client class like a virtual web browser it can test view and django app and method to interact with each other ---- ###### django.test.Client class can do - simulation [GET] and [POST] request and check response . from http status code to webpage info, - check redirect link(if has it), and check URL and status code in every step - use a involve specify value template context to test a request be django template render ---- Enter Djnago shell mode ```cmd= python3 manage.py shell from django.test.utils import setup_test_environment setup_test_environment() ``` setp_test_environment() used to init test environment ```cmd= from django.test import Client c=Client() response=c.get('/index/') response.statuse_code 200 ``` to test index view Client class offer get() and post() method to simulate GET/POST request. By reference get() request [/index/] directory print http status code 200 meaning success ---- ##### test index tests.py ```python= from django.test import TestCase class IndexPageTest(TestCase): def test_index_page_renders_index_template(self): response=self.client.get('/index/') self.assertEqual(response.status_code,200) self.assertTemplateUsed(response,'index.html') ``` Althought we didn,t import django.test.Client class,but self.sclient finally call as same as django.test.Client by client.get method request [/index/] directory. status_code get HTTP status code.use assertEqual() to distinct that is 200 or not . assertTemplateUsed() can distinct is index.html template or not. ---- ##### test login action tests.py ```python= from django.contrib.auth.models import User class LoginActionTest(TestCase): def setUp(self): User.objects.create_user('admin','admin@mail.com','admin123456') def test_add_admin(self): user=User.objects.get(username="admin") self.assertEqual(user.username,'admin') self.assertEqual(user.email,"admin@mail.com") def test_login_action_password_null(self): test_data={"username":'','password':''} response=self.client.post('/login_action',data=test_data) self.assertEqual(response.status_code,200) self.assertIn(b"username or password error!",response.content) def test_login_action_success(self): test_data={'username':'admin','password':'admin123456'} response=self.client.post('/login_action',data=test_data) self.assertEqual(response.status_code,302) ``` In setUp() init method call User.objects.create_user() to create login user data test_add_admin() to test added user data is correct or not in other instance user post() method request ['/login_action/'] test login function test_data define user parameter{'username':'admin','password':'admin123456'} test_login_action_username_password_null() and test_login_action_username_password_error() instance to distinct test username and test user password is null or error assertIn() response HTML page is involve [username or password error!] hint string test_login_action_success() instance test username and password is correct.Why we assert http status code is 302 not 200?because in login_action view why username authenticate success HttpResponseRedirect() to [/event_manage/] directory.this a redirect so it will response 302 ---- ##### test release conference tests.py ```python= from sign.models import Event class EventManageTest(TestCase): def setUp(TestCase): User.objects.create_user('admin','admin@mail.com','admin123456') Event.objects.create(name='xiaomi5',limit=2000,address='beijing',status=1,start_time='2017-8-10 12:30:00') self.login_user={'username':'admin',"password":'admin123456'} def test_event_manage_success(self): response=self.client.post('/login_action/',data=self.login_user) response=self.post('/event_manage/') self.assertEqual(response.status_code,200) self.assertIn(b'xiaomi5',response.content) self.assertIn(b'beijing',response.content) def test_event_manage_search_success(self): response=self.client.post('/login_action/',data=self.login_user) response=self.client.post('/search_name/',{'name':'xiaomi5'}) self.assertEqual(response.status_code,200) self.assertIn(b'xiaomi5',response.content) self.assertIn(b"beijing",response.content) ``` due to release manage event_manage and release conference name search search_name two view function been @login_require decorater decorate . if want to test these two features must login success and create user data.so you will see every instance been called login function before that. ---- ##### test guest management tests.py ```python= from sign.models import Event,Guest class GuestManageTest(TestCase): def setUp(self): User.objects.create_user('admin','admin@mail.com','admin123456') Event.objects.create(id=1,name="xiaomi5",limit=2000,address="beijing") Guest.objects.create(realname='alen',phone=18611001100,email='alen@mail.com',sign=0,event_id=1) self.login_user={'username':'admin','password':'admin123456'} def test_event_manage_success(self): response=self.client.post('/login_action/',data=self.login_user) response=self.client.post('/guest_manage/') self.assertEqual(response.status_code,200) self.assertIn(b'alen',response.content) self.assertIn(b'188611001100',response.content) def test_guest_manage_search_success(self): response=self.client.post('/login_action/',data=self.login_user) response=self.client.post('/search_phone/',{'phone':'18611001100'}) self.assertEqual(response.status_code,200) self.assertIn(b'alen',response.content) self.assertIn(b'18611001100',response.content) ``` guest_manage and search_phone test need create full data.first login_user data .second is Guest for release conference data, ---- ##### test user sign in tests.py ```python= class SignIndexActionTest(TestCase): def setUp(self): User.objects.create_user('admin','admin@mail.com','admin123456') Event.objects.create(id=1,name='xiaomi5',limit=2000,address='beijing',status=1,start_time='2017-8-10 12:30:00') Event.objects.create(id=2,name='oneplus4',limit=2000,address='shenzhen',status=1,start_time='2017-6-10 12:30:00') Guest.objects.create(realname='alen',phone=18611001100,email='alen@mail.com',sign=0,event_id=1) Guest.objects.create(realname='una',phone=18611001100,email='una@mail.com',sign=1,event_id=2) self.login_user={'username':'admin','password':'admin123456'} def test_sign_index_action_phone_null(self): response=self.client.post('/login_action/',data=self.login_user) response=self.client.post('/sign_index_action/1/',{"phone":""}) self.assertEqual(response.status_code,200) self.assertIn(b'phone',response.content) def test_sign_index_action_phone_or_event_id_error(self): response=self.client.post('/login_action/',data=self.login_user) response=self.client.post('/sign_index_action/2/',{"phone":"18611001100"}) self.assertEqual(response.status_code,200) self.assertIn(b'event id or phone error.',response.content) def test_sign_index_action_user_sign_has(self): response=self.client.post('/login_action/',data=self.login_user) response=self.client.post('/sign_index_action/2/',{"phone":"18611001100"}) self.assertEqual(response.status_code,200) self.assertIn(b'user has sign in.',response.content) def test_sign_index_action_sign_success(self): response=self.client.post('/login_action/',data=self.login_user) response=self.client.post('/sign_index_action/1/',{"phone":"18611001100"}) self.assertEqual(response.status_code,200) self.assertIn(b'sign in success!',response.content) ``` setUp() should create two data [xiaomi5] and [oneplus4] guest [alen]is belong to [xiaomi5] release conference Guest [una] is belong to [oneplus4] release conference and [una] status [sign up] by [alen] 18611001100 in [oneplus4] release conference sign in will hint [event id or phone error] by [una] cellphone sign in will hint[user has sign in.] another two instance distinct cellphone is null and sign in success,it is comparatily good understand --- # Interface Related concept ---- ### layerd automated testing testing pyramid concept is offered by Mick Cohn iom his Succeeding with Agile book.his basic concept is: we should have more lower level unit test rather than use interface execute higher lever peer 2 peer testing ![](https://i.imgur.com/ERQaVRR.png) Martin Fowler master in testing pyramid model concept offer layerd automated testing concept.in before automated testing add [layerd] to decorated it to distinct [traditional] automated testing thinking. So,What is traditional automated testing ?why should promote layerd automated testing thinking? The traditional automated testing can be comprehend as basic a Product UI layer automated testing,it is a type of a automated testing let a black box function test turn on to program or tool to execute. in the currenetly,most of research organization has develop and test group split(department wall) , quality management duty problem,in this condition,testing group normal reflection is try to control black box testing environment in testing gropup and execute any probabily cover even execute any probabily comprehensive UI automated testing. This may will lead to two evil consequences:first is testing group be hurried swell,Second is as know as comprehensive UI automated testing execrise.because UI is very Volatile. So UI automated testing maintain cost is high comparatily layer automated testing promote from black box(UI) single layer to black and white multilayer automated testing system,from comprehend black box aitomated testing to system different layer automated testing for each other. ![](https://i.imgur.com/I9YaoDr.png) ---- ### unittest and moduletest no matter software developer or software testing employee seem that they are familiar with these two concept.when we are developing we only have project directory, program file function and class method didn,t have [unit] and [module] 1. Unit Testing 1. minimal of testing program application 2. Procedure direct,unit also can be whole module.but in common is single function or procedure 3. in OOP unit usually like a interface Ex:class but it can be independece method 4. most of time unit Testing is design by programer ownself 2. Module Testing 1. liite be different with unit testing 2. module testing focus on appearlly funciton features to testing 3. it is only a part of unit test 4. most of condition code by other programer ---- ### InterFace testing 1. Program interface - like program module interface,gerenally it offered input and output class ,method,function features. 2. Translayer interface - generally specify system through different translayer offer interface like HTTP SOAP.this translyer interface let bottom layer code Encapsulation and through translayer call didn,t involved bottom layer code that didn,t restrict by program language. we can through interface testing tools or other program language to testing.most of time complete with testing member. ##### interface classify 1. interface of between system and system 2. interface of bottom layer to top layer service 3. interface of system inside ,between service and service calling ##### testing interface meaning according of automatic testing define,the bottomest layer is developed by developer programs unit test to ensure program quality.the topest layer by function test employee custom + UI automatically to ensure function usabilty,so what is the meaning of interface testing 1. can explore problem in early 2. to short of product research lifecycle 3. explore more bottom layer problem ### Interface of Program language In most of OOP language they offer interface this concept. ##### Java interface interface in java is specify a abstract class , is abstract method of set,In usually we use **interface** to dimension a class can through inherit interface to inherit abstract method interface is not a class,althought program interface is very simaliar with program class but they are not belong to same concept.class descripe object method and instance.a pratice interface class should pratice all interface describe method or must dimension them a abstract class Why we use interface? the feature of interface is it only define specification no matter pratice. ##### Python Zope.interface Python also have interface concept .althought Python origin didn,t offer create and use of interface.but we can through module to use like a interface concept ```python= from zope.interface import Interface from zope.interface.declarations import implementer class IHost(Interface): def goodmorning(self,host): """Say good morning to host""" @implementer(IHost) class Host(object): def goodmorning(self,name): '''Say good morning to guests''' return "Good morning,%s!"%name if __name__ =="__main__": h=Host() hi=h.goodmorning('Tom') print(hi) ``` ---- # develop Web Interface ### Why should develop Web Interface along with frontend technique development and HTMl technique popular,web page can present more lively content and more complex interaction and this has another topic is frontend and backend seperate is a trend in recently year and these are that advantage 1. backend no need to proficient frontend techniqu(HTML5/CSS/Javascript) only should focus on data processing and offer Web Interface 2. frontend more and more profession through called Web Interface to get data and focus on data showing and web page interaction design 3. Web interface application more widely, due to backend develop interface cab offer to Web page and also can offer to mobile app; can offer to internal company and company outside to called. ### What is Web Interface When we request a page will response server resource it involve HTML CSS Javascript aside from these , server also will return image video font and extensions ...resource and these resource are transfer by HTTP/HTTPS and through data formattion (CSV/XML/JSON) ##### HTTP Full name HyperText Transfer Protocal.HTTP is Base on TCP/IP transfer data (HTML Image Media...) HTTP works on general service structure.HTTP through browser and url request HTTP Service Provider 1. HTTP features - No connect: meaning restrict every connect only process one request。After Server Processed Client request and receive Client response then disconnect, use this method can save transfer time. - Independent Media: we just need only know Client side and server side how to process data content ,Every type of data can through HTTP to transfer。Client Side and Server speficy suitable MIME-type content - No Status:HTTP is no status meaning protocal didn,t has memerize ability for deal . Leak on status meaning when we dealing need predata that server should resend data may cause data size bigger,if server no need predata that its response will fast. 2. HTTP request Method - According to HTTP standard HTTP request can be different type - HTTP 1.0 defined 3 type of request method GET POST HEAD - HTTP 1.1 added 5 type of request method OPTIONS PUT DELETE TRACE CONNECT 3. response status code - before when browser receive and present this webpage server will retuen a serverHeader involve HTTP status code to response browser request - HTTP status code divide into 5 types: - 1XX :infromation, when server recieve request,need requester continue execute operation - 2XX :Success,operation recieve and processed - 3XX :Redirect need further operate to complete request - 4XX :Client Side Error, request involve syntax error or cannot complete request - 5XX :Server Side Error,has error when Server processing request - Common Status Code and complaination - 200 OK: request success - 302 Fund: temporary moved,resource tempoary moved Client side should continue use Origin URI - 400 Bad Request: Client Side request syntax error cannot comprehend by server side - 401 Unauthorized: request asked Client identify - 403 Forbidden: Server cannot accoring Client side request but reject this request - 404 Not Found: Server cannot according Client request to find resource - 500 Internal Server Error: Internal Server Error cannot complete request - 503 Server Unavailable: due to overloading or system maintain server cannot process client side request temporary 4. request head infromation and response header infromation 1. request header infromation - request header accpet Client side to Server Side transfer request added infromation and Client side infromation common headers as follows - Accept: browser can accept MIME type - Accept-Encoding: Browser can decoding data encoding type Ex gzip - Accept-Language: Browser excepted language type.When server offer more than one type of language will use it - Connection: persent whether should keep connect.from HTTP/1.1 ,all open Kepp-Alive in default to maintain connection - Host: initail URL host and port.it usually from HTTP URL extracted - User-Agent: request header region accept Client Side let his OS, browser and other property tell server 2. response header infromation - reponse header accept server transfer added response infromation which cannot put in status line can related to server infromation and for Request-URI identity resource access further information common headers as follows - Content-Type: meaing behind file belong which MIME type - Date: recent GMT time - Server: involve server to process request software infromation - X-Frame-Options: for browser instruct a page which can markup in "<frame iframe object>.Website can use this features to ensure own website content has inject to other website to avoid click hijack actack ##### JSON fromat JSON(JavaScript Object Notation) is a lightweight data transfer format it independent on language and platformm, JSON parser and JSON ware support differernt program language.JSON has self-describing it is easy to comprehend ### develop System Web Interface ##### disposition interface directory method of develop a Web interface as same as develop system to distinguish we set Web Interface root directory is [/api] throught two layer directory present pratice specific function interface ### Write a Web Interface Document write Web Interface document is a important part .because development interface is called by other development,but how to know interface been called? we must refer interface document.So interface document should update immediately content correctlly EX: conference interface | Name | add conference | | ----------- | -------------------------------------------- | | description | add conference | | URL | http://127.0.0.1:8000/api/add_event | | call method | POST | | parameter | eid,name,limit,status,address,start_time | | return | {"status":200,"message":"add event success"} | ---- # Introduce Interface testing tools There are very kinds of interface testing tool we classify three types 1. Interface testing tool - features easier than others can simulate and send HTTP request and show return interface data Ex:HttpRequester ,Postman... 3. Interface automation test tool - features richer than Interface testing tool.gernerally offer batch execution,interface return assertion authorization and testing report creataion Ex: JMeter,Robot Framework,soapUI 5. Interface efficacy test tool - Mainlly use testing interface efficacy,authorize interface process parallel ability Ex:JMeter,LoadRunner,soapUI ### Postman ### JMeter ### Robot Framework ---- # Interface automation tesing framework ### defect of interface testing tool 1. testing data cannot control - Testing interface is essence of testing on data ,call interface enter some data and authorize interface return data correctness.if interface return data cannot control that will cause cannot authorize automation testing return data,however gerneral testing tool didn,t have initalize testing data cannot testing [automatically] really 3. cannot testing encrpted interface - this is a anthoer defect of general testing tool.Acctually in project most of interface cannot called casually ,we will use User authorization signature encrypt... methods to improve interface security. general interface testing tool can not simulate and generate these encrypt algorithm 5. lack of expanding ability - when we enjoy the convience brought by tool ,mean while will restricted by restriction of tool.however interface testing tool cannot pratice features expanding or need complex method to pratice.In contrast program language didn,t exist this restriction ### Request Library Request use Apache2 Licensed authorized HTTP Library .it is based on urllib3 and extended all the feature of urllib3.Request support HTTP persist connection and connect pond and also support Cookie and Session ,support file upload ,support ensure reply content encoding automatically ,support international URL and POST data encoding ##### interface testing ```python= import requests url="http://127.0.0.1:8000/api/get_event_list/" r=request.get(url,params={'eid':'1'}) result=r.json() assert result['status']==200 assert result['message']=='success' assert result['data']['name']=='紅米手機發佈會' assert result['data']['address']=='台北國際會議中心' assert result['data']['start_time']=='2018-09-20T14:00:00' ``` ##### Integrate unittest ```python= import requests import unittest class GetEventListTest(unittest.TestCase): def setUp(self): self.url='http://127.0.0.1:8000/api/get_event_list' def test_get_event_null(self): r=requests.get(self.url,params={'eid':''}) result=r.json() self.assertEqual(result['status'],10021) self.assertEqual(result['message'],"parameter error") def test_get_event_error(self): r=requests.get(self.url,params={'eid':'901'}) result=r.json() self.assertEqual(result['status'],10022) self.assertEqual(result['message'],"query result is enpty") def test_get_event_success(self): r=requests.get(self.url,params={'eid':'1'}) result=r.json() self.assertEqual(result['status'],10022) self.assertEqual(result['message'],"success") self.assertEqual(result['data']['name'],'小米5發佈會') self.assertEqual(result['data']['address'],'北京國家會議中心') self.assertEqual(result['data']['start_time'],'2016-12-08T14:29:21') if __name__=="__main__": unittest.main() ``` ##### interface testing framework development according to interface testing automatically,unittest has helped us completed most of work,only need initailize testing data and use HTMLTestRunner to produce a report a interface testing framework is mostlly complete! ###### framework procedure ![](https://i.imgur.com/6wyLyPs.png) 1. interface testing framework will insert data to testing database 2. call been tested system offered interface 3. system interface according parameter to testing database search parameter 4. combine all of search result to a specify format and return to testing framework 5. through unittest framework assertion authorize interface ruturn data and produce report # interface security mechanism ### User authorization Gernerally interface testing tools will offer a User Auth/Authorization this option and asked type username/password columns.this is different with login system account/password.login system username/password is parameter transfer through interface.but User Auth is involve in request ##### Develop with Auth interface ```python= from django.contrib import auth as django_auth import base64 #user authorization def user_auth(request): get_http_auth=request.META.get('HTTP_AUTHORIZATION',b'') auth=get_http_auth.split() try: auth+parts=base64.b64decode(auth[1]).decode('utf-8').partition(':') except IndexError: return 'null' username,password=auth_parts[0],auth_parts[2] user=django_auth.authenticate(username=username,password=password) if user is not None: django_auth.login(request,user) return 'success' else: return 'fail' ``` request.META is a python dictionary it involve this time HTTP request and HEADER infromation like user authorization,ip and user Agent(usually is browser name and version...) HTTP_AUTHORIZATION used to get HTTP authorization data if it is empty will get a empty byte object ### Digital Signature When using HTTP/SOAP transfer protocal,signature is a parameter and has important feature 1. authorization[through Client side key and Server side key matching] - Ex: http://127.0.0.1:8000/api/?a=1&b=2 - Assume signature key is @admin123 - After added parameter => http://127.0.0.1:8000/api/?a=1&b=2&sign=@admin123 - It is obviously not safe so gerernally will through encrypt algorithm to encrpyt them Like MD5 - after encrypt => http://127.0.0.1:8000/api/?a=1&b=2&sign=4b9db269c5f978e1264480b0a7619eea ```python= import hashlib md5=hashlib.md5() sign_str="@admin123" sign_bytes_utf8=sign_str.encode(encoding='utf-8') md5.update(sign_bytes_utf8) md5.hexdigest()#4b9db269c5f978e1264480b0a7619eea ``` 2. data protect edited ### interface encrypt ##### PyCrypto library Pycrypto is a free encrypt algroithm library it supports common DES AED and MD5 SHA ... HASH calculation Ex1 SHA256 ```python= from Crypto.Hash import SHA256 hash =SHA256.new() hash.update(b'message') hash.digest()#Produce message to a encrypt code hash.hexdigest()# 16 bit encode ``` Ex2 AES ```python= from Crypto.Cipher import AES obj=AES.new("This is a key123",AES_MODE_CBC,'This is an IV456') message="The answer is no" ciphertext=obj.encrypt(message)#encrypt obj2=AES.new('This is a key123',AES_MODE_CBC,"This is an IV456") obj2.decrypt(ciphertext)#decrypt ``` Ex3 Random algorithm ```python= from Crypto.Random import random random.choice(['dogs','cats','bears']) ``` # Web Services ### Related of Web Service concept 1. SOA(Service-Oriented Architecture) - SOA propose in business calculate realms must let feature dependency too close close coupling system divide into business oriented thick-tincy soft-coupling no-status service. service release let other service calling. a couple of depend each other service make a SOA architeture system - now that it called a architeture.gerenally think SOA involve execute environment,program template , architeture style and relation method thoery. - SOA in essence is a service set .they can communication in each protocal .these protocal can be easily transer data also can be two or more service coordination to being some event. needed some method to connect in each serivce - so-called service is accuratly definition package completion and indenpendent from other service environment and status function - although different company and person has different understand in SOA but we still can see some key definition - For SOA we didn,t need too seriously define SOA is what a architeture.just meet its definition and rules software system can be SOA architeture 2. SOA and Web Service - In 1996 Gartner forward-looking propose SOA thinking SOA expound [For complex company IT System should meet differently can reuse tiny dividde,let feature related features offerer comprise together for consumer offer service] its aim is solve business internal different IT resource cannot connect each other infromation isolation problem - until 2000 recent ESB(Enterprise Service Bus),Web Service ,SOAP ..technology appearance to make SOA application more implement mean while more company like IBM Oracle.. propose solve suite and product base on SOA - beccause almost all SOA appliance is bind with Web Service so these two concept very common to mixed use.it is undeniable Web Service is suitable for SOA.SOA,s population has lots of reason is Web Servie standard mature and application common,most of guys agree Web Service technology present SOA need in every aspect - first is base on access and visit individual feature entity satisfy soft coupling needed: in Web Service all of access through SOAP to access use WSDL definition interface package through UDDI to search in directory can dynamic change a service offer direction and won,t influnt client side settings .outside client side fundamental no need to care about access server side entity - second suitable big data and low frequency access satisfy big chunck feature:base on efficacy and efficient meet SOA service ofer big chunck application feature and cross system edge access frequency won,t frequent in each program throught WSDL and base on literal SOAP request can pratioce Disposable recevice a lot data - finally base on standard pure text message transfer for different architeture offer protocal technism.Web Service all transfer protocal through SOAP to do that and SOAP is base on XML. XML is a sturtured pure text message .from oldest EDI(Eletronic Data Interchange) start pure text message may be a best way in different architeture trasfer format suit on emphasis SOA architeture for different architeture and host system transparency - combine the above points.Web Service indeed a best way SOA.however In terms of SOA itself.Not necessarily use Web Service to pratice .The most important thing is SOA essence.along with people knowledge and concept increasely will have more and more texhnism been develop. - [SOA is not Web Service ,Web Service is best pratice SOA technism] 3. Web Services - HTTP is most common internet transfer protocal however WebServicce is a deployment in Web object or application component,Web Service data transfering saming needed HTTP 4. SOAP - Simple Object Access Protocal SOAP is based on XML in distribute or distribution environment change data simple transfer protocal,accept web service offerer and client through firewall communication in internet - SOAP,s design for a soft , Scattered environment use xml exchange strutured infromation offer a easy and Featherweight technism - When SOAP message really need tranfer in Internet .SOAP message can bind with other different bottom layer transfer protocal, meanwhile SOAP message can use in multiple trasfer mode it involve HTTP SMTP MIME and also support from message system call RPC... Whole application, - Off course most of condition we bind on HTTP ,So it result in many people think SOAP is HTTP + XML or think SOAP is HTTP,s POST request a own version. keeping a type of special XML message format . although we see it is correct but obviously these point for SOAP definition is wrong. 5. WSDL - Web Service Description Language it based on XML language to describe Web Service and how access from them - WSDL document mainly use these element to describe some Web Service - <portType> :Web Service execute operation</portType> - <message> :Web Service use message</message> - <types> :Web Service use data type</types> - <binding> :Web Service use tranfer protocal</binding> 1. WSDL prot it descibe Web Service can be executed operation and related message 2. WSDL message define a operation data element every message are combind with one or many component can think tey are parameter in function 3. WSDL types define Web Servicce use data type for maximum platform neutrality ,WSDL use XML Schema sytax to define data type 4. WSDL bindings define every port and protocal detail.for interface ,interface document is very important,it describe how to access interface .WSDL can think as Web Service a standard formation document.we through reading WSDL to know how to call web service interface 6. UDDI - Universal Description Discovery and Integration - WSDL use it to visit Web Service some related infromation then that in internet or in business different department how to discover our needed Web Service ? and Web Service offeree how let its develop Web Service deploy to internet ? this time should use UDDI - UDDI is a independent in platform framework through internet to describe service and discover business and integrate with business service - UDDI is universe describe discover and integrate service - UDDI is a use to store relate web service information directory - UDDI is a WSDL describe Web Service interface directory - UDDI thriught SOAP to communicate ### Web Service development and call Python is not suitable for development Web Service Interface and develop Web Service interface didn,t have advantage in efficeny but Python is easy and useful every application can found corresponding library,Web Services development and call also not to mention ##### suds-jurko call interface Suds us Web Service client side light level and base on SOAP python client according to PyPI warehousr version.this project end to update from 2010/9 but Python 2 User also can use this project Suds-jurko is base on Suds.its aim is expand the origin suds but this project is end to update from 2014/1 but it also support by Python 3 wexml.com.cn offer some deployed Web Service interface By this common Web Service Interface we can through Suds-jurko to call this interface .For example "use cellphone number belong district create soap_client.py" ```python= from suds.client import Client url="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl" client=Client(url) print(client) ``` ```cmd= python3 soup_client.py Suds (https://fedorahosted.org/suds/) version: 0.6 Service (MobileCodeWS ) tns="http://WebXml.com.cn/" Prefixes (1) ns0 ="http://WebXml.com.cn/" Ports (2): (MobileCodeWSSoap) Methods (2): getDatabaseInfo() getMobileCodeInfo(xs:string mobileCode , xs:string userID) Types (1): ArrayOfString (MobileCodeWSSoap12) Methods (2): getDatabaseInfo() getMobileCodeInfo(xs:string mobileCode,xs:string userID) Types (1) ArrayOfString ``` through check interface return information to realize interface offer getMobileCodeInfo() method to check mobile number come from ,method recieve two parameter mobileCode and userID.mobileCode to be searched cellphone number and userID is registed ramdomly distributed user ID.but after tested we discover userID is not nessary to call interface After known Web Service concrete method and parameter then we can call interface ```python= from suds.client import Client url="http://ws.webxml.com.cn/zh_zn/web_services.aspx" client=Client(url) result=client.service.getMobileCodeInfo('186XXXXXXXX') print(result) ``` To keep security i mean let mobile number last 8 number use "X" ```cmd= python3 soap_client.py #186XXXXXXXX :北京 北京 北京聯通GSM卡 ``` Web Services interface calling is very easy and we try it to search climate ```python= from suds.client import Client from suds.xsd.doctor import ImportDoctor ,Import url="http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl" imp=Import("http://www.w3.org//2001/XMLSchema",location="http://www.w3.org/2001/XMLSchema.xsd") imp.fliter.add("http://WebXml.com.cn/") client=Client(url,plugins=[ImportDoctor(imp)]) result=client.service.getWeatherbyCityName("北京") print(result) ``` ---- ##### spyne develop interface Compare to Web Service interface calling , i am more wandering Web Service interface how to develop it,case through before concepty we can find it.it look like a very complex technical but acctually we can find application library about develop web service in python soaplib is a easy,easy to expandation SOAP library it used to create and deploy SOAP web service tool however this project was be canneled in 2011/03 but Python 2 user still can use that library tto develop Web Service application spyne is a unrelative to structure transfer RPC library ,focus on public service and have good defined APIS it is still renewing Web Service application .it is easy to use as same as soaplib and soupported python 3 ```python= from spyne import Application,rpc,ServiceBase,Iterable,Integer,Unicode from spyne.server.wsgi import WsgiApplication from spyne.protocal.soap import Soap11 class HelloWorldService(ServiceBase): @rpc(Unicode,Integer,_returns=Iterable(Unicode)) def say_hello(ctx,name,times): for i in range(times): yeild 'Hello , %s' %name application=Application([HelloWorldService], tns='spyne.examples.hello', in_protocal=Soap11(validator='lxml'), out_protocal=Soap11() ) if __name__ == "__main__": from wsgiref.simple_server import make_server wsgi_app =WsgiApplication(application) server=make_server('192.168.127.131',8000,wsgi_app) server.serve_forever() ``` we recommaned you execute that program in linux ,Here develop a say_hello() interface it need to parameter name and times .Interface will for name in times [hello,name], it is quitly wasy 192.168.127.131 is host IP addressm,8000 is port number can be a Web Service server ---- ### JMeter test SOAP interface open Jmeter tool create SOAP interface testing.click [threading group] and in feature table select [Sampler] ->[SOAP/XML-RPC Request] for example use Chinese cellphone number search interface "http://ws.websxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl" check User KeepAlive checkbox in Soap/XML-RPC Data textfield fill XML format SOAP request data. if you don,t understand how to describe interface parameter can use Suds-jurko to wirte script call interface and through Wireshark to catch package catch SOAP request --- # REST ### RPC and REST 1. RPC - RPC is a Remote Procedure Call abbreviation.it is Web Service domain popular develop style - RPC style development focus on Server/Client side method calling ,not care about which network layer or which protocal to base on.RPC style representive is XML-RPC and Web Service 1. XML-RPC - XML-RPC is a use XML formation package method and use HTTP protocal be their transfer protval's RPC style implementation .XML-RPC request method always through HTTP POST method request and response are XML formation - XML-RPC is a overtime technical is have been SOAP replaced,test instance manage system TestLink for outside interface which developed by PHP XML-RPC 2. Big Web Service - Big Web Service is base on SOAP+WSDL+UDDI ...technical to implement RPC style big web service summary 2. REST - Representational State Transfer (REST) - REST has multilanguage cross platform feature it a web service which allow REST style - If a structure meet REST priciple and we call t RESTful structure.To comprehend RESTul structure the best way is to comprehend Representational State Transfer this phase meaning.every paragraph meaning if you understand this noun that you will know REST is what design. 1. Resource - REST's name [presentational layer state transfer] it ignore the subject.[presentational layer] is specifyu the [resoure]'s [presentational layer]' - The [resource] is a network entity.or say is a concrete information on the interface .it just a string a picture a music a service .you can use a URI(unify resource identity) to locate it ,every source match a specify URI.If want to receive that resource .we can visit that URI therefore URI being every resource address or the unique idenitification symbloism - The [surf the internet] is interact with ervery [resource] that in the internet calling its URI 2. Representation - [Reousece] is a information entity,it has many outside present type.we present [resource] type called its [Representation] - URI only represent resource entity not representate its type the file extension is not nesssary. and URI should only representate [resource] address it concrete type should add Header in http request use Accept and Content-Type columns to specify it.these two column are described the [represntational layer] 3. State Transfer - To visit a website ,it representate Client Side and Server Side interactions.In this interaction will involve data and state transfer - HTTP Protocal is a non-state protocal .this is meaning all the state will store in server side therefore if Client Side want to operate Server should through some way let Server Side State Transfer and this trasfer is build on representational layer so is [Representational later state transfer] - Client side use means only can be HTTP.In concretely is GET POST PUT DELETE in http four method - Comprehensive the upside explanination ,let's summary about what is RESTful structure - every URI represent a resource - Between client side and server side trasfer presentation layer - Client through this four HTTP verb to operate server side resource to implement [Representation layer transfer] - Related to Web Services related concept has introduced.we use the following picture to explain ecery concept and technology involve relationship - ![](https://i.imgur.com/7woaXLE.png) ### Django REST Framework Django REST Framework As the name suggests it is a base on django REST style framework if has the following features: - Feature powerful flexable can help you develop more fast - Support authentication invovlve OAuth 1a and OAuth 2 - Support ORM and non-ORM data resource serialize - Rich document and goor social media support ##### Test interface > rest_test.py ```python= import unittest import requests class UserTest(unittest.TestCase): def setUp(self): self.base_url='hhttp://127.0.0.1:8000/users' self.auth=('admin','admin123456') def test_user1(self): r=requests.get(self.base_url+'/1/',auth=self.auth) request=r.json() self.assertEqual(result['username'],'admin') self.assertEqual(result['email'],'admin@mail.com') def test_user2(self): r=requests.get(self.base_url+'/2/',auth=self.auth) result=r.json() self.assertEqual(result['username','tom']) self.assertEqual(result['email'],'tom@mail.com') def test_user3(self): r=requests.get(self.base_url+'/3/',auth=self.auth) result=r.json() self.assertEqual(result['username','jack']) self.assertEqual(result['email'],'jack@mail.com') class GroupsTest(unittest.TestCase): def setUp(self): self.base_url='http://127.0.0.1:8000/groups' self.auth=('admin','admin123456') def test_groups1(self): r=requests.get(self.base_url+'/1/',auth=self.auth) request=r.json() self.assertEqual(result['name'],'test') def test_user2(self): r=requests.get(self.base_url+'/2/',auth=self.auth) result=r.json() self.assertEqual(result['name','developer']) if __name__ =="__main__": unittest.main() ``` --- # Django project deploy ### uWSGI ##### uWSGI introduction What is WSGI? WSGI full name Web Server Gateway Interface,it is design for python to define Web server and Web application or between in framework a simple and common used interface WSGI is Web server and Web application or between in framework a low layer interface to increase portable web application common point .Many python web framework also built-in WSGI server like ":lask,webpy,Django ...but built-in WSGI is normal efficiency.gernerally use in project deploy testing ,most of time use production WSGI or use uWSGI which develop by Nginx What is uWSGI? uWSGI is a Web server it implement WSGI ,uWSGI,HTTP..protcal In Nginx ,ngx_http_uwsgi_module function is change with uWSGI sercer about WSGI,uwsgi,uWSGI segement in three part - WSGI is a interface which in Web server and Web application or application framework also we can recognize it is a protocal - uwsgi in a transfer protocal - uWSGI is a web server which implement uwsgi and WSGI two protocal uwsgi protocal is a uWSGI server built-in porotcal.it is used to define transfer information type.Every uwsgi packet the forest 4 byte is transfer information type description .It is different with WSGI protocal ##### install uWSGI create test.py file ```python= def application(env,start_response): start_response('200 OK',[('Content-Type'),'text/html']) return [b"Hello World"] ``` Ubuntu terminal ```bash= $ uwsgi --http :8001 --wsgi-file test.py ``` ##### use uWSGI to execute Django Ubuntu Terminal ```bash= $ uwsgi --http :8000 --chdir /home/fnngj/pydj/guest/ --wsgi-file guest/wsgi.py --master --processes 4 --stats 127.0.0.1:9191 ``` uwsgi command parameter as the following 1. --http : protcal type and port number 2. --processes : open processes number 3. --workers : open processe number same as --processes 4. --chdir : specify execute directory 5. --wsgi-file : load wsgi-file (load wsgi.py) 6. --stats : in specify address open statement service 7. --threads: open threading number 8. --master : allow master processes exist 9. --daemoniz : let process execute in backend and let logging file output to specify logging file or UDP server. 10. --pidfile : specify PID file directory,to record master process PID number(PID,service process PID) 11. --vacuum : when server exit will auto clean enviroment,delete Unix Socket file and PID file ### Nginx Nginx is a feather weight Web Serber/Reverse Proxy Server and Email(IMAP/POP3) proxy server release in a BSD-like protocal Use Nginx +uWSGI this combination to depoly Django is a common depoly way.In this way we usually use Nginx as frontend,use it to receive all the Web Request and unified management request.Nginx can deal with all the static request this is Nginx advantage however put all the non-static request thourgh uWSGI transfer to Django to complete a web request that uWSGI function like a bridge can play connect Nginx and Django feature ##### Nginx +uWSGI +Django django_uwsgi.ini ```ini= # Request Method and port number socket = :8000 # Django Project Route chdir = /home/fnngj/pydj/guest # wsgi file module = guest.wsgi # Allow main process exist? master=true #Open process number processes =3 # When Server exit clean environment automatically vacuum = true ``` - socket: specify request method and port number - through uWSGI to visit Django directly should change it to http - through Nginx request uWSGI to visit Django should use socket Use uwsgi to open Web Project ```bash= uwsgi --ini django_uwsgi.ini ``` Setup Nginx config /etc/nginx/sites-available/default ```nginx= server{ listen 8089; listen [::]:8089; server_name 127.0.0.1 192.168.127.134; location /{ include /etc/nginx/uwsgi_params; uwsgi_pass 127.0.0.1:8000; } } ``` this is a vary simple configuration listen specify Nginx proxy uWSGI output port server_name specify Nginx proxy uWSGI output IP address; can specify multiple IP or functional variable name,127.0.0.1 direct localhost ,192.168.127.134 is localhost IP address,To config these IP address is for other computer in LAN can visit it How to Nginx convert request to uWSGI? this mainly config is these two lines. >include /etc/nginx/uwsgi_params; >uwsgi_pass 127.0.0.1:8000; include must specify uwsgi_params file if startup failed then should specify that file absolute directory it usually put under the /etc/nginx/: however uwsgi_pass specify local IP address must same as guest_uwsgi.ini configuration file after setupo can visit http:127.0.0.1:8089 or http:192.168.127.134:8089 When we visit the page request will through Nginx then convert to uWSGI Web container to deal with ##### Process static file When we visit roll call page we will found that all the static file cannot visit open /etc/nginx/sites-available/default file to add the static file of Web Project ```nginx= server{ listen 8089; listen [::]:8089; server_name 127.0.0.1 192.168.127.134; location / { include /etc/nginx/uwsgi_params; uwsgi_pass 127.0.0.1:8000; } #Setup static file directory location /static { alias /home/fnngj/pydj/guest/sign/static } } ``` guest project static files directory is in .../sign/static so the sonfiguration like this behind.Restart Nginx then visit roll call page style file can be reference normally ### Create 404 page Nomatter use Django [runserver] command or through Nginx+uWSGI to execute project ,if the visit direcotry is not exist that will show no found When the project been depolied this page must not be seen by user.One part is not secure cause it will expose some Django detailer error response anthoer hand is this page is not beautiful.Open project .../guest/setting.py close Django debug mode setting.py ```python= DEBUG = False ``` after close debug should setup ALLOWED_HOSTS ALLOWED_HOSTS cam restrict request host value to avoid hacker construct package to send request.Only the host of in the list can be visit.Gernerally we didn,t recommend you to use * to config,when DEBUG are setup to False should setting it or will be thrown Exception --- # Interface Effeciency Testing ### Locust Effeceiency Testing Tool When we bring up efficiency testing tool the first one flashing in my mind in LoadRunner or JMeter LoadRunner is a popular Business Efficiency Testing Tool it is very powerful but it is more complex,currenetly most of article which introduce interface efficiency testing is based on that toll,even if some book fully talk about how to use LoadRunner. JMeter is same as very popular open source efficiency testing tool and it has complete feature.In this book will use it for example.In fact it is a standard efficiency testing tool .The related data of JMeter is very rich too.it's official document is also comsummate. Locust same as a efficiency testing tool.Offical say:[An open source load testing tool],but it has apparently different with the above of two.In contrast it has fewer feature but not have non-merit Locust is fully base on Python use PurePython to describe testing script and HTTP request it base on Requests library .Aside from HTTP/HTTPS protocal Locust also can testing other protocal system only need to use python corresponding library to request description. LoadRunner and JMeter these two use procesess and threading testing tool are hard to stimulate higher parelle presure in single machine.Locust parelle mechinism reject procesess and threading use third party assistant program gevent's technism to help program prevent systemic resource schedule therefore can improve single machine parelle ability highly. That is base on this feature.I choose to use Locust tool to my efficiency testing tool another reason is it can let us use another way to realize efficiency testing will see the essence of efficiency easierly. ##### Install Locust although Locust still can throught pip to insytall but if you use Python3 we recommend you copy and download from GitHub to install [Github Link](https://github.com/locustio/locust) Locust is based on gevent flask requests msgpack-python six pyzmq - gevent: implement to assist program a assitant manufacture library in Python.another called in Coroutine use gevent can receive high paralle efficiency - flask: Web framwework in Python same as Django - requests: Use it to test HTTP interface - msgpack-python: a fast compact serial binary formation like JSON data - six: it offer some easy tool to pack up between Python2/3 different - pyzmq: if you intend ;et Locust execute in multithreading/multimachine ,i suggest you install in pyzmq When we install Locust it will check out currenet Python environment whether we have installed these library ,if hasn,t.it will install them before install locust.It also restrict from these library version some of them is must equal to some version some of them is more than that version.We also can meet the requirement to install these library that will fast in install Locust To checkout install succuess or not.Open windows terminal enter **locust --help** then press enter key ##### effiency testing case First do a easy case.To familiar with Locust tool base use flow.If use LoadRunner effiency testing tool.We should first flash in your mind is how to record/write a effiency testing script.In fact for the web application.It is from a Web Page to construct in essence can through different URL to get different page 1. Write effiency testing script - Use Locust write a easy effiency testing behavior description script - ```python= from locust import HttpLocust ,TaskSet,task class UserBehavior(TaskSet): @task def baidu_page(self): def.client.get("/") class WebSiteUser(HttpLocust): task_set=UserBehavior min_wait=3000 max_wait=6000 ``` - UserBehavior class inherit TaskSet ckass to describe user behavior - baidu_page() method present a user behavior ,visit baidu use @task decorate that method to a task.client.get() to specify request path "/",therefore it is baidu index,so specify for a root path - WebSiteUser class use to setting effiency Testing - task_set: direct to a defined user behavior class - min_wait: execute user waiting time low limit among task (ms) - max_wait: execute user waiting time high limit among task (ms) 2. Execute Effiency testing - first open effiency testing - ```cmd= locust -f locustfile.py --host=https://www.baidu.com ``` - - -f to specify effiency testing command file - - --host to set up application URL address. Notice that visit baidu use HTTPS protocal - Through browser to visit :http://127.0.0.1:8089(Locust open network monitor default port number is 8089) - - Number of Users to simulate: set upo simulate user number - - Hatch rate(user spawned/second):produce (open) virtual user figure per second - Press [Start swarming] button to execute effiency testing - effiency testing parameter like these - - Type: request type EX:GET/POST - - Name: request path - - request: currenet request number - - fails: current fail request number - - Median: Median value unit million second,half server response time lower than value another higher than that value - - Average: Average vqaluie unit million second,all request average response time - - Min: request minimum server response time unit million second - - Max: request maximum server response time unit milloin second - - Content Size: unit request size unit byte - - reqs/sec: request number per second - About effiency testing result analysis.Due to baidu index efficiency testing,effiency requirement unknown,network environment more complex therefore non-neccesary for analysis. ##### Write Effiency Testing script When after effiency testing prepare the following according to cusiness analysis condition use Locust write effiency testing script ```python= from locust import HttpLocust,TaskSet,task #Web Effiency testing class UserBehavior(TaskSet): def on_start(self): ''' on_start is called when a Locust start before any task is scheduled ''' self.login() def login(self): self.client.post("/login_action".{"username":"admin","password":"admin123456"}) @task(2) def event_message(self): self.client.get("/event_message/") @task(2) def guest_message(self): self.client.get("/guest_message") @task(1) def search_phone(self): self.client.get("/search_phone",params={'phone':"13800112541"}) class WebsiteUser(HttpLocust): task_set=UserBehavior min_wait=3000 max_wait=6000 ``` Due to visit conference manage page,guest page and guest cellphone search behavior their precondition is user must logined ,therefore on_start method defined every locust user first thing need to do is login through @task decorated method to a task method parameter use to specify that behavior execution weight.the parameter more high will have more probability to been virtual user execute.If we don,t setup then default value is 1 conference manage page guset manage page and guest search feature execution weight is 2:2:1 min_wait and max_wait use to specify user execute task low limit and high limit that is 3~6 seconds it is close to user real behavior As for every path of thing is what?use Get or Post and need parameter or nor : all of them can through django project view function definition to decide call method is same as Requests library ##### Execute effiency testing cmd ```cmd= locust -f locustfile.py --host=http://192.168.127.134:8089 ``` http://192.168.127.134:8089 is conference roll call system depoly IP and port number - through browser to visit Locust tool :http://127.0.0.1:8089 - Number of users to simulate : setup simulate user equal to 100 - Hatch rate (users spawned/second): spawned(started) users number to 10 per second Click [Start swarming] button execute effiency testing Click [Next test] button reset virtual user and execute them ### Interface effiency testing Interface effiency testing compare to system effiency testing is easier we don,t need to care about business factor and user behavior only need to simulate calling interface authenticate interface maximum processess ability Interface effiency testing requirement Consider to guset roll call feature,conference need muiltiaisle in paralle let guset to roll call need enough authenticate roll call interface processes ability in paralle ##### Write interface effiency testing script through Locust write interface effiency testing script ```python= from locust import HttpLocust ,TaskSet,task from random import randint #Web interface testing class UserBehavior(TaskSet): @task() def user_sign(self): number=randint(1,3001) phone=13800110000+number str_phone=str(phone) self.client.post("/api/user_sign/",data={"eid":"1","phone":str_phone}) class WebsiteUser(HttpLocust): task_set=UserBehavior min_wait=0 max_wait=0 ``` In effiency testing script add a random spawner when every simulate user execute roll behavior will random spawn a cellphone to roll call this aim is try to let every roll call request cellphone not same but random cannot assure every viretual user request not repeat completely When we construct system effiency testing we have instared 3000 record of guest information.We let these 3000 user being roll call.Due to interface testing we setup min_wait and max_wait equal to 0 million second not to consider thinking time ##### Execute interface effiency testing interface testing and system effiency testing is different.Gernerally we hope calling interface request number is fixed,for example 3000 request through Web Interface effiency testing that we only can through [stop] button to end up with that so request number is very to controler in a fixed value Same use [locust] command to execute effiency testing through paramter setup execution of testing ```cmd= locust -f locustfile.py --host=http://192.168.127.134:8089 --no-web -c 10 -r 10 n 3000 ``` Start up parameter - --no-web :present no need to use Web Interface to execute testing - -c :setup vurtual user figure - -r :setup spawn virtual user figure per second - -n setup request figure ##### Multithreading and testing interface effiency Through Locust tool to execute effiency testing there is a problem vause use random to produce cellphone number to roll call ,although merely execute 3000 request but it not matched 3000 not roll call user completely to roll call.it will remain unroll call user also will ahve many repeat roll call failed user,but repeat roll call user request also will been process normally then the failed request is cannot been process normally by server Don't confuse the two If our requirement is calculate the timeout of roll call in 3000 guset then we can use python multithreading to implenment this requirement ```python= import requests import threading from time import time #define interface vase url base_url="http://192.168.127.134:8089" #roll call threading def sign_thread(start_user,end_user): for i in range(start_user,end_user): phone=1380010000+i datas={"eid","phone":phone} r=requests.post(base_url+"/api/user_sign",data=datas) result=r.json() try: assert result["message"]=="sign success" except AssertionError as e: print("phone:"+str(phone)+",user sign fail!") #Set User Group(5 threading) lists={1:601,602:1201,1201:1801,1801:2401,2401:3001} #Create threading list threads=[] #Create threading for start_user ,end_user in lists.items(): t=threading.Thread(target=sign_thread,args(start_user,end_user)) threads.append(t) if __name__ == "__main__": # start time start_time=time() #Open threading for i in range(len(lists)): threads[i].start() for i in range(len(lists)): threads[i].join() #End time end_time=time() print("Start time:"+str(start_time)) print("Emd time:"+str(end_time)) print("Run time:"+str(end_time-start_time)) ``` let 3000 figure user average divide into 5 groups and put then into dictionary,each group through threading class Thread call sign_thread() method produce a threading so 5 threadings can be comprehend [virtual user] calling interface threading in paralle start() use to open threading ,jon() use to safe threading --- # Django X Swagger ---- ### Install DRF swagger module [Reference article](https://zoejoyuliao.medium.com/%E7%94%A8-django-rest-framework-%E6%92%B0%E5%AF%AB-restful-api-%E4%B8%A6%E7%94%9F%E6%88%90-swagger-%E6%96%87%E6%AA%94-%E4%B8%8B-%E7%94%9F%E6%88%90-swagger-%E6%96%87%E6%AA%94-60c45e04afa8) this package can create swagger automatically in django rest framework ```cmd= pip install -U drf-yasg ``` ---- ### ADD drf-yasg in SETTINGS.py ```python= INSTALLED_APPS = [ 'drf_yasg', ] ``` ---- ### edit url.py and produce swagger url ```python= from rest_framework import permissions from drf_yasg.views import get_schema_view from drf_yasg import openapi schema_view = get_schema_view( openapi.Info( title="Snippets API", default_version='v1', description="Test description", terms_of_service="https://www.google.com/policies/terms/", contact=openapi.Contact(email="contact@snippets.local"), license=openapi.License(name="BSD License"), ), public=True, permission_classes=(permissions.AllowAny,), ) urlpatterns += [ url(r'^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'), url(r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'), url(r'^redoc/$', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'), ] ``` ----