Python web frame:Bottle

s

s

http://bottlepy.org/docs/dev/

http://tocode.sinaapp.com/18


安装

Install the latest stable release via PyPi (easy_install -U bottle)


设置

设置Aptana Studio 3,不然找不到代码提示
Window -> Preferences -> Pydev -> Interpreter - Python

D:\Python27\Lib\site-packages\bottle-0.11.6-py2.7.egg

然后重启Aptana


简单的示例

# coding=utf-8

from bottle import route, run, template, static_file, abort, error, redirect, post, get, request

@route('/')
@route('/index.html')
def index():
    return '<a href="/hello/world">Go to Hello World page</a>'

@route('/hello')
@route('/hello/:name')
def hello(name='World'):
    return template('<b>Hello {{name}}</b>!', name=name)

@route('/static/:filename')
def serve_static(filename):
    return static_file(filename, root='./static')

@route('/raise_error')
def raise_error():
    abort(404, "error...")
    
@error(404)
def error404(error):
    return '404 error !!!!!'

@route('/redirect')
def redirect_to_hello():
    redirect('/hello')

@route('/ajax')
def ajax_response():
    return {'dictionary': 'you will see ajax response right? Content-Type will be "application/json"'}

@get('/upload')
def upload_view():
    return """
        <form action="/upload" method="post" enctype="multipart/form-data">
          <input type="text" name="name" />
          <input type="file" name="data" />
          <input type="submit" name="submit" value="upload now" />
        </form>
        """    


@post('/upload')
def do_upload():
    name = request.forms.get('name')
    data = request.files.get('data')
    if name is not None and data is not None:
        filename = data.filename
        print filename
        
        fullPath = u'./upfiles/'+filename                #中文文件名显示正常
#        outfile = file('./upfiles/'+filename, 'wb')     #中文文件名会是乱码
        
        outfile = file(fullPath, 'wb')
        try:
            buf = data.file.read(data.bufsize)
            currentSize = len(buf)
            print currentSize#测试window上buffer大小为8192字节,即8k
            while buf:
                outfile.write(buf)
                buf = data.file.read(data.bufsize)
                currentSize += len(buf)
            outfile.close()
            return "Hello %s! You uploaded %s (%d bytes)." % (name, filename, currentSize)
        except Exception, e:
            print e.message
            return 'Failed in uploading %s !' % filename
        
    return "You missed a field."

@route('/tpl')
def tpl():
    return template('test')
    
run(host='localhost', port=8080)


教程

译言网:Bottle 教程

http://article.yeeyan.org/view/35282/123019



s


s




s


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值