开发者

Dispatching URLs through Flask without a TCP port

开发者 https://www.devze.com 2023-04-13 08:59 出处:网络
I want to build a hybrid application (Web technologies embedded in a desktop app). I will start with a Web version and the embed it using WebKit, but I don\'t want the embedded version to service re

I want to build a hybrid application (Web technologies embedded in a desktop app).

I will start with a Web version and the embed it using WebKit, but I don't want the embedded version to service requests through a TCP port.

With WebKit (Qt,Gtk) I can 开发者_StackOverflow中文版intercept all URL requests and act on them.

What I'm missing is a way to invoke the Flask URL-to-callable dispatcher without going through TCP (or WSGI).

Any ideas better than analyzing the call stack with a debugger?


Simon Sapin answered on the (quite active) Flask mailing list:

Why not WSGI ?

You have to get a Python interpreter somewhere. Then you need to call your application somehow with data from WebKit like the URL being requested, and get the response. WSGI is just that: a calling convention for Python functions (or other callable objects.)

If WSGI is more complex than you’d like, you can use the test client:

  • http://flask.pocoo.org/docs/api/#flask.Flask.test_client
  • http://werkzeug.pocoo.org/docs/test/#werkzeug.test.Client
  • http://werkzeug.pocoo.org/docs/test/#werkzeug.test.EnvironBuilder

That’s how I do it in Frozen-Flask. It simulates HTTP requests to a Flask app at the WSGI level and write the responses to static files. The test client is just an easier way to make WSGI calls:

https://github.com/SimonSapin/Frozen-Flask/blob/master/flaskext/frozen/__init__.py#L228

WSGI really is Flask’s "entry point".

Other than that if you’re interested in Flask inner workings start looking from here:

https://github.com/mitsuhiko/flask/blob/master/flask/app.py#L1477

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号