from aiohttp import web
import aiohttp
from settings import config
import asyncio
import psycopg2 as p
import json
import aiopg
import aiohttp
import asyncio
async def fetch(client):
async with client.get('https://jsonplaceholder.typicode.com/todos/1') as resp:
assert resp.status == 200
return await resp.json()
async def index():
async with aiohttp.ClientSession() as client:
html = await fetch(client)
return web.Response(html)
loop = asyncio.get_event_loop()
loop.run_until_complete(index())
this is my views.py
from aiohttp import web
from routes import setup_routes
from settings import config
app = web.Application()
setup_routes(app)
web.run_app(app,port=9090)
main.py
from views import index
def setup_routes(app):
app.router.add_get('/', index)
and here is my routes.py
but when ever i tried to fire the url of localhost:9090 i just get an internal server 500 error saying
TypeError: index() takes 0 positional arguments but 1 was given
but t i can print the json in the terminal but couldnt fire the same as web response in the browser i dont know what is wrong in this case
Your index
coroutine is a handler, so it must accept a single positional argument, which will receive a Request
instance. For example:
async def index(request):
async with aiohttp.ClientSession() as client:
html = await fetch(client)
return web.Response(html)
The loop.run_until_complete(index())
at the top-level of views.py
is unnecessary and won't work once index()
is defined correctly.