bottlepy - 用python bottle搭建的http服务post请求405怎么解决啊?
PHPz
PHPz 2017-04-18 10:15:09
[Python讨论组]

网上有方案说改核心代码

class HTTPResponse(Response, BottleException):
    def __init__(self, body='', status=None, headers=None, **more_headers):
        super(HTTPResponse, self).__init__(body, status, headers, **more_headers)
 
    def apply(self, response):
        response._status_code = self._status_code
        response._status_line = self._status_line
        if self._headers:
            if response._headers:
                response._headers.update(self._headers)
            else:
                response._headers = self._headers
 
        response._cookies = self._cookies
        response.body = self.body

或者

#!/usr/bin/python  
# -*- conding:utf-8 -*-  
  
from bottle import *  
  
#decorator  
def allow_cross_domain(fn):  
    def _enable_cors(*args, **kwargs):  
        #set cross headers  
        response.headers['Access-Control-Allow-Origin'] = '*'  
        response.headers['Access-Control-Allow-Methods'] = 'GET,POST,PUT,OPTIONS'  
        allow_headers = 'Referer, Accept, Origin, User-Agent'  
        response.headers['Access-Control-Allow-Headers'] = allow_headers       
        if bottle.request.method != 'OPTIONS':  
            # actual request; reply with the actual response  
            return fn(*args, **kwargs)      
    return _enable_cors  
     
 
@route('/helloworld/:yourwords', methods=['GET', 'POST'])  
@allow_cross_domain                                              #在此处加上定义的函数  
def hello(yourwords):  
    return 'hello world. ' + yourwords  
  
run(host='0.0.0.0', port=8080)  

我发现都不行,get请求可以的,post请求就405 Method Not Allowed

我的代码

from bottle import route, run, request, response

@route('/color/a')
def colora():
    print(request.forms.get('words'))
    print('xxxx')
    return '{"name":"test"}'

run(host='0.0.0.0', port=50001, debug=True)
PHPz
PHPz

学习是最好的投资!

全部回复(1)
巴扎黑

from bottle import get, post, request # or route
@post ('/login') # or @route('/login', method='POST')

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号