今天用python寫了一個接口,其中有一個可以用curl執行,鼓搗了一下curl命令,鼓搗一下午~
1.下載curl
下載地址:https://curl.haxx.se/download.html

下載后,直接解壓縮,解壓后打開自己的解壓目錄,復制上面的路徑,如

設置環境變量:選中我的電腦--> 右擊-->屬性--> 高級系統設置

新建系統環境變量
變量名:CURL_HOME,變量值:剛才復制的路徑,點擊確定
然后選擇 Path -- > 編輯
選擇新建
1.首先添加: %CURL_HOME%\I386
2.然后再新建添加: E:\mysoftware\curl-7.68.0-win64-mingw\bin, 這個路徑是你解壓curl的地址打開后找到里面的bin目錄的地址
配置完成后,確定退出。
打開cmd,輸入curl --help

打開idea,啟動項目,打開你的terminal然后輸入請求地址回車即可。
1.get請求
curl http://127.0.0。1:5001/index
2.post請求
curl -d "date=2018-12-28&course_id=123456&chapter_id=99999" http://127.0.0.1:5001/reg
其他電腦輸入
curl -d "date=2018-12-28&course_id=123456&chapter_id=99999" http://192.168.5.175:5001/reg


使用python代碼:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import subprocess 4 import flask,json 5 6 ''' 7 CREATE TABLE IF NOT EXISTS `my_user`( 8 `auto_id` INT UNSIGNED AUTO_INCREMENT, 9 `username` VARCHAR(50) NOT NULL, 10 `passwd` VARCHAR(40) NOT NULL, 11 `is_admin` INT, 12 PRIMARY KEY ( `auto_id` ) 13 )ENGINE=InnoDB DEFAULT CHARSET=utf8; 14 ''' 15 #__name__代表當前的python文件,把當前這個python文件,當成一個服務 16 server=flask.Flask(__name__) 17 def excute_command(username,pwd): 18 if username=='yyz': 19 return "excute_command username=yyz pwd=%s" % pwd 20 else: 21 return "excute_command username=%s pwd=%s" % (username,pwd) 22 23 def my_db(sql): 24 import pymysql 25 coon = pymysql.connect(host='127.0.0.1',user='root',passwd='y44444444',port=3306,db='test_db',charset='utf8') 26 cur = coon.cursor() 27 cur.execute(sql) 28 if sql.strip()[:6].upper() == 'SELECT': #判斷sql語句是否select開頭 29 res = cur.fetchall() #獲取到sql語句所有的返回結果 30 else: 31 coon.commit() 32 res = 'ok' 33 cur.close() 34 coon.close() 35 return res 36 37 @server.route('/index',methods=['get']) #裝飾器 ip:5000/index?xxx 38 def index(): 39 res={'msg':'這個是我開發的第一個接口','msg_code':0} #這個是字典,接口返回的是json,所以需要引入json,並且返回進行json.dumps 40 return json.dumps(res,ensure_ascii=False) #返回結果是unicode,需要增加ensure_ascii=False 41 42 #帶參數post方法,請求時地址后加上?date=2018-12-28&course_id=123456&chapter_id=99999 43 @server.route('/reg',methods=['post']) 44 def reg(): 45 try: 46 #傳入參數 47 date = flask.request.values.get('date') 48 print("date-date",date) 49 course_id = int(flask.request.values.get('course_id')) 50 chapter_id = int(flask.request.values.get('chapter_id')) 51 #subprocess.Popen("bash ./main.sh '" + str(date) + "'", shell=True) 52 res = {'msg': str(date) + '執行shell成功!', 'msg_code': 0} 53 except: 54 res = {'msg':'參數非法,請檢查接口文檔!','msg_code':1001} 55 56 return json.dumps(res,ensure_ascii=False) 57 58 if __name__ == '__main__': 59 server.run(port=5001,debug=True, host='0.0.0.0') 60 #端口號要是不指定,默認為5000.debug=True,改了代碼之后不用重啟,會自動重啟一次。后面增加host='0.0.0.0',別人可以訪問
