需要修改两个函数,第一个是init函数,修改如下:
async def init(loop, address, port): # <1>
app = web.Application(loop=loop) # <2>
app.router.add_route('GET', '/', home) # <3>
app = web.Application() # <2>
app.add_routes([web.get('/', home)])
handler = app.make_handler() # <4>
server = yield from loop.create_server(handler,
address, port) # <5>
runner = web.AppRunner(app)
await runner.setup()
server = await loop.create_server(runner.server,
address, port) # <5>
return server.sockets[0].getsockname() # <6>
不修改程序会报deprecation警报
第二个函数是home函数,修改如下:
def home(request): # <1>
query = request.GET.get('query', '').strip() # <2>
query = request.query.get('query', '').strip() # <2>
...
不修改程序会报如下错误:
Error handling request
Traceback (most recent call last):
...
File "D:\fluentPy\chapter17_18\http_charfinder.py", line 29, in home
query = request.GET.get('query', '').strip() # <2>
^^^^^^^^^^^
AttributeError: 'Request' object has no attribute 'GET'
因为request.GET已经被弃用了
如果程序运行正常,浏览器界面如下: