The easiest way to use cors on bottle
Project description
The easiest way to implement cors on your bottle py web application
Installing the plugin
pip install bottle-cors-plugin
after this on your bottle app you need to import cors_plugin and install for example.
# -*- coding: utf-8 -*-
from bottle import app, response, route, run
from bottle_cors_plugin import cors_plugin
@route('/', method='GET')
def landing():
response.content_type = 'application/json'
return {'status': 'Works'}
#Confugure the server
app = app()
app.install(cors_plugin('*'))
if name == "__main__":
run(host='localhost', port=7000)
On the cors_plugin function you can send a simple string or array of origins this variable will set globaly on the plugin so you just set-it one time to add * origins just don’t put anything on the function
cors_plugin()
This will return the * origins
cors_plugin('https://google.com')
with just google.com as and origin or
cors_plugin(['https://google.com', 'http://google.com'])
for multiple origins
Aborts
for normal abort errors you need to import the abort of the cors_plugin like this
from bottle_cors_plugin import abort
@route('/', method='GET')
def landing():
response.content_type = 'application/json'
abort(500, 'Hola')
return {'status': 'Works'}
It works with all errors, and for custom error handler just import the cors_headers to apply on the function example like this
# -*- coding: utf-8 -*-
from import_env import os
from bottle import error, response
from bottle_cors_plugin import cors_headers
error_log = error
for status_code in range(200, 600):
@error(status_code)
def errorCustom(error_log):
cors_headers()
error_log.content_type = 'application/json'
return error_log.body
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for bottle_cors_plugin-0.1.8-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 06c1e0949e6023e2743813c8fd494d95bbca0ec47a8765ca32159b1534d168dd |
|
MD5 | c227c3de3f51c6ed8e3e5f825b26109a |
|
BLAKE2b-256 | 4f25fda90fcdb18d27b6b3d27cbae595da28779e418b2d08ea4474df7b047e9b |