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

Any examples on how to integrate with aiohttp client? #553

Closed
didip opened this issue Jun 16, 2019 · 2 comments
Closed

Any examples on how to integrate with aiohttp client? #553

didip opened this issue Jun 16, 2019 · 2 comments

Comments

@didip
Copy link
Contributor

didip commented Jun 16, 2019

I am trying to replace an old Tornado proxy server with combo of Starlette and aiohttp SessionClient.

However I keep getting raise RuntimeError('Timeout context manager should be used '

Most examples I see is to use the event loop directly to wait until completion, but I don't think I should do that from inside Starlette's http request handler.

@tomchristie
Copy link
Member

Early days, but you may want to take a look at https://github.com/encode/http3
You'll need to use the AsyncClient (It provides both sync and async interfaces)

We'll probably end up to moving to that as the test client once it's got support for plugging into WSGI and ASGI apps - encode/httpx#49

@chenglinning
Copy link

`import logging
import uvicorn
import httpx

from starlette.applications import Starlette
from starlette.responses import PlainTextResponse
from starlette.responses import JSONResponse

async def getbaidu():
client = httpx.AsyncClient(proxies=None, trust_env=False)
response = await client.get('https://www.baidu.com/')
print("OOOKK: {}".format(response))

app = Starlette()
@app.route("/", methods=["GET", "POST"])
async def homepage(request):
data = await request.json()
logging.info("method: {} url: {} clientip: {}".format(request.method, request.url, request.client.host))
logging.info("data type: {}".format(type(data)))
logging.info("request: {}".format(data))
await getbaidu()
return JSONResponse({'hello': 'world'})

if name == "main":
logging.info("Starting Starlette server!")
logging.info("Hello world")
uvicorn.run("myapp:app", host="0.0.0.0", port=8000, reload=True)`

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

No branches or pull requests

3 participants