Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7857521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:04:08+00:00 2026-06-02T21:04:08+00:00

I am writing a web application with Pyramid and would like to restrict the

  • 0

I am writing a web application with Pyramid and would like to restrict the maximum length for POST requests, so that people can’t post huge amount of data and exhaust all the memory on the server. However I looked pretty much everywhere I could think of (Pyramid, WebOb, Paster) and couldn’t find any option to accomplish this. I’ve seen that Paster has limits for the number of HTTP headers, length each header, etc., but I didn’t see anything for the size of the request body.

The server will be accepting POST requests only for JSON-RPC, so I don’t need to allow huge request body sizes. Is there a way in the Pyramid stack of accomplishing this?

Just in case this is not obvious from the rest, a solution which has to accept and load the whole request body into memory before checking the length and returning a 4xx error code defeats the purpose of what’s I’m trying to do, and is not what I’m looking for.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T21:04:09+00:00Added an answer on June 2, 2026 at 9:04 pm

    You can do it in a variety of ways here’s a couple of examples. one using wsgi middleware based on webob(installed when you install pyramid among other things). and one that uses pyramids 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()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing web application that uses Spring MVC to bind Spring beans with REST-like
I'm writing a web application and need to initialize some parameters that I'm pulling
I'm writing a web application where users can submit pictures, videos, and descriptons of
I am writing a web application that allows a user to get an employee's
I'm currently writing a web application that have about 6-12 pages. On each one
I'm writing a web application that involves a continuous cycle of creating (and removing)
I am writing a web application that uses a JSP usebean tag in the
In my web application I would like to complately avoid html and use only
I'm writing a web application (in Python, not that it matters). One of the
I am writing a web application in PHP that will store large numbers of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.