Python搭建HTTP服务器:API接口(支持RESTful API)

API接口包含:

  • GET API
  • POST API
  • RESTful API
  • 文件上传API
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# coding=utf-8

import os
from flask import Flask, jsonify, request, redirect, url_for
from werkzeug import secure_filename


UPLOAD_FOLDER = 'D:/www/upload'
ALLOWED_EXTENSIONS = set(['txt', 'apk', 'zip', 'rar'])

app = Flask(__name__)

app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER


# Hello World
@app.route('/')
def hello_world():
return 'Hello World!'


# RESTful API
@app.route("/user/<int:id>")
def user(id):
return jsonify(id=id, username='Joe.Ye', head_url='http://www.androidios.cc/images/avatar.png')


# GET API
@app.route("/user/info")
def user_info():
id = request.args.get("id")
print request.headers
return jsonify(id=id, username='Joe.Ye', head_url='http://www.androidios.cc/images/avatar.png')


# POST API
@app.route("/login", methods=["POST"])
def login():
username = request.form.get("username");
password = request.form.get("password");

if username=='AppBlog.CN' and password=='123456':
return jsonify(code=1, message=u'登录成功')

return jsonify(code=0, message=u'用户名或者密码错误')


# POST API(JSON)
@app.route("/login/json", methods=["POST"])
def login_json():
json = request.get_json();
username = json.get("username");
password = json.get("password");

if username=='AppBlog.CN' and password=='123456':
return jsonify(code=1, message=u'登录成功')

return jsonify(code=0, message=u'用户名或者密码错误')


# POST API
@app.route("/user/new", methods=["POST"])
def user_create():
json = request.get_json()
print json

id = json.get("id")
username = json.get("username");
print id
print username

return jsonify(code=1, message=u"保存用户成功")


# POST API
@app.route("/user/edit", methods=["POST"])
def user_edit():
id = request.form.get("id")
username = request.form.get("username")

print request.headers

return jsonify(code=1, message=u"更新用户用户成功")


# 校验文件类型
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS


# 文件上传API,请查看文章底部附言
@app.route("/upload", methods=['GET', 'POST'])
def upload():
if request.method == 'POST':
username = request.form.get("username")
print 'username = ' + username
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
print 'file = ' + filename
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
#return redirect(url_for('upload'))
return jsonify(code=1, message=u'文件上传成功')
return """
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form action="" method="post" enctype="multipart/form-data">
<input type=file name=file>
<input type=submit value=Upload>
</form>
<p>%s</p>
""" % "<br>".join(os.listdir(app.config['UPLOAD_FOLDER'],))


if __name__ == '__main__':
app.run(host='0.0.0.0')

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :