如何在Python Bottle REST 端点中将CORS功能应用于有限/特定的域名或IP?

2024-04-25 07:34:53 发布

您现在位置:Python中文网/ 问答频道 /正文

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. 无法将有限的IP/域添加到访问控制允许源站。你知道吗

需要帮忙吗?你知道吗


Tags: torunfromimportappbottlemakeserver