Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support legacy WSGI write functions #2920

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

Cito
Copy link

@Cito Cito commented Nov 2, 2023

Summary

HTTPX already supports calling into WSGI apps.

However, this support does not include legacy (WSGI 1) apps and frameworks that use a write callable. This small PR adds support for these apps and frameworks, without changing the way how the WSGI support works otherwise. It also works as specified in the PEP when both write callable and iterable output are used.

Checklist

  • I understand that this PR may be closed in case there was no previous discussion.
    (see Add support for WSGI apps using write callables #2921)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
    (doesn't need documentation - just makes a special case work that didn't work before)

@tomchristie
Copy link
Member

However, this support does not include legacy (WSGI 1) apps and frameworks that use a write callable.

Interesting thanks ☺️

Could you show me a real-world scenario to demo this against?

@Cito
Copy link
Author

Cito commented Nov 3, 2023

Hi Tom. A real-world example would be Webware for Python which uses WSGI with write callables. You can create and run a demo with Webware for Python as follows:

# create and activate virtual env
python -m venv venv
source venv/bin/activate
# install web framework
pip install "Webware-for-Python[dev]>=3"
# create and run a demo app
webware make DemoApp
cd DemoApp
webware serve -b

In the web browser, you should see a simple demo application, served by a development web server.

We now want to fetch pages with httpx from this demo app using WSGI directly, i.e. without running the web server.

So you can stop the running app, but keep the virtualenv activated and install httpx with this patch in it. In the same directory DemoApp, create the following Python script:

### Create webware application

import webware
webware.addToSearchPath()
from Application import Application

settings = {'PrintConfigAtStartUp': False}
app = Application(".", settings=settings, development=True)

### Test run the application

import httpx

print("Home page fetched via WSGI:")
base_url = "http://localhost:8080/"
client= httpx.Client(app=app, base_url=base_url)
print(client.get(base_url).text)

If you run this script, you should now see in the output the HTML of the start page of the demo app. The point here is that this works without a web server, i.e. using the WSGI application in-process. Without the patch in this PR, this would not work.

In the PR, I have also created a test case with a demo app echo_body_with_write_callable that is using the write callable feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants