python – 在金字塔上将POST请求限制为最大大小
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 在金字塔上将POST请求限制为最大大小,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3415字,纯文字阅读大概需要5分钟。
内容图文
![python – 在金字塔上将POST请求限制为最大大小](/upload/InfoBanner/zyjiaocheng/784/4633a5cfc336428ea45477269c9abd9e.jpg)
我正在使用Pyramid编写Web应用程序,并希望限制POST请求的最大长度,以便人们无法发布大量数据并耗尽服务器上的所有内存.然而,我几乎到处都能看到(金字塔,WebOb,Paster)并且无法找到完成此任务的任何选项.我已经看到Paster对HTTP标头的数量,每个标头的长度等有限制,但我没有看到请求体的大小.
服务器将仅接受JSON-RPC的POST请求,因此我不需要允许大量的请求体大小.金字塔堆栈中有没有一种方法可以实现这一目标?
为了防止其他问题不明显,在检查长度并返回4xx错误代码之前必须接受并将整个请求主体加载到内存中的解决方案违背了我正在尝试做的目的,而不是我正在寻找什么.
解决方法:
你可以通过各种方式来实现这里的几个例子.一个使用基于webob的wsgi中间件(当你在其他东西中安装金字塔时安装).和一个使用金字塔event mechanism
"""
restricting execution based on request body size
"""
from pyramid.config import Configurator
from pyramid.view import view_config
from pyramid.events import NewRequest, subscriber
from webob import Response, Request
from webob.exc import HTTPBadRequest
import unittest
def restrict_body_middleware(app, max_size=0):
"""
this is straight wsgi middleware and in this case only depends on
webob. this can be used with any wsgi compliant web
framework(which is pretty much all of them)
"""
def m(environ, start_response):
r = Request(environ)
if r.content_length <= max_size:
return r.get_response(app)(environ, start_response)
else:
err_body = """
request content_length(%s) exceeds
the configured maximum content_length allowed(%s)
""" % (r.content_length, max_size)
res = HTTPBadRequest(err_body)
return res(environ, start_response)
return m
def new_request_restrict(event):
"""
pyramid event handler called whenever there is a new request
recieved
http://docs.pylonsproject.org/projects/pyramid/en/1.2-branch/narr/events.html
"""
request = event.request
if request.content_length >= 0:
raise HTTPBadRequest("too big")
@view_config()
def index(request):
return Response("HI THERE")
def make_application():
"""
make appplication with one view
"""
config = Configurator()
config.scan()
return config.make_wsgi_app()
def make_application_with_event():
"""
make application with one view and one event subsriber subscribed
to NewRequest
"""
config = Configurator()
config.add_subscriber(new_request_restrict, NewRequest)
return config.make_wsgi_app()
def make_application_with_middleware():
"""
make application with one view wrapped in wsgi middleware
"""
return restrict_body_middleware(make_application())
class TestWSGIApplication(unittest.TestCase):
def testNoRestriction(self):
app = make_application()
request = Request.blank("/", body="i am a request with a body")
self.assert_(request.content_length > 0, "content_length should be > 0")
response = request.get_response(app)
self.assert_(response.status_int == 200, "expected status code 200 got %s" % response.status_int)
def testRestrictedByMiddleware(self):
app = make_application_with_middleware()
request = Request.blank("/", body="i am a request with a body")
self.assert_(request.content_length > 0, "content_length should be > 0")
response = request.get_response(app)
self.assert_(response.status_int == 400, "expected status code 400 got %s" % response.status_int)
def testRestrictedByEvent(self):
app = make_application_with_event()
request = Request.blank("/", body="i am a request with a body")
self.assert_(request.content_length > 0, "content_length should be > 0")
response = request.get_response(app)
self.assert_(response.status_int == 400, "expected status code 400 got %s" % response.status_int)
if __name__ == "__main__":
unittest.main()
内容总结
以上是互联网集市为您收集整理的python – 在金字塔上将POST请求限制为最大大小全部内容,希望文章能够帮你解决python – 在金字塔上将POST请求限制为最大大小所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。