2
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

john
  • 539
  • 2
  • 9
  • 24

2 Answers2

4

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.

user4815162342
  • 141,790
  • 18
  • 296
  • 355
  • but whenever i tried to run the server i cant stop the server and cant acess the page its saying me that the page cannot be loaded ,can i know why and how can we fix the issue?? – john Dec 14 '18 at 15:17
1

Your index() async function should accept request argument to be a web-handler compatible.

Andrew Svetlov
  • 16,730
  • 8
  • 66
  • 69