Home > Enterprise >  Flask app: handle requests on trigger (no server, non blocking)
Flask app: handle requests on trigger (no server, non blocking)

Time:03-25

TLDR

consider a flask app:

# server.py
import flask

app = flask.Flask(__name__)
# routes, views, etc go here...
# ...

# run app
if __name__ == '__main__':
    app.run()

instead of running the server, I would like to trigger requests and get responses when I decide:

# server-trigger.py
from server import app

dummy_request = """GET /123 HTTP/1.1
Host: 127.0.0.1:8888
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="99", "Google Chrome";v="99"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36
Accept: text/html,application/xhtml xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: he-IL,he;q=0.9,en-US;q=0.8,en;q=0.7"""

res = app.handle_request(dummy_request) # handle request is not real, but needed.
# app.dispatch_request({})  # tried this, doesn't work.
# app.full_dispatch_request() # doesn't work either.

does it possible using flask?

Scenario details

not necessary to understand, but can help

I have already built and tested Flask app, currently served locally via app.run() option.

instead of running the flask dev server locally, I want another enterprise server to handle requests for my app. I'm trying to integrate with this server. this server allows custom handling requests on custom endpoints.

for example

enterprise-server.com/                   - server home endpoint
enterprise-server.com/custom-endpoint    - my custom endpoint on this server

I can run any python code that I would like on this endpoint, but I must process the request and return a valid response. means that this function cannot start a server, because it will block the thread and wait for requests, without returning any response.

the thing is, I already have a flask app that I want to leverage instead of rewriting my flask app.

so I thought, all the routes already defined in my app instance (which is an instance of Flask server), but I don't want to run the server and listen to requests, I just want to forward requests to the app instance when I decide to.

# flask-server.py
# instead of 
app.run()

# enterprise-server-custom-endpoint-handle.py
# run this instead:
def handle_custom_endpoint(req:string):
   res = app.handle_request(req)
   return res

of course that some adjustments will be made to the flask routes as it runs on other path.

CodePudding user response:

Edit: I've rewritten my answer because I hadn't considered the format of the request (raw).

I found two options. Option 1 entails parsing the http request (using http-request-translator) before passing it to the flask test client. Option 2 does the same thing in a much cleaner way making use of low-level Werkzeug modules. Unfortunately, Option 2 doesn't work because of unmaintained code. I've included it anyway, in case someone can maintain/fork the project werkzeug-raw.

app.py:

from flask import Flask

no_server_app = Flask(__name__)

@no_server_app.route('/custom-endpoint/<int:id>')
def test_endpoint(id: int):
    return {"input":id,"output":id 1}

if __name__ == '__main__':
    no_server_app.run()

endpoint_handler.py (option 1):

from flask.testing import FlaskClient
from hrt.interface import HttpRequestTranslator as hrt

from app import no_server_app

class RawClient(FlaskClient):
    def open(self,raw_req):
        req = hrt(request=raw_req)
        req = {'headers':req.headers, **req.details}

        # remove unnecessary items from req dict
        for k in ['Host','protocol','version','pre_scheme']:
            req.pop(k)

        return super(RawClient, self).open(**req)

no_server_app.test_client_class = RawClient
client = no_server_app.test_client()

if __name__ == '__main__':
    raw_req = 'GET /custom-endpoint/100 HTTP/1.1'
    response = client.open(raw_req)
    print(response.status_code, response.data)

endpoint_handler.py (option 2):

import werkzeug_raw
from flask.testing import FlaskClient

from app import no_server_app

client = no_server_app.test_client()

if __name__ == '__main__':
    raw_req = b'GET /custom-endpoint/100 HTTP/1.1'
    response = werkzeug_raw.open(client,raw_req)
    print(response.status_code, response.data)

CodePudding user response:

Parsing your raw request into objects as needed by Flask/werkzeug is actually not that hard, so you could do that with some own code:

def parse_raw_request(raw_request)
    lines = raw_request.splitlines()
    method, path, server_protocol = lines[0].split(" ")
    headers = dict(line.split(":", maxsplit=1) for line in lines[1:] if line)
    return method, path, server_protocol, headers

Maybe here and there some more to make it robust. And then creating an app.test_client and send a request from this data like @timothyh suggested is quite easy:

dummy_request = """GET / HTTP/1.1
Host: 127.0.0.1:8888
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="99", "Google Chrome";v="99"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36
Accept: text/html,application/xhtml xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: he-IL,he;q=0.9,en-US;q=0.8,en;q=0.7"""

method, path, server_protocol, headers = parse_raw_request(dummy_request)
client = app.test_client()
response = client.open(path=path, method=method, headers=headers)  # cant set server_protocol
print(response.data)
  • Related