如何将cors功能应用于python瓶子休息端点中的有限/特定域或IP?

时间:2017-11-07 06:57:46

标签: python cors response bottle

from bottle import Bottle, response, request
import bottle
from bottle.ext import beaker
from bottle import redirect, request, response, route, run, static_file, error
from bottle import Bottle, get, ServerAdapter
from bottle.ext import beaker

#app = Bottle()
app = beaker.middleware.SessionMiddleware(bottle.app())

def enable_cors(fn):
  def _enable_cors(*args, **kwargs):
    # set CORS headers
    response.headers['Access-Control-Allow-Origin'] = '100.200.100.500,100.300.100.400' # not working
    response.headers[
        'Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS'
    response.headers[
        'Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type,' \
                                          ' X-Requested-With, X-CSRF-Token'
    response.add_header('Server', ' ')  # not able to make changes
    response.set_header('Server', ' ')  # not able to make changes

    if request.method != 'OPTIONS':
        # actual request; reply with the actual response
        return fn(*args, **kwargs)
return _enable_cors

@route('/about', method=['OPTIONS', 'GET', 'POST'], strict_slash=True)
@enable_cors
def about():
   response.headers['content_type'] = 'text/ttt' # able to make changes
   response.headers['Server'] = ' ' # not able to make changes
   response.server = ' '
   print "request :: ",dict(request.headers)
   print response.iter_headers()
   #return HTTPResponse(content_type = 'text/plain', body="Tell me about yourself.")
   return "Tell me about yourself."

if __name__ == '__main__':
    #bottle.run(host='localhost', port=5010, debug=True, server='paste')
    #app.run(host='localhost', port=5010, debug=True, server='paste')
    run(app=app, host='100.200.100.500', port=8080, debug=True, server='paste')

问题::

  1. 无法更改响应标头参数“Server”=“”。尝试将服务器作为粘贴运行时,会出现此问题。
  2. 无法将有限的Ips /域添加到Access-Control-Allow-Origin。
  3. 有什么帮助吗?

0 个答案:

没有答案