可以当作一个 demo 来玩吧,在这里分享一下。里面涉及的内容包含了文件上传,cookie 设置和读取,重定向(redirect)。

from bottle import run, post, get, request, response, redirect
import os login_page='''
<b>Sorry! Authentication is needed!</b>
<form action='/login' enctype="multipart/form-data" method='post'>
<fieldset>
<legend>Input passwd for Admin:</legend>
<label>passwd:</label>
<input type="password" name="passwd"/>
<input type='submit' value='submit'>
</fieldset>
</form>
'''
upload_page='''
<!DOCTYPE html>
<html>
<head>
<title>
Firmware Update
</title>
</head>
<body>
<form action='/upload' enctype="multipart/form-data" method='post'>
<fieldset>
<legend>Firmware Update:</legend>
<label>Select the file:</label>
<input type="file" name="upload"/>
<input type='submit' value='submit'>
</fieldset>
</form>
</body>
</html>
''' @get('/')
def home():
usr = request.get_cookie("account")
if usr:
redirect('/upload')
else:
return login_page @post('/login')
def login():
passwd = request.POST['passwd'] if passwd == '':
response .set_cookie("account", 'admin')
redirect('/upload')
else:
return 'wrong! go back & retry!' @get('/upload')
def upload():
usr = request.get_cookie("account")
if usr:
return upload_page
else:
redirect('/') @get('/logout')
def logout():
response.delete_cookie("account")
redirect('/') @post('/upload')
def do_upload():
usr = request.get_cookie("account")
if usr:
upload = request.files.get('upload')
name, ext = os.path.splitext(upload.filename)
if ext not in ('.tgz','.gz'):
return 'You give a wrong file. Retry!' save_path = "/opt/myapp/update.tar.gz"
with open(save_path, 'w') as open_file:
open_file.write(upload.file.read()) return '''
<b>Done! Please re-power the machine to fill the update! </b>
<a href="/logout" title="logout">logout</a>
'''
else:
redirect('/') run(host='192.168.1.230', port=80)

用 bottle.py 写了个简单的升级包上传的更多相关文章

  1. 使用jsp/servlet简单实现文件上传与下载

    使用JSP/Servlet简单实现文件上传与下载    通过学习黑马jsp教学视频,我学会了使用jsp与servlet简单地实现web的文件的上传与下载,首先感谢黑马.好了,下面来简单了解如何通过使用 ...

  2. Django 实现简单的文件上传

    今天分享一下Django实现的简单的文件上传的小例子. 步骤 创建Django项目,创建Django应用 设计模型 处理urls.py 以及views.py 设计模板,设计表单 运行项目,查看数据库 ...

  3. UEditor之实现配置简单的图片上传示例

    UEditor之实现配置简单的图片上传示例 原创 2016年06月11日 18:27:31 开心一笑 下班后,阿华到楼下小超市买毛巾,刚买完出来,就遇到同一办公楼里另一家公司的阿菲,之前与她远远的有过 ...

  4. 服务器文档下载zip格式 SQL Server SQL分页查询 C#过滤html标签 EF 延时加载与死锁 在JS方法中返回多个值的三种方法(转载) IEnumerable,ICollection,IList接口问题 不吹不擂,你想要的Python面试都在这里了【315+道题】 基于mvc三层架构和ajax技术实现最简单的文件上传 事件管理

    服务器文档下载zip格式   刚好这次项目中遇到了这个东西,就来弄一下,挺简单的,但是前台调用的时候弄错了,浪费了大半天的时间,本人也是菜鸟一枚.开始吧.(MVC的) @using Rattan.Co ...

  5. Java实现一个简单的文件上传案例

    Java实现一个简单的文件上传案例 实现流程: 1.客户端从硬盘读取文件数据到程序中 2.客户端输出流,写出文件到服务端 3.服务端输出流,读取文件数据到服务端中 4.输出流,写出文件数据到服务器硬盘 ...

  6. 微信小程序简单封装图片上传组件

    微信小程序简单封装图片上传组件 希望自己 "day day up" -----小陶 我从哪里来 在写小程序的时候需要上传图片,个人觉得官方提供的 Uploader 组件不是太好用, ...

  7. 简单的GIT上传

    简单的GIT上传 上传项目时先新建一个 文件夹 mkdir test 然后在切换到test文件夹中然后把github 中的项目拷贝下来 git glone url 然后git init 查看文件 然后 ...

  8. 一、简单的图片上传并预览功能input[file]

    一.简单的图片上传并预览功能input[file] <!DOCTYPE html> <html lang="en"> <head> <me ...

  9. DVWA靶机--简单的文件上传漏洞

    简单的文件上传漏洞(靶机安全级别:low) 事先准备好一句话木马,密码为pass 上传一句话木马,显示上传路径(一般网站是不会显示路径的,这里靶机为了方便你测试漏洞,直接显示出了路径: ../../h ...

随机推荐

  1. HttpSession javax.servlet.http.HttpServletRequest.getSession(boolean arg0)理解

    request.getSession()和request.getSession(true)意思相同:获取session,如果session不存在,就新建一个 reqeust.getSession(fa ...

  2. apache路由端口配置

    <VirtualHost *:80> ServerName a.com ProxyPreserveHost On ProxyRequests On ProxyPass / http://1 ...

  3. Asp.Net Core获取请求信息/获取请求地址

     一.Asp.Net Core 2.0版本中目前HttpRequest是个抽象类 在控制器或视图上下文中获取到的 Request对象,是 DefaultHttpRequest的实例. 定义 如图 : ...

  4. SVG.Js事件示例,简单绑定拖动操作

    一.代码: var draw = SVG('container').size(300, 300); draw.style({ border: '1px solid red' }); var group ...

  5. 生成学习算法(Generative Learning algorithms)

    一.引言 前面我们谈论到的算法都是在给定\(x\)的情况下直接对\(p(y|x;\theta)\)进行建模.例如,逻辑回归利用\(h_\theta(x)=g(\theta^T x)\)对\(p(y|x ...

  6. [转]QT QDateTime类、QTimer类

    QDateTime类,头文件#include <QDateTime> 可以使用QDateTime类来获得系统时间.通过QDateTime::currentDateTime()来获取本地系统 ...

  7. Three.js中如何显示帧速【转】

    https://blog.csdn.net/hannahlwh1988/article/details/36876295 Step1:src中添加: <script src="js/s ...

  8. [leetcode]Next Permutation @ Python

    原题地址:https://oj.leetcode.com/problems/next-permutation/ 题意: Implement next permutation, which rearra ...

  9. 构建配置 ProGuard Shrink 混淆和压缩

    官方文档 压缩代码和资源 要尽可能减小 APK 文件,您应该启用压缩来移除 release build 中未使用的代码和资源.此页面介绍如何执行该操作,以及如何指定要在构建时保留或舍弃的代码和资源. ...

  10. Spring Boot 直接用jar运行项目

    概述 在Spring Boot 开篇-创建和运行一文中,介绍了如何创建一个Sprint Boot项目并且运行起来.但是运行的方式是在IDEA中直接Run起来的.还有另一中方式可以可以把Spring B ...