0

I want to not answer a request handled by Flask. I don't want to return any error code, data, or an answer at all.

What I am trying to accomplish by doing this is that there is an endpoint takes sensor data and do not return any information. The clients POST the data to this endpoint, but they do not wait for an answer and shutdown (I have no control over the clients.) So I'm seeing the following error: "[Errno 10053] An established connection was aborted by the software in your host machine". So I asked myself, why do I even respond to these requests.

Bora
  • 1,402
  • 16
  • 19
  • to clarify, when a user comes to a page say(/index) you want nothing to happen(blank screen), or am I totally off base here? – WildCard May 24 '16 at 17:51
  • 2
    You might have to do this at the webserver level. If the request makes it as far as your application, you have to do _something_. – John Gordon May 24 '16 at 17:53
  • You could make a response function that does a very long `sleep()`, and nothing else... – John Gordon May 24 '16 at 18:13
  • So when a sensor POSTs its data, do you want to accept the data and process it? If so, then you're _not ignoring_ the request. – John Gordon May 24 '16 at 18:44
  • 2
    @Bora Just ignore or filter out that error. Creating a `sleep` endpoint for this is madness. – Two-Bit Alchemist May 24 '16 at 18:44
  • @JohnGordon I am asking how to not respond an incoming request. I can take the information however I want, that's easy. Being able to ignore a request was going to solve all of my problems. – Bora May 24 '16 at 19:57
  • @Two-BitAlchemist Ignoring or filtering out that error is also a solution. But I think that would be responding a request that I don't have to, although I know it will fail and create an error log. That seems like an extra work, and a workaround to the problem. Not a solution. Not answering the requests would be a better solution. But it seems that's not possible with Flask. – Bora May 24 '16 at 20:04

1 Answers1

4

I can think of two reasons to do something like this:

  • You have a "friend" that you want to prevent from accessing your site, or
  • You have the misguided notion that this will help prevent (D)DoS attacks.

When you say "ignore a request totally" you kind of actually can't do that, generally speaking. Unless you know the IP address that the traffic is coming from, and then you can instruct your OS, Network card, router, switch, load balancer, maybe even ISP to filter out the traffic coming from that IP.

Otherwise, you're kind of out of luck because of how the Internet works.

HTTP works over TCP*. Specifically the client process looks something like this:

  • Translate DNS (e.g. google.com) to IP address (e.g. 216.58.218.174)
  • open up a TCP connection to 216.58.218.174:80 (using google for the example)
  • send the HTTP header over to Google:

    GET / HTTP/1.1

  • read the response

Once that TCP/IP connection has been created to your server, at the very least you're going to have to terminate the connection.

There's really no good way to do this from within Python itself, and certainly not within Flask.

As you've updated your answer, it turns out you really don't have to change anything, Flask is already handling the error behind the scenes. It may be routing the message to a specific logger that you might be able to handle if you really don't want to see the messages, but it's not really important.

The only thing you may want to do, if your return processing is expensive (like tying up the database with a several second long query) is look into streaming your response instead, which will fail much more cheaply.

*Mostly. Sure you can do it over UDP, but you probably aren't

Community
  • 1
  • 1
Wayne Werner
  • 49,299
  • 29
  • 200
  • 290