python 框架
支持异步的 python web 框架
轻量级 flask 框架
import base64
import random
import io
import time
from flask import Flask, request
from flask_restful import reqparse, abort, Api, Resource
import json
app = Flask(__name__)
api = Api(app)
users = {
"magigo":["123456"]
}
def gen_token(uid):
token = base64.b64encode(bytes(''.join([str(uid), str(random.random()), str(time.time()+7200)]), encoding='utf-8'))
users[uid].append(token)
return token
def verify_token(token):
_token = base64.b64decode(token)
print(str(_token, encoding='utf-8'))
if not users.get(str(_token, encoding='utf-8').split(':')[0])[-1] == token:
return -1
if float(_token.split(':')[-1]) >= time.time():
return 1
else:
return 0
@app.route('/login', methods=['POST', 'GET'])
def login():
auth = request.headers['Authorization'].split(' ')[-1]
print('Authorization:\n',auth)
uid, pw = str(base64.b64decode(auth), encoding='utf-8').split(':')
print(uid)
if users.get(uid)[0] == pw:
return gen_token(uid)
else:
return 'error'
@app.route('/test', methods=['POST', 'GET'])
def test():
token = request.args.get('token')
if verify_token(token) == 1:
return 'data'
else:
return 'invalid state!'
@app.route('/nlp/result/<queryString>', methods=['POST','GET'])
def get_nlp_result(queryString):
print(queryString)
result = dict(a='a',b='b')
return json.dumps(result,ensure_ascii=False,indent=4)
parser = reqparse.RequestParser()
parser.add_argument('queryString',type=str)
class NLP_Result(Resource):
def post(self):
args = parser.parse_args()
print(args)
return dict(a='a', b='b')
def get(self):
self.post()
api.add_resource(NLP_Result,'/nlp/queryResult')
if __name__ == '__main__':
app.run(debug=True,host='192.168.5.3')
import requests
def query_nlp_result(queryString):
response = requests.get('http://192.168.5.3:5000/nlp/result/{}'.format(queryString))
print(response.content.decode())
def login_req():
response = requests.get('http://192.168.5.3:5000/login', auth=('magigo','123456'))
print(response.text)
def verify_test():
token = 'bWFnaWdvMC41OTAyODkzODc4MDg4MTA4MTU2NjMyNjg0OC4zODU3NDI3'
response = requests.get('http://192.168.5.3:5000/test',params={'token':token})
print(response.text)
if __name__ == '__main__':
# query_nlp_result('how are you!')
verify_test()
login_req()
# 今日踩坑, flask 为何 端口占用不提示呀呀呀呀呀!!! damn!!!
netstat -ano|findstr "5000"
taskkill /pid 5000
使用 pipenv 进行包管理
常用参数及命令一览
1 pipenv --where 列出本地工程路径
2 pipenv --venv 列出虚拟环境路径
3 pipenv --py 列出虚拟环境的Python可执行文件
4 pipenv install 安装包(创建虚拟环境)
5 pipenv install moduel --dev 在开发环境安装包
6 pipenv graph 查看包依赖
7 pipenv lock 生成lockfile
8 pipenv install --dev 安装所有开发环境包
9 pipenv uninstall --all 卸载所有包
10 pipenv --rm 删除虚拟环境
1 . cd yourproj
Pipfile
####################
[[source]]
url = "https://mirrors.aliyun.com/pypi/simple"
verify_ssl = true
name = "aliyun"
[dev-packages]
[packages]
cymysql = "*"
Flask-Cors = "*"
Flask-HTTPAuth = "*"
Flask-SQLAlchemy = "*"
Flask-WTF = "*"
requests = "*"
[requires]
python_version = "3.6"
#################
[[source]]
url = "https://pypi.douban/simple"
verify_ssl = true
name = "douban"
[dev-packages]
[packages]
requests = {version="*", index="douban"}
maya = {version="*", index="aliyun"}
records = "*"
pipenv install
# 安装完成之后呢执行 pipenv graph 查看安装结果
pipenv graph
pipenv --venv # 查看 虚拟环境名称,在 pycharm 里要使用到
成熟企业级 Django 框架
如果有来生,一个人去远行,看不同的风景,感受生命的活力。。。