web.py|web.py 真香

开始 【web.py|web.py 真香】如果你想为你的应用或网站搭建一个简易的后台,那么web.py是不错的选择。
life is short ,we need python!
URL POST GET code

# Helloword.py # coding:utf-8 # 导入web.py模块 import web# 定义URL结构对应的处理类 urls = ( '/hello', 'HelloHandler', '/word(.*)', 'WordHandler', ) # 创建一个基于我们刚提交的URL列表的application app = web.application(urls, globals())# 创建处理类 # HelloHandler 和WordHandler的区别就是hello中的ulr无通配符,word是有通配符 class HelloHandler: def GET(self): return "this is a Hello Return" class WordHandler: def GET(self, name): #通配符中匹配的文字,将作为name传入 return "this is a Work Return" + name# 启动上面我们写的应用 if __name__ == "__main__": app.run()

run
python Helloword.py 或者指定端口号 python Helloword.py 2333

test
在浏览器输入 http://0.0.0.0:8080/hello 或 http://0.0.0.0:8080/wordssss

CRUD增删改查 选用sqlite作为数据库,因为我只会这个orz
sqlite
sqlite3 User.db CREATE TABLE t_user(id BIGINT PRIMARY KEY NOT NULL, name TEXT NOT NULL);

code
#coding:utf-8 import web import jsonurls = ( '/user/(.*?)', 'UserHandler', )app = web.application(urls, globals()) db = web.database(dbn='sqlite',db='User.db')class UserHandler: def GET(self, name): inputFrom = web.input(); if name == "create": formName = inputFrom.get("name") userID = db.insert('t_user', name=formName) web.header('content-type', 'text/json') return json.dumps({'id': userID})elif name == "delete": formId = inputFrom.get("id") stWhere = "id=" + formId db.delete('t_user', where=stWhere) return "success"elif name == "update": formId = inputFrom.get("id") formName = inputFrom.get("name")stWhere = "id=" + formId db.update('t_user', where=stWhere, name=formName)return "update"elif name == "read":dbUsers = db.query('select * from t_user') jsonUsers = []for dbUser in dbUsers: jsonUser = {'id': dbUser.id, 'name': dbUser.name} jsonUsers.append(jsonUser) web.header('content-type', 'text/json') return json.dumps({'users': jsonUsers})if __name__ == "__main__": app.run()

test
http://0.0.0.0:8080/user/create?name="u1"http://0.0.0.0:8080/user/create?id=1&name="u11"http://0.0.0.0:8080/user/delete?id=1http://0.0.0.0:8080/user/read

文件上传 code
# UploadFile.py # coding:utf-8 import weburls = ( '/upload', 'UploadFileHandler', )app = web.application(urls, globals())class UploadFileHandler: def POST(self): input = web.input(uploadFile={}) # 保存文件 filename = input.uploadFile.filename fout = open(filename, 'wb') # w:打开一个文件只用于写入 wb:以二进制格式打开一个文件只用于写入,一般用于非文本文件如图片等 fout.write(input.uploadFile.file.read()) fout.close()# 处理其他参数 other = input.get("other") return "success"if __name__ == "__main__": app.run()

test
//okHttp android public void uplaodFile(final File file){ try { RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"),file); RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("uploadFile", file.getName(), fileBody) .addFormDataPart("other","dinosaur") .build(); Request request = new Request.Builder() .url(ipAction.getUrl() + "/dinosaur/upload") .post(requestBody) .build(); Call call = client.newCall(request); Response response = call.execute(); }catch (Exception e){ e.printStackTrace(); } }

    推荐阅读