bottle

Does bottle handle requests with no concurrency?

Deadly 提交于 2019-12-03 12:53:12
问题 At first, I think Bottle will handle requests concurrently, so I wrote test code bellow: import json from bottle import Bottle, run, request, response, get, post import time app = Bottle() NUMBERS = 0 @app.get("/test") def test(): id = request.query.get('id', 0) global NUMBERS n = NUMBERS time.sleep(0.2) n += 1 NUMBERS = n return id @app.get("/status") def status(): return json.dumps({"numbers": NUMBERS}) run(app, host='0.0.0.0', port=8000) Then I use jmeter to request /test url with 10

Bottle.py error routing

萝らか妹 提交于 2019-12-03 10:35:53
Bottle.py ships with an import to handle throwing HTTPErrors and route to a function. Firstly, the documentation claims I can (and so do several examples): from bottle import error @error(500) def custom500(error): return 'my custom message' however, when importing this statement error is unresolved but on running the application ignores this and just directs me to the generic error page. I found a way to get around this by: from bottle import Bottle main = Bottle() @Bottle.error(main, 500) def custom500(error): return 'my custom message' But this code prevents me from embedding my errors all

Convert mongodb return object to dictionary

爱⌒轻易说出口 提交于 2019-12-03 09:31:05
I'm using the bottle framework together with mongoengine. I have an orders model : class OrderDetail(Option): orderDetailsQty = FloatField() def to_dict(self): return mongo_to_dict_helper(self) class Order(Document): userName = StringField(required=True) orderDate = DateTimeField() orderStatus = ListField(EmbeddedDocumentField(Status)) orderDetails = ListField(EmbeddedDocumentField(OrderDetail)) orderComments = ListField(EmbeddedDocumentField(Comment)) isActive = BooleanField() def to_dict(self): orderObj = mongo_to_dict_helper(self) orderDetailList = [] for orderDetail in orderObj[

Error 2006: “MySQL server has gone away” using Python, Bottle Microframework and Apache

爷,独闯天下 提交于 2019-12-03 09:06:28
After accessing my web app using: - Python 2.7 - the Bottle micro framework v. 0.10.6 - Apache 2.2.22 - mod_wsgi - on Ubuntu Server 12.04 64bit; I'm receiving this error after several hours: OperationalError: (2006, 'MySQL server has gone away') I'm using MySQL - the native one included in Python. It usually happens when I don't access the server. I've tried closing all the connections, which I do, using this: cursor.close() db.close() where db is the standard MySQLdb.Connection() call. The my.cnf file looks something like this: key_buffer = 16M max_allowed_packet = 128M thread_stack = 192K

Decorators vs. classes in python web development

帅比萌擦擦* 提交于 2019-12-03 06:13:26
I've noticed three main ways Python web frameworks deal request handing: decorators, controller classes with methods for individual requests, and request classes with methods for GET/POST. I'm curious about the virtues of these three approaches. Are there major advantages or disadvantages to any of these approaches? To fix ideas, here are three examples. Bottle uses decorators: @route('/') def index(): return 'Hello World!' Pylons uses controller classes: class HelloController(BaseController): def index(self): return 'Hello World' Tornado uses request handler classes with methods for types:

Static files not loaded in a Bottle application when the trailing slash is omitted

拥有回忆 提交于 2019-12-03 04:02:04
I am serving a test file through apache using Bottle. Following are my apache config: WSGIDaemonProcess temp user=www-data group=www-data processes=1 threads=5 WSGIScriptAlias /temp /opt/gridops/usage/temp/adapter.wsgi <Directory /opt/gridops/usage/temp> WSGIProcessGroup temp WSGIApplicationGroup %{GLOBAL} Order deny,allow Allow from all </Directory> adapter.wsgi : import os,sys os.chdir(os.path.dirname(__file__)) sys.path = ['/opt/gridops/usage/temp'] + sys.path os.chdir(os.path.dirname(__file__)) sys.stdout = sys.stderr import bottle print "++"*10 import index # This loads your application

How to upload and save a file using bottle framework

不羁岁月 提交于 2019-12-03 03:51:34
问题 HTML: <form action="/upload" method="post" enctype="multipart/form-data"> Category: <input type="text" name="category" /> Select a file: <input type="file" name="upload" /> <input type="submit" value="Start upload" /> </form> View: @route('/upload', method='POST') def do_login(): category = request.forms.get('category') upload = request.files.get('upload') name, ext = os.path.splitext(upload.filename) if ext not in ('png','jpg','jpeg'): return 'File extension not allowed.' save_path = get

采用Tornado作为Web Server 运行第三方Web框架

混江龙づ霸主 提交于 2019-12-03 02:47:32
部门内部的所有Web应用,restful服务等都是采用Python开发的,主要用到了bottle框架和django框架。 这两个框架都是采用的自身的Web Server运行的,在高负载的情况下响应比较慢。改为Tornado作为Web Server来承载应用。 1. Bottle应用 from bottle import run import argparse if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("-H", "--host", help="Host to bind the API server on", default="localhost", action="store", required=False) parser.add_argument("-p", "--port", help="Port to bind the API server on", default=8090, action="store", required=False) args = parser.parse_args() run(server="tornado",host=args.host, port=args.port) 2. Django应用 #!/usr/bin/env

Bottle + WebSocket

有些话、适合烂在心里 提交于 2019-12-02 19:03:56
is it possible to host a normal Bottle application and a WebSocket one (example: https://github.com/defnull/bottle/blob/master/docs/async.rst ) in the same application (same port)? So that /ws will go to WebSocket handler and all other will be normally routed to other bottle handlers. It sure is. The server: #!/usr/bin/python import json from bottle import route, run, request, abort, Bottle ,static_file from pymongo import Connection from gevent import monkey; monkey.patch_all() from time import sleep app = Bottle() @app.route('/websocket') def handle_websocket(): wsock = request.environ.get(

How to upload and save a file using bottle framework

社会主义新天地 提交于 2019-12-02 17:38:27
HTML: <form action="/upload" method="post" enctype="multipart/form-data"> Category: <input type="text" name="category" /> Select a file: <input type="file" name="upload" /> <input type="submit" value="Start upload" /> </form> View: @route('/upload', method='POST') def do_login(): category = request.forms.get('category') upload = request.files.get('upload') name, ext = os.path.splitext(upload.filename) if ext not in ('png','jpg','jpeg'): return 'File extension not allowed.' save_path = get_save_path_for_category(category) upload.save(save_path) # appends upload.filename automatically return 'OK