• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python的Bottle框架中返回静态文件和JSON对象的方法

Python的Bottle框架中返回静态文件和JSON对象的方法

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python bottle,美国que bottle京东,que bottle伸缩水壶,baby bottle奶瓶,swell bottle水杯等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

代码如下:

# -*- coding: utf-8 -*-
#!/usr/bin/python
# filename: todo.py
# codedtime: 2014-8-28 20:50:44

import sqlite3
import bottle
  
@bottle.route('/help3')
def help():
  return bottle.static_file('help.html', root='.') #静态文件

@bottle.route('/json:json#[0-9]+#')
def show_json(json):
  conn = sqlite3.connect('todo.db')
  c = conn.cursor()
  c.execute("SELECT task FROM todo WHERE id LIKE ?", (json))
  result = c.fetchall()
  c.close()

  if not result:
    return {'task':'This item number does not exist!'}
  else:
    return {'Task': result[0]} #返回Json对象

bottle.debug(True)
bottle.run(host='127.0.0.1', port=8080, reloader = True)

</div>

 第一个路由@bottle.route('/help3') 返回一个静态问,在浏览器中输入:http://127.0.0.1:8080/help3

结果如下:

2015430174409809.png (313×137)

其中的 root='.')或 root='./')表示在程序当前目录下,当然你也可以知道其他的路径如: root='/path/to/file'

第二个路由@bottle.route('/json:json#[0-9]+#')返回一个Json对象,在浏览器中输入:http://127.0.0.1:8080/json4

结果如下:

2015430174429421.png (376×141)

Web程序难免会遇到访问失败的错误,那么怎样去捕获这些错误,Bottle可以用路由机制来捕捉错误,如下捕获403、404:

@error(403)
def mistake403(code):
  return 'The parameter you passed has the wrong format!'

@error(404)
def mistake404(code):
  return 'Sorry, this page does not exist!'

</div>

其他错误处理如法泡制!

</div>

您可能想查找下面的文章:

  • 关于python的bottle框架跨域请求报错问题的处理方法
  • 使用Python的Bottle框架写一个简单的服务接口的示例
  • 使用Python的Bottle框架写一个简单的服务接口的示例
  • Python的Bottle框架中返回静态文件和JSON对象的方法
  • 简单的连接MySQL与Python的Bottle框架的方法
  • Python的Bottle框架中实现最基本的get和post的方法的教程
  • Python的Bottle框架中返回静态文件和JSON对象的方法
  • 简单的连接MySQL与Python的Bottle框架的方法
  • Python的Bottle框架中实现最基本的get和post的方法的教程
  • Python的Bottle框架中获取制定cookie的教程

相关文章

  • 以Python的Pyspider为例剖析搜索引擎的网络爬虫实现方法
  • python回溯法实现数组全排列输出实例分析
  • 在Python中利用Into包整洁地进行数据迁移的教程
  • 详解Django缓存处理中Vary头部的使用
  • Python def函数的定义、使用及参数传递实现代码
  • python中List的sort方法指南
  • Python使用cookielib模块操作cookie的实例教程
  • Python实现二分查找与bisect模块详解
  • Python使用百度API上传文件到百度网盘代码分享
  • 详解Django中的权限和组以及消息

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python条件变量之生产者与消费者操作实例分析
    • Python简单连接MongoDB数据库的方法
    • Python转码问题的解决方法
    • python3简单实现微信爬虫
    • 使用Python下的XSLT API进行web开发的简单教程
    • 探究Python中isalnum()方法的使用
    • python解决方案:WindowsError: [Error 2]
    • python去掉字符串中重复字符的方法
    • Python远程桌面协议RDPY安装使用介绍
    • Python程序设计入门(2)变量类型简介

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有