{"id":334586,"date":"2022-06-16T15:00:48","date_gmt":"2022-06-16T15:00:48","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=334586"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=334586","title":{"rendered":"<span>\u0410\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 python \u0431\u0435\u0437 \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0431\u043e\u043b\u0438 (\u0447\u0430\u0441\u0442\u044c 2)<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><strong><em>\u042d\u0442\u043e \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0446\u0438\u043a\u043b\u0430 \u0441\u0442\u0430\u0442\u0435\u0439 \u043f\u0440\u043e <\/em><\/strong><code>asyncio<\/code><strong><em>. \u041d\u0430\u0447\u0430\u043b\u043e <\/em><\/strong><a href=\"https:\/\/habr.com\/ru\/post\/667630\/\" rel=\"noopener noreferrer nofollow\"><strong><em>\u0437\u0434\u0435\u0441\u044c<\/em><\/strong><\/a><strong><em>.<\/em><\/strong><\/p>\n<h2>6. Aiohttp \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0436\u0438\u0442\u0435\u043b\u0438 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u043c\u0438\u0440\u0430<\/h2>\n<p>\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u0433\u043e\u0442\u043e\u0432\u0438\u0442\u044c asyncio. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0443\u0436\u0435 \u0437\u043d\u0430\u0435\u043c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043c\u043d\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043c\u043e\u0434\u043d\u044b\u0439 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441. \u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0439 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043d\u044b\u0439 \u043f\u0430\u0442\u0442\u0435\u0440\u043d &#171;API-\u0448\u043b\u044e\u0437&#187;. \u042d\u0442\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u0448\u0442\u0443\u043a\u0430. \u041f\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0443 \u043d\u0430 \u0441\u0432\u043e\u0439 API-\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0434\u0440\u0443\u0433\u0438\u0445 API, \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0437\u043d\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u0443 \u0442\u043e\u0447\u043a\u0443 \u0432\u0445\u043e\u0434\u0430, \u0430 \u0432\u0441\u0435 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043e\u0442 \u043d\u0435\u0433\u043e \u0441\u043a\u0440\u044b\u0442\u044b.<\/p>\n<p>\u0412 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0433\u043b\u0430\u0432\u0435 \u043c\u044b \u043d\u0430\u0443\u0447\u0438\u043b\u0438\u0441\u044c \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0442\u044c \u043f\u043e\u0433\u043e\u0434\u0443 \u0443 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 <code>api.openweathermap.org<\/code>. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0435\u0433\u043e \u0441\u043b\u0435\u0433\u043a\u0430 \u0438\u043c\u043f\u043e\u0440\u0442\u043e\u0437\u0430\u043c\u0435\u0441\u0442\u0438\u043c. \u0412\u0435\u0440\u043d\u0435\u0435 \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0440\u0443\u0441\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u043c. \u041f\u0443\u0441\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u044b\u043b\u0430\u0435\u0442 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0433\u043e\u0440\u043e\u0434\u0430 \u043d\u0430 \u0440\u0443\u0441\u0441\u043a\u043e\u043c \u044f\u0437\u044b\u043a\u0435 \u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u0445 GET-\u0437\u0430\u043f\u0440\u043e\u0441\u0430 (\u0435\u0441\u043b\u0438 \u0432\u044b \u043d\u0435 \u0437\u043d\u0430\u0435\u0442\u0435 \u0447\u0442\u043e \u044d\u0442\u043e \u0442\u0430\u043a\u043e\u0435, \u0431\u0435\u0433\u043e\u043c \u0447\u0438\u0442\u0430\u0442\u044c \u043f\u0440\u043e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b HTTP) \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043e\u0442\u0432\u0435\u0442 \u0432 \u0432\u0438\u0434\u0435 json \u043e\u043f\u044f\u0442\u044c-\u0442\u0430\u043a\u0438 \u043d\u0430 \u0432\u0435\u043b\u0438\u043a\u043e\u043c \u0438 \u043c\u043e\u0433\u0443\u0447\u0435\u043c.<\/p>\n<p>\u041c\u044b \u0443\u0436\u0435 \u043e\u0441\u0432\u043e\u0438\u043b\u0438 http-\u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <code>aiohttp<\/code>, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u0432\u043d\u0435\u0448\u043d\u0438\u043c API. \u041e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u0432 \u044d\u0442\u043e\u0439 \u0436\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 \u0435\u0441\u0442\u044c \u0438 \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0435 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u043e\u0433\u043e http-\u0441\u0435\u0440\u0432\u0435\u0440\u0430. \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0435\u0440\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u0440\u043e\u043a\u0441\u0438:<\/p>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440 6.1<\/h3>\n<pre><code class=\"python\">import json from aiohttp import ClientSession, web   async def get_weather(city):     async with ClientSession() as session:         url = f'http:\/\/api.openweathermap.org\/data\/2.5\/weather' \\               f'?q={city}&amp;APPID=2a4ff86f9aaa70041ec8e82db64abf56'          async with session.get(url) as response:             weather_json = await response.json()             try:                 return weather_json[\"weather\"][0][\"main\"]             except KeyError:                 return '\u041d\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0445'   async def handle(request):     city = request.rel_url.query['city']     weather = await get_weather(city)     result = {'city': city, 'weather': weather}      return web.Response(text=json.dumps(result, ensure_ascii=False))   if __name__ == '__main__':     app = web.Application()     app.add_routes([web.get('\/weather', handle)])     web.run_app(app) <\/code><\/pre>\n<p>\u0412 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>get_weather<\/code> \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u043e\u0432\u043e\u0433\u043e \u043d\u0435\u0442, \u043c\u044b \u0435\u0435 \u043b\u0438\u0448\u044c \u0441\u043b\u0435\u0433\u043a\u0430 &#171;\u043e\u0431\u043b\u0430\u0433\u043e\u0440\u043e\u0434\u0438\u043b\u0438&#187;, \u0447\u0442\u043e\u0431\u044b \u0437\u0430\u043f\u0440\u043e\u0441 \u043f\u043e\u0433\u043e\u0434\u044b \u0434\u043b\u044f \u043d\u0435\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e \u0433\u043e\u0440\u043e\u0434\u0430 \u043d\u0435 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u043b \u043a \u0442\u0440\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u044f\u043c \u0434\u043b\u044f \u0432\u0441\u0435\u0433\u043e \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0417\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>handle<\/code> (&#171;\u0440\u0443\u0447\u043a\u0430&#187; \u043d\u0430 \u0441\u043b\u0435\u043d\u0433\u0435 \u0431\u044d\u043a\u0435\u043d\u0434\u0435\u0440\u043e\u0432). \u0418\u0437 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u0442\u0441\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 city \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0432 <code>get_weather<\/code>. \u0414\u0430\u043b\u0435\u0435 \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043e\u0442\u0432\u0435\u0442 \u0432 \u0432\u0438\u0434\u0435 json. \u0410\u0434\u0440\u0435\u0441 \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0438 \u0442\u0438\u043f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0437\u0430\u0434\u0430\u0435\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c: <code>app.add_routes([web.get('\/weather', handle)])<\/code>.<\/p>\n<p>\u0421\u0442\u043e\u043f! \u0410 \u0433\u0434\u0435 \u0436\u0435 \u043d\u0430\u0448 \u0441\u0442\u0430\u0440\u044b\u0439 \u0434\u0440\u0443\u0433 <code>asyncio.run<\/code>? \u041d\u0435 \u043f\u0435\u0440\u0435\u0436\u0438\u0432\u0430\u0439\u0442\u0435. \u041a\u043e\u0433\u0434\u0430 \u043c\u044b \u0438\u043c\u0435\u0435\u043c \u0434\u0435\u043b\u043e \u0441 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c\u0438 \u0432\u0435\u0431-\u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430\u043c\u0438 (\u0430 <code>aiohttp<\/code> \u2014 \u044d\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a, \u0445\u043e\u0442\u044c \u0438 \u0441\u0443\u043f\u0435\u0440-\u043c\u0438\u043d\u0438\u043c\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439), \u0432\u0441\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u043f\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044e \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0443 \u0437\u0430\u0434\u0430\u0447 <code>asyncio<\/code> \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0443 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430 &#171;\u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c&#187;. \u0413\u0434\u0435-\u0442\u043e \u0433\u043b\u0443\u0431\u043e\u043a\u043e \u0432 \u043d\u0435\u0434\u0440\u0430\u0445 &#8216;web.run_app&#8217; \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u0437\u0430\u0434\u0430\u0447\u0438, \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0446\u0438\u043a\u043b \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0432 <code>asyncio.run<\/code>. \u041d\u0430\u043c, \u043a\u0430\u043a \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u043c, \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0435\u0442 \u043d\u0443\u0436\u0434\u044b \u0431\u0435\u0441\u043f\u043e\u043a\u043e\u0438\u0442\u0441\u044f \u043e\u0431 \u044d\u0442\u0438\u0445 \u043d\u0438\u0437\u043c\u0435\u043d\u043d\u044b\u0445 \u0434\u0435\u0442\u0430\u043b\u044f\u0445. \u041f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043c\u0438\u0440\u043d\u043e \u0441\u043f\u0438\u0442 \u0432 \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e\u043c \u0446\u0438\u043a\u043b\u0435, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043f\u0440\u0438\u0434\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 GET \u043d\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 URL. \u041a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u0442\u043e \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0435\u0442, \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043b\u043e\u0433\u0438\u043a\u0430 \u0432 \u0440\u0443\u0447\u043a\u0435. \u0418 \u0441\u043d\u043e\u0432\u0430 \u0431\u0430\u044e-\u0431\u0430\u0439 \u0434\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u041d\u043e \u0435\u0441\u043b\u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0435\u0449\u0435 \u043d\u0435 \u0443\u0441\u043f\u0435\u043b \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c\u0441\u044f, \u043a\u0430\u043a \u043f\u043e\u0441\u0442\u0443\u043f\u0438\u043b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439, \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0435\u0433\u043e \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435, \u043d\u0435 \u0434\u043e\u0436\u0438\u0434\u0430\u044f\u0441\u044c (\u043f\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438) \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043f\u0435\u0440\u0432\u043e\u0433\u043e. \u0412 \u044d\u0442\u043e\u043c \u0441\u0430\u043c\u0430 \u0441\u0443\u0442\u044c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>\u0417\u0430\u0445\u043e\u0434\u0438\u043c \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u043e\u043c \u043d\u0430 \u0430\u0434\u0440\u0435\u0441: <code>localhost:8080\/weather?city=Sochi<\/code> \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u0438\u043c\u043f\u0430\u0442\u0438\u0447\u043d\u044b\u0439 json:<\/p>\n<pre><code>{\"city\": \"Sochi\", \"weather\": \"Clouds\"} <\/code><\/pre>\n<p>\u041a\u0441\u0442\u0430\u0442\u0438, \u0435\u0441\u043b\u0438 \u0432\u044b \u0432\u0441\u0435\u0440\u044c\u0435\u0437 \u0440\u0435\u0448\u0438\u043b\u0438 \u0437\u0430\u043d\u044f\u0442\u044c\u0441\u044f \u0431\u044d\u043a\u0435\u043d\u0434-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u043e\u0439, \u043e\u0434\u043d\u0438\u043c \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u043e\u043c \u0432\u0430\u043c \u043d\u0438\u043a\u0430\u043a \u043d\u0435 \u043e\u0431\u043e\u0439\u0442\u0438\u0441\u044c. \u041f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0439 \u0437\u0430\u043b\u0435\u0437\u0430\u0442\u044c \u0432\u0433\u043b\u0443\u0431\u044c HTTP. \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442 \u0434\u0435-\u0444\u0430\u043a\u0442\u043e \u0437\u0434\u0435\u0441\u044c <a href=\"https:\/\/www.postman.com\" rel=\"noopener noreferrer nofollow\">Postman<\/a>, \u043d\u043e \u0432 \u043f\u0440\u0438\u0440\u043e\u0434\u0435 \u0435\u0441\u0442\u044c \u0438 \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f. <\/p>\n<p>\u0421\u043a\u0435\u043b\u0435\u0442 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0433\u043e\u0442\u043e\u0432, \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u043d\u0430\u0440\u0430\u0449\u0438\u0432\u0430\u0442\u044c \u043d\u0430 \u043d\u0435\u0433\u043e \u043c\u044b\u0448\u0446\u044b. \u0412\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u044b\u043c API-\u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u043e\u043c <code>libretranslate.de<\/code>:<\/p>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440 6.2<\/h3>\n<pre><code class=\"python\">import json from aiohttp import ClientSession, web   async def get_weather(city):     async with ClientSession() as session:         url = f'http:\/\/api.openweathermap.org\/data\/2.5\/weather' \\               f'?q={city}&amp;APPID=2a4ff86f9aaa70041ec8e82db64abf56'          async with session.get(url) as response:             weather_json = await response.json()             try:                 return weather_json[\"weather\"][0][\"main\"]             except KeyError:                 return '\u041d\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0445'   async def get_translation(text, source, target):     async with ClientSession() as session:         url = 'https:\/\/libretranslate.de\/translate'          data = {'q': text, 'source': source, 'target': target, 'format': 'text'}          async with session.post(url, json=data) as response:             translate_json = await response.json()              try:                 return translate_json['translatedText']             except KeyError:                 return text   async def handle(request):     city_ru = request.rel_url.query['city']     city_en = await get_translation(city_ru, 'ru', 'en')      weather_en = await get_weather(city_en)     weather_ru = await get_translation(weather_en, 'en', 'ru')      result = {'city': city_ru, 'weather': weather_ru}      return web.Response(text=json.dumps(result, ensure_ascii=False))   if __name__ == '__main__':     app = web.Application()     app.add_routes([web.get('\/weather', handle)])     web.run_app(app) <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0432 \u0440\u0443\u0447\u043a\u0435 \u0434\u0432\u0430\u0436\u0434\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>get_translation<\/code> \u0438, \u0432\u0443\u0430\u043b\u044f:<\/p>\n<p><code>localhost:8080\/weather?city=\u0421\u043e\u0447\u0438<\/code><\/p>\n<pre><code>{\"city\": \"\u0421\u043e\u0447\u0438\", \"weather\": \"\u041e\u0431\u043b\u0430\u043a\u0430\"} <\/code><\/pre>\n<p>\u0427\u0442\u043e \u044d\u0442\u043e \u0437\u0430 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441 \u0431\u0435\u0437 \u043b\u043e\u0433\u0433\u0435\u0440\u0430? \u041e\u0434\u043d\u0430\u043a\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0430\u0441\u043a\u0432\u043e\u0437\u044c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u0443\u044e \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0443\u044e (\u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0443\u044e) \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 <code>logging<\/code> \u2014 \u044d\u0442\u043e \u043d\u0435\u043f\u0440\u043e\u0444\u0435\u0441\u0441\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e. \u0412\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439 \u043b\u043e\u0433\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f <code>aiologger<\/code>:<\/p>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440 6.3<\/h3>\n<pre><code class=\"python\">import json from aiohttp import ClientSession, web from aiologger.loggers.json import JsonLogger   logger = JsonLogger.with_default_handlers(             level='DEBUG',             serializer_kwargs={'ensure_ascii': False},         )   async def get_weather(city):     async with ClientSession() as session:         url = f'http:\/\/api.openweathermap.org\/data\/2.5\/weather' \\               f'?q={city}&amp;APPID=2a4ff86f9aaa70041ec8e82db64abf56'          async with session.get(url) as response:             weather_json = await response.json()             try:                 return weather_json[\"weather\"][0][\"main\"]             except KeyError:                 logger.error(f'\u041d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u043e\u0433\u043e\u0434\u0443 \u0434\u043b\u044f \u0433\u043e\u0440\u043e\u0434\u0430: {city}')                 return '\u041d\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0445'   async def get_translation(text, source, target):     await logger.info(f'\u041f\u043e\u0441\u0442\u0443\u043f\u0438\u043b \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u043d\u0430 \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u043b\u043e\u0432\u0430: {text}')      async with ClientSession() as session:         url = 'https:\/\/libretranslate.de\/translate'          data = {'q': text, 'source': source, 'target': target, 'format': 'text'}          async with session.post(url, json=data) as response:             translate_json = await response.json()              try:                 return translate_json['translatedText']             except KeyError:                 logger.error(f'\u041d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0434\u043b\u044f \u0441\u043b\u043e\u0432\u0430: {text}')                 return text   async def handle(request):     city_ru = request.rel_url.query['city']      await logger.info(f'\u041f\u043e\u0441\u0442\u0443\u043f\u0438\u043b \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u0433\u043e\u0440\u043e\u0434: {city_ru}')      city_en = await get_translation(city_ru, 'ru', 'en')     weather_en = await get_weather(city_en)     weather_ru = await get_translation(weather_en, 'en', 'ru')      result = {'city': city_ru, 'weather': weather_ru}      return web.Response(text=json.dumps(result, ensure_ascii=False))   if __name__ == '__main__':     app = web.Application()     app.add_routes([web.get('\/weather', handle)])     web.run_app(app) <\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043b\u043e\u0433\u0433\u0435\u0440 \u043d\u0438 \u043d\u0430 \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434\u0443 \u043d\u0435 \u0437\u0430\u0434\u0435\u0440\u0436\u0438\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0432 \u0446\u0435\u043b\u043e\u043c. \u041e\u043d \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u0443\u0437\u044b, \u043f\u043e\u043a\u0430 \u043c\u044b \u043e\u0436\u0438\u0434\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u043b\u0438 \u043e\u0442\u0432\u0435\u0442\u0430 \u043e\u0442 \u0432\u043d\u0435\u0448\u043d\u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430.<\/p>\n<p>\u041d\u0443 \u0430 \u0442\u0435\u043f\u0435\u0440\u044c \u0432\u0438\u0448\u0435\u043d\u043a\u0430 \u043d\u0430 \u0442\u043e\u0440\u0442\u0435 \u2014 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u0447\u0442\u043e-\u0442\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u0432 \u0411\u0414, \u043d\u0443, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u043f\u043e\u0441\u0442\u0443\u043f\u0438\u0432\u0448\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b (\u043d\u0438\u0447\u0435\u0433\u043e \u0431\u043e\u043b\u0435\u0435 \u0443\u043c\u043d\u043e\u0433\u043e \u043c\u043d\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043d\u0435 \u043f\u0440\u0438\u0448\u043b\u043e). \u0421\u0430\u043c\u0430\u044f \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u0411\u0414 \u0432 \u043c\u0438\u0440\u0435, \u043a\u0430\u043a \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u2014 \u044d\u0442\u043e SQLite. \u0418 \u0434\u043b\u044f \u043d\u0435\u0435, \u043a \u0441\u0447\u0430\u0441\u0442\u044c\u044e, \u0435\u0441\u0442\u044c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u0434\u0440\u0430\u0439\u0432\u0435\u0440 <code>aiosqlite<\/code>. \u041f\u0440\u043e\u0431\u0443\u0435\u043c:<\/p>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440 6.4<\/h3>\n<pre><code class=\"python\">import json import aiosqlite import asyncio from aiohttp import ClientSession, web from aiologger.loggers.json import JsonLogger from datetime import datetime   logger = JsonLogger.with_default_handlers(             level='DEBUG',             serializer_kwargs={'ensure_ascii': False},         )   async def create_table():     async with aiosqlite.connect('weather.db') as db:         await db.execute('CREATE TABLE IF NOT EXISTS requests '                          '(date text, city text, weather text)')         await db.commit()   async def save_to_db(city, weather):     async with aiosqlite.connect('weather.db') as db:         sql = f'INSERT INTO requests VALUES (\"{datetime.now()}\", \"{city}\", \"{weather}\")'          await db.execute(sql)         await db.commit()   async def get_weather(city):     async with ClientSession() as session:         url = f'http:\/\/api.openweathermap.org\/data\/2.5\/weather' \\               f'?q={city}&amp;APPID=2a4ff86f9aaa70041ec8e82db64abf56'          async with session.get(url) as response:             weather_json = await response.json()             try:                 return weather_json[\"weather\"][0][\"main\"]             except KeyError:                 logger.error(f'\u041d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u043e\u0433\u043e\u0434\u0443 \u0434\u043b\u044f \u0433\u043e\u0440\u043e\u0434\u0430: {city}')                 return '\u041d\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0445'   async def get_translation(text, source, target):     await logger.info(f'\u041f\u043e\u0441\u0442\u0443\u043f\u0438\u043b \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u043d\u0430 \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u043b\u043e\u0432\u0430: {text}')      async with ClientSession() as session:         url = 'https:\/\/libretranslate.de\/translate'          data = {'q': text, 'source': source, 'target': target, 'format': 'text'}          async with session.post(url, json=data) as response:             translate_json = await response.json()              try:                 return translate_json['translatedText']             except KeyError:                 logger.error(f'\u041d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0434\u043b\u044f \u0441\u043b\u043e\u0432\u0430: {text}')                 return text   async def handle(request):     city_ru = request.rel_url.query['city']      await logger.info(f'\u041f\u043e\u0441\u0442\u0443\u043f\u0438\u043b \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u0433\u043e\u0440\u043e\u0434: {city_ru}')      city_en = await get_translation(city_ru, 'ru', 'en')     weather_en = await get_weather(city_en)     weather_ru = await get_translation(weather_en, 'en', 'ru')      result = {'city': city_ru, 'weather': weather_ru}      await save_to_db(city_ru, weather_ru)      return web.Response(text=json.dumps(result, ensure_ascii=False))   if __name__ == '__main__':     asyncio.run(create_table())      app = web.Application()     app.add_routes([web.get('\/weather', handle)])     web.run_app(app) <\/code><\/pre>\n<p>\u041c\u043e\u0436\u0435\u0442\u0435 \u0437\u0430\u0433\u043b\u044f\u043d\u0443\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043d\u0430 \u043b\u0435\u0442\u0443 \u0444\u0430\u0439\u043b\u0430 <code>weather.db<\/code> (\u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u043d\u0435 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0449\u0438\u043a, \u0430 \u043a\u0430\u043a\u0443\u044e-\u043d\u0438\u0431\u0443\u0434\u044c \u043f\u0440\u0438\u0431\u043b\u0443\u0434\u0443 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0411\u0414, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <a href=\"https:\/\/dbeaver.io\/\" rel=\"noopener noreferrer nofollow\">DBeaver<\/a>). \u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 <code>requests<\/code>. \u0418 \u0441\u043d\u043e\u0432\u0430 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a, \u043c\u044b \u0432\u0435\u0434\u044c \u0436\u0438\u0432\u0435\u043c \u0432 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u043c \u043c\u0438\u0440\u0435.<\/p>\n<p>\u0421\u043c\u043e\u0442\u0440\u0438\u0442\u0435-\u043a\u0430! \u041d\u0435\u043e\u0436\u0438\u0434\u0430\u043d\u043d\u043e \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f \u043d\u0430\u0448 \u0437\u0430\u0431\u044b\u0442\u044b\u0439 \u0434\u0440\u0443\u0433 <code>asyncio.run<\/code>, \u043a \u0447\u0435\u043c\u0443 \u0431\u044b \u044d\u0442\u043e? \u0410 \u044d\u0442\u043e \u043a \u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b <code>requests<\/code> (\u0435\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0432\u043e\u0439 \u0435\u0449\u0435 \u043d\u0435\u0442) \u0434\u043e\u043b\u0436\u043d\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u043b\u0438\u0448\u044c \u043e\u0434\u043d\u0430\u0436\u0434\u044b \u043f\u0440\u0438 \u0441\u0442\u0430\u0440\u0442\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0435\u0449\u0435 \u0434\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430 \u0432\u043d\u0443\u0442\u0440\u0438 <code>web.run_app<\/code>. \u041d\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f-\u0442\u043e <code>create_table<\/code> \u0443 \u043d\u0430\u0441 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0430\u044f, \u0442\u0430\u043a \u043f\u0440\u043e\u0441\u0442\u043e \u0435\u0435 \u043d\u0435 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0448\u044c (\u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0439\u0442\u0435 \u0441\u0430\u043c\u0438, \u0435\u0441\u043b\u0438 \u043d\u0435 \u0432\u0435\u0440\u0438\u0442\u0435). \u0412\u043e\u0442 \u0438 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0435\u0434\u0438\u043d\u043e\u0436\u0434\u044b \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0435\u0435 &#171;\u0432\u0440\u0443\u0447\u043d\u0443\u044e&#187; \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 <code>asyncio.run<\/code> \u0438 \u043b\u0438\u0448\u044c \u0437\u0430\u0442\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0431\u0440\u0430\u0437\u0434\u044b \u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f <code>web.run_app<\/code>.<\/p>\n<p>\u0412 \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u044d\u0442\u043e\u0433\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0430 \u0445\u043e\u0447\u0443 \u0432\u0430\u0441 \u043f\u043e\u0437\u0434\u0440\u0430\u0432\u0438\u0442\u044c. \u0422\u0435\u043f\u0435\u0440\u044c \u0432\u044b \u0438\u043c\u0435\u0435\u0442\u0435 \u0432 \u0440\u0443\u043a\u0430\u0445 \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0435 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0445 \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439. \u041d\u0435\u0432\u0430\u0436\u043d\u043e \u043a\u0430\u043a\u043e\u0439 \u0444\u0440\u0435\u0439\u043c\u0444\u043e\u0440\u043a \u0432\u044b \u0431\u0443\u0434\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c: <code>FastAPI<\/code>, <code>Tornado<\/code>, <code>Falcon<\/code> \u0438\u043b\u0438 \u043a\u0430\u043a\u043e\u0439-\u043d\u0438\u0431\u0443\u0434\u044c \u0435\u0449\u0435. \u041f\u0440\u0438\u043d\u0446\u0438\u043f \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0442\u043e\u0442 \u0436\u0435 \u0441\u0430\u043c\u044b\u0439 \u043a\u0430\u043a \u0432 \u0441\u0442\u0430\u0440\u043e\u043c \u0434\u043e\u0431\u0440\u043e\u043c <code>aiohttp<\/code>: \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0440\u0443\u0447\u043a\u0443 \u0438 \u0432 \u043d\u0435\u0439 \u043d\u0430\u043d\u0438\u0437\u044b\u0432\u0430\u0435\u043c &#171;\u0448\u0430\u0448\u043b\u044b\u043a&#187; \u0438\u0437 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439. \u0413\u043b\u0430\u0432\u043d\u043e\u0435 \u0437\u0430 \u0447\u0435\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u2014 \u044d\u0442\u043e \u0447\u0442\u043e\u0431\u044b \u0432 \u044d\u0442\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0435 \u0437\u0430\u0442\u0435\u0441\u0430\u043b\u043e\u0441\u044c \u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0435\u0435 \u0438\u0437 \u0441\u043a\u0443\u0447\u043d\u043e\u0433\u043e \u043f\u044b\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u043c\u0438\u0440\u0430.<\/p>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u043c\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043f\u0440\u043e\u0449\u0430\u0435\u043c\u0441\u044f.<\/p>\n<p><strong><em>\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0441\u043b\u0435\u0434\u0443\u0435\u0442&#8230;<\/em><\/strong><\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <\/p>\n<div class=\"tm-article-poll\">\n<div class=\"tm-notice tm-article-poll__notice tm-notice_positive\"><!----> <\/p>\n<div class=\"tm-notice__inner\"><!----> <\/p>\n<div class=\"tm-notice__content\"><span>\u0422\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u043c\u043e\u0433\u0443\u0442 \u0443\u0447\u0430\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0432 \u043e\u043f\u0440\u043e\u0441\u0435. <a rel=\"nofollow\" href=\"\/kek\/v1\/auth\/habrahabr\/?back=\/ru\/post\/671798\/&#038;hl=ru\">\u0412\u043e\u0439\u0434\u0438\u0442\u0435<\/a>, \u043f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430.<\/span><\/div>\n<\/div>\n<\/div>\n<div class=\"tm-article-poll__header\">\u041e \u0447\u0435\u043c \u0431\u044b \u0432\u044b \u0445\u043e\u0442\u0435\u043b\u0438 \u0443\u0437\u043d\u0430\u0442\u044c \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0446\u0438\u043a\u043b\u0430?<\/div>\n<div class=\"tm-article-poll__answers\">\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent\">             0%           <\/span> <span class=\"tm-article-poll__answer-label\">\u041e \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u043c\u0438 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c\u0438 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430\u043c\u0438 \u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430\u043c\u0438<\/span> <span class=\"tm-article-poll__answer-votes\">             0           <\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress\" style=\"width:0%;\"><\/div>\n<\/div>\n<\/div>\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent tm-article-poll__answer-percent_winning\">             0%           <\/span> <span class=\"tm-article-poll__answer-label\">\u041b\u0443\u0447\u0448\u0435 \u0431\u0443\u0434\u0435\u043c \u0443\u0433\u043b\u0443\u0431\u043b\u044f\u0442\u044c\u0441\u044f \u0432 \u043d\u0438\u0437\u043a\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u044b\u0435 \u0434\u0435\u0442\u0430\u043b\u0438 asyncio \u0438 \u043d\u0430\u0443\u0447\u0438\u043c\u0441\u044f \u0441\u0430\u043c\u0438 \u043f\u0438\u0441\u0430\u0442\u044c \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0438<\/span> <span class=\"tm-article-poll__answer-votes\">             0           <\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress tm-article-poll__answer-progress_winning\" style=\"width:0%;\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"tm-article-poll__stats\">        \u041d\u0438\u043a\u0442\u043e \u0435\u0449\u0435 \u043d\u0435 \u0433\u043e\u043b\u043e\u0441\u043e\u0432\u0430\u043b.          \u0412\u043e\u0437\u0434\u0435\u0440\u0436\u0430\u043b\u0441\u044f 1 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c.      <\/div>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/671798\/\"> https:\/\/habr.com\/ru\/post\/671798\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><strong><em>\u042d\u0442\u043e \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0446\u0438\u043a\u043b\u0430 \u0441\u0442\u0430\u0442\u0435\u0439 \u043f\u0440\u043e <\/em><\/strong><code>asyncio<\/code><strong><em>. \u041d\u0430\u0447\u0430\u043b\u043e <\/em><\/strong><a href=\"https:\/\/habr.com\/ru\/post\/667630\/\" rel=\"noopener noreferrer nofollow\"><strong><em>\u0437\u0434\u0435\u0441\u044c<\/em><\/strong><\/a><strong><em>.<\/em><\/strong><\/p>\n<h2>6. Aiohttp \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0436\u0438\u0442\u0435\u043b\u0438 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u043c\u0438\u0440\u0430<\/h2>\n<p>\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u0433\u043e\u0442\u043e\u0432\u0438\u0442\u044c asyncio. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0443\u0436\u0435 \u0437\u043d\u0430\u0435\u043c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043c\u043d\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043c\u043e\u0434\u043d\u044b\u0439 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441. \u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0439 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043d\u044b\u0439 \u043f\u0430\u0442\u0442\u0435\u0440\u043d &#171;API-\u0448\u043b\u044e\u0437&#187;. \u042d\u0442\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u0448\u0442\u0443\u043a\u0430. \u041f\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0443 \u043d\u0430 \u0441\u0432\u043e\u0439 API-\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0434\u0440\u0443\u0433\u0438\u0445 API, \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0437\u043d\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u0443 \u0442\u043e\u0447\u043a\u0443 \u0432\u0445\u043e\u0434\u0430, \u0430 \u0432\u0441\u0435 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043e\u0442 \u043d\u0435\u0433\u043e \u0441\u043a\u0440\u044b\u0442\u044b.<\/p>\n<p>\u0412 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0433\u043b\u0430\u0432\u0435 \u043c\u044b \u043d\u0430\u0443\u0447\u0438\u043b\u0438\u0441\u044c \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0442\u044c \u043f\u043e\u0433\u043e\u0434\u0443 \u0443 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 <code>api.openweathermap.org<\/code>. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0435\u0433\u043e \u0441\u043b\u0435\u0433\u043a\u0430 \u0438\u043c\u043f\u043e\u0440\u0442\u043e\u0437\u0430\u043c\u0435\u0441\u0442\u0438\u043c. \u0412\u0435\u0440\u043d\u0435\u0435 \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0440\u0443\u0441\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u043c. \u041f\u0443\u0441\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u044b\u043b\u0430\u0435\u0442 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0433\u043e\u0440\u043e\u0434\u0430 \u043d\u0430 \u0440\u0443\u0441\u0441\u043a\u043e\u043c \u044f\u0437\u044b\u043a\u0435 \u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u0445 GET-\u0437\u0430\u043f\u0440\u043e\u0441\u0430 (\u0435\u0441\u043b\u0438 \u0432\u044b \u043d\u0435 \u0437\u043d\u0430\u0435\u0442\u0435 \u0447\u0442\u043e \u044d\u0442\u043e \u0442\u0430\u043a\u043e\u0435, \u0431\u0435\u0433\u043e\u043c \u0447\u0438\u0442\u0430\u0442\u044c \u043f\u0440\u043e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b HTTP) \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043e\u0442\u0432\u0435\u0442 \u0432 \u0432\u0438\u0434\u0435 json \u043e\u043f\u044f\u0442\u044c-\u0442\u0430\u043a\u0438 \u043d\u0430 \u0432\u0435\u043b\u0438\u043a\u043e\u043c \u0438 \u043c\u043e\u0433\u0443\u0447\u0435\u043c.<\/p>\n<p>\u041c\u044b \u0443\u0436\u0435 \u043e\u0441\u0432\u043e\u0438\u043b\u0438 http-\u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <code>aiohttp<\/code>, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u0432\u043d\u0435\u0448\u043d\u0438\u043c API. \u041e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u0432 \u044d\u0442\u043e\u0439 \u0436\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 \u0435\u0441\u0442\u044c \u0438 \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0435 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u043e\u0433\u043e http-\u0441\u0435\u0440\u0432\u0435\u0440\u0430. \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0435\u0440\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u0440\u043e\u043a\u0441\u0438:<\/p>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440 6.1<\/h3>\n<pre><code class=\"python\">import json from aiohttp import ClientSession, web   async def get_weather(city):     async with ClientSession() as session:         url = f'http:\/\/api.openweathermap.org\/data\/2.5\/weather' \\               f'?q={city}&amp;APPID=2a4ff86f9aaa70041ec8e82db64abf56'          async with session.get(url) as response:             weather_json = await response.json()             try:                 return weather_json[\"weather\"][0][\"main\"]             except KeyError:                 return '\u041d\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0445'   async def handle(request):     city = request.rel_url.query['city']     weather = await get_weather(city)     result = {'city': city, 'weather': weather}      return web.Response(text=json.dumps(result, ensure_ascii=False))   if __name__ == '__main__':     app = web.Application()     app.add_routes([web.get('\/weather', handle)])     web.run_app(app) <\/code><\/pre>\n<p>\u0412 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>get_weather<\/code> \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u043e\u0432\u043e\u0433\u043e \u043d\u0435\u0442, \u043c\u044b \u0435\u0435 \u043b\u0438\u0448\u044c \u0441\u043b\u0435\u0433\u043a\u0430 &#171;\u043e\u0431\u043b\u0430\u0433\u043e\u0440\u043e\u0434\u0438\u043b\u0438&#187;, \u0447\u0442\u043e\u0431\u044b \u0437\u0430\u043f\u0440\u043e\u0441 \u043f\u043e\u0433\u043e\u0434\u044b \u0434\u043b\u044f \u043d\u0435\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e \u0433\u043e\u0440\u043e\u0434\u0430 \u043d\u0435 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u043b \u043a \u0442\u0440\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u044f\u043c \u0434\u043b\u044f \u0432\u0441\u0435\u0433\u043e \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0417\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>handle<\/code> (&#171;\u0440\u0443\u0447\u043a\u0430&#187; \u043d\u0430 \u0441\u043b\u0435\u043d\u0433\u0435 \u0431\u044d\u043a\u0435\u043d\u0434\u0435\u0440\u043e\u0432). \u0418\u0437 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u0442\u0441\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 city \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0432 <code>get_weather<\/code>. \u0414\u0430\u043b\u0435\u0435 \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043e\u0442\u0432\u0435\u0442 \u0432 \u0432\u0438\u0434\u0435 json. \u0410\u0434\u0440\u0435\u0441 \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0438 \u0442\u0438\u043f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0437\u0430\u0434\u0430\u0435\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c: <code>app.add_routes([web.get('\/weather', handle)])<\/code>.<\/p>\n<p>\u0421\u0442\u043e\u043f! \u0410 \u0433\u0434\u0435 \u0436\u0435 \u043d\u0430\u0448 \u0441\u0442\u0430\u0440\u044b\u0439 \u0434\u0440\u0443\u0433 <code>asyncio.run<\/code>? \u041d\u0435 \u043f\u0435\u0440\u0435\u0436\u0438\u0432\u0430\u0439\u0442\u0435. \u041a\u043e\u0433\u0434\u0430 \u043c\u044b \u0438\u043c\u0435\u0435\u043c \u0434\u0435\u043b\u043e \u0441 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c\u0438 \u0432\u0435\u0431-\u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430\u043c\u0438 (\u0430 <code>aiohttp<\/code> \u2014 \u044d\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a, \u0445\u043e\u0442\u044c \u0438 \u0441\u0443\u043f\u0435\u0440-\u043c\u0438\u043d\u0438\u043c\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439), \u0432\u0441\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u043f\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044e \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0443 \u0437\u0430\u0434\u0430\u0447 <code>asyncio<\/code> \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0443 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430 &#171;\u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c&#187;. \u0413\u0434\u0435-\u0442\u043e \u0433\u043b\u0443\u0431\u043e\u043a\u043e \u0432 \u043d\u0435\u0434\u0440\u0430\u0445 &#8216;web.run_app&#8217; \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u0437\u0430\u0434\u0430\u0447\u0438, \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0446\u0438\u043a\u043b \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0432 <code>asyncio.run<\/code>. \u041d\u0430\u043c, \u043a\u0430\u043a \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u043c, \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0435\u0442 \u043d\u0443\u0436\u0434\u044b \u0431\u0435\u0441\u043f\u043e\u043a\u043e\u0438\u0442\u0441\u044f \u043e\u0431 \u044d\u0442\u0438\u0445 \u043d\u0438\u0437\u043c\u0435\u043d\u043d\u044b\u0445 \u0434\u0435\u0442\u0430\u043b\u044f\u0445. \u041f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043c\u0438\u0440\u043d\u043e \u0441\u043f\u0438\u0442 \u0432 \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e\u043c \u0446\u0438\u043a\u043b\u0435, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043f\u0440\u0438\u0434\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 GET \u043d\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 URL. \u041a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u0442\u043e \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0435\u0442, \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043b\u043e\u0433\u0438\u043a\u0430 \u0432 \u0440\u0443\u0447\u043a\u0435. \u0418 \u0441\u043d\u043e\u0432\u0430 \u0431\u0430\u044e-\u0431\u0430\u0439 \u0434\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u041d\u043e \u0435\u0441\u043b\u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0435\u0449\u0435 \u043d\u0435 \u0443\u0441\u043f\u0435\u043b \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c\u0441\u044f, \u043a\u0430\u043a \u043f\u043e\u0441\u0442\u0443\u043f\u0438\u043b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439, \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0435\u0433\u043e \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435, \u043d\u0435 \u0434\u043e\u0436\u0438\u0434\u0430\u044f\u0441\u044c (\u043f\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438) \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043f\u0435\u0440\u0432\u043e\u0433\u043e. \u0412 \u044d\u0442\u043e\u043c \u0441\u0430\u043c\u0430 \u0441\u0443\u0442\u044c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>\u0417\u0430\u0445\u043e\u0434\u0438\u043c \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u043e\u043c \u043d\u0430 \u0430\u0434\u0440\u0435\u0441: <code>localhost:8080\/weather?city=Sochi<\/code> \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u0438\u043c\u043f\u0430\u0442\u0438\u0447\u043d\u044b\u0439 json:<\/p>\n<pre><code>{\"city\": \"Sochi\", \"weather\": \"Clouds\"} <\/code><\/pre>\n<p>\u041a\u0441\u0442\u0430\u0442\u0438, \u0435\u0441\u043b\u0438 \u0432\u044b \u0432\u0441\u0435\u0440\u044c\u0435\u0437 \u0440\u0435\u0448\u0438\u043b\u0438 \u0437\u0430\u043d\u044f\u0442\u044c\u0441\u044f \u0431\u044d\u043a\u0435\u043d\u0434-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u043e\u0439, \u043e\u0434\u043d\u0438\u043c \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u043e\u043c \u0432\u0430\u043c \u043d\u0438\u043a\u0430\u043a \u043d\u0435 \u043e\u0431\u043e\u0439\u0442\u0438\u0441\u044c. \u041f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0439 \u0437\u0430\u043b\u0435\u0437\u0430\u0442\u044c \u0432\u0433\u043b\u0443\u0431\u044c HTTP. \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442 \u0434\u0435-\u0444\u0430\u043a\u0442\u043e \u0437\u0434\u0435\u0441\u044c <a href=\"https:\/\/www.postman.com\" rel=\"noopener noreferrer nofollow\">Postman<\/a>, \u043d\u043e \u0432 \u043f\u0440\u0438\u0440\u043e\u0434\u0435 \u0435\u0441\u0442\u044c \u0438 \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f. <\/p>\n<p>\u0421\u043a\u0435\u043b\u0435\u0442 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0433\u043e\u0442\u043e\u0432, \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u043d\u0430\u0440\u0430\u0449\u0438\u0432\u0430\u0442\u044c \u043d\u0430 \u043d\u0435\u0433\u043e \u043c\u044b\u0448\u0446\u044b. \u0412\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u044b\u043c API-\u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u043e\u043c <code>libretranslate.de<\/code>:<\/p>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440 6.2<\/h3>\n<pre><code class=\"python\">import json from aiohttp import ClientSession, web   async def get_weather(city):     async with ClientSession() as session:         url = f'http:\/\/api.openweathermap.org\/data\/2.5\/weather' \\               f'?q={city}&amp;APPID=2a4ff86f9aaa70041ec8e82db64abf56'          async with session.get(url) as response:             weather_json = await response.json()             try:                 return weather_json[\"weather\"][0][\"main\"]             except KeyError:                 return '\u041d\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0445'   async def get_translation(text, source, target):     async with ClientSession() as session:         url = 'https:\/\/libretranslate.de\/translate'          data = {'q': text, 'source': source, 'target': target, 'format': 'text'}          async with session.post(url, json=data) as response:             translate_json = await response.json()              try:                 return translate_json['translatedText']             except KeyError:                 return text   async def handle(request):     city_ru = request.rel_url.query['city']     city_en = await get_translation(city_ru, 'ru', 'en')      weather_en = await get_weather(city_en)     weather_ru = await get_translation(weather_en, 'en', 'ru')      result = {'city': city_ru, 'weather': weather_ru}      return web.Response(text=json.dumps(result, ensure_ascii=False))   if __name__ == '__main__':     app = web.Application()     app.add_routes([web.get('\/weather', handle)])     web.run_app(app) <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0432 \u0440\u0443\u0447\u043a\u0435 \u0434\u0432\u0430\u0436\u0434\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>get_translation<\/code> \u0438, \u0432\u0443\u0430\u043b\u044f:<\/p>\n<p><code>localhost:8080\/weather?city=\u0421\u043e\u0447\u0438<\/code><\/p>\n<pre><code>{\"city\": \"\u0421\u043e\u0447\u0438\", \"weather\": \"\u041e\u0431\u043b\u0430\u043a\u0430\"} <\/code><\/pre>\n<p>\u0427\u0442\u043e \u044d\u0442\u043e \u0437\u0430 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441 \u0431\u0435\u0437 \u043b\u043e\u0433\u0433\u0435\u0440\u0430? \u041e\u0434\u043d\u0430\u043a\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0430\u0441\u043a\u0432\u043e\u0437\u044c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u0443\u044e \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0443\u044e (\u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0443\u044e) \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 <code>logging<\/code> \u2014 \u044d\u0442\u043e \u043d\u0435\u043f\u0440\u043e\u0444\u0435\u0441\u0441\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e. \u0412\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439 \u043b\u043e\u0433\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f <code>aiologger<\/code>:<\/p>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440 6.3<\/h3>\n<pre><code class=\"python\">import json from aiohttp import ClientSession, web from aiologger.loggers.json import JsonLogger   logger = JsonLogger.with_default_handlers(             level='DEBUG',             serializer_kwargs={'ensure_ascii': False},         )   async def get_weather(city):     async with ClientSession() as session:         url = f'http:\/\/api.openweathermap.org\/data\/2.5\/weather' \\               f'?q={city}&amp;APPID=2a4ff86f9aaa70041ec8e82db64abf56'          async with session.get(url) as response:             weather_json = await response.json()             try:                 return weather_json[\"weather\"][0][\"main\"]             except KeyError:                 logger.error(f'\u041d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u043e\u0433\u043e\u0434\u0443 \u0434\u043b\u044f \u0433\u043e\u0440\u043e\u0434\u0430: {city}')                 return '\u041d\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0445'   async def get_translation(text, source, target):     await logger.info(f'\u041f\u043e\u0441\u0442\u0443\u043f\u0438\u043b \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u043d\u0430 \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u043b\u043e\u0432\u0430: {text}')      async with ClientSession() as session:         url = 'https:\/\/libretranslate.de\/translate'          data = {'q': text, 'source': source, 'target': target, 'format': 'text'}          async with session.post(url, json=data) as response:             translate_json = await response.json()              try:                 return translate_json['translatedText']             except KeyError:                 logger.error(f'\u041d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0434\u043b\u044f \u0441\u043b\u043e\u0432\u0430: {text}')                 return text   async def handle(request):     city_ru = request.rel_url.query['city']      await logger.info(f'\u041f\u043e\u0441\u0442\u0443\u043f\u0438\u043b \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u0433\u043e\u0440\u043e\u0434: {city_ru}')      city_en = await get_translation(city_ru, 'ru', 'en')     weather_en = await get_weather(city_en)     weather_ru = await get_translation(weather_en, 'en', 'ru')      result = {'city': city_ru, 'weather': weather_ru}      return web.Response(text=json.dumps(result, ensure_ascii=False))   if __name__ == '__main__':     app = web.Application()     app.add_routes([web.get('\/weather', handle)])     web.run_app(app) <\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043b\u043e\u0433\u0433\u0435\u0440 \u043d\u0438 \u043d\u0430 \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434\u0443 \u043d\u0435 \u0437\u0430\u0434\u0435\u0440\u0436\u0438\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0432 \u0446\u0435\u043b\u043e\u043c. \u041e\u043d \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u0443\u0437\u044b, \u043f\u043e\u043a\u0430 \u043c\u044b \u043e\u0436\u0438\u0434\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u043b\u0438 \u043e\u0442\u0432\u0435\u0442\u0430 \u043e\u0442 \u0432\u043d\u0435\u0448\u043d\u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430.<\/p>\n<p>\u041d\u0443 \u0430 \u0442\u0435\u043f\u0435\u0440\u044c \u0432\u0438\u0448\u0435\u043d\u043a\u0430 \u043d\u0430 \u0442\u043e\u0440\u0442\u0435 \u2014 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u0447\u0442\u043e-\u0442\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u0432 \u0411\u0414, \u043d\u0443, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u043f\u043e\u0441\u0442\u0443\u043f\u0438\u0432\u0448\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b (\u043d\u0438\u0447\u0435\u0433\u043e \u0431\u043e\u043b\u0435\u0435 \u0443\u043c\u043d\u043e\u0433\u043e \u043c\u043d\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043d\u0435 \u043f\u0440\u0438\u0448\u043b\u043e). \u0421\u0430\u043c\u0430\u044f \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u0411\u0414 \u0432 \u043c\u0438\u0440\u0435, \u043a\u0430\u043a \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u2014 \u044d\u0442\u043e SQLite. \u0418 \u0434\u043b\u044f \u043d\u0435\u0435, \u043a \u0441\u0447\u0430\u0441\u0442\u044c\u044e, \u0435\u0441\u0442\u044c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u0434\u0440\u0430\u0439\u0432\u0435\u0440 <code>aiosqlite<\/code>. \u041f\u0440\u043e\u0431\u0443\u0435\u043c:<\/p>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440 6.4<\/h3>\n<pre><code class=\"python\">import json import aiosqlite import asyncio from aiohttp import ClientSession, web from aiologger.loggers.json import JsonLogger from datetime import datetime   logger = JsonLogger.with_default_handlers(             level='DEBUG',             serializer_kwargs={'ensure_ascii': False},         )   async def create_table():     async with aiosqlite.connect('weather.db') as db:         await db.execute('CREATE TABLE IF NOT EXISTS requests '                          '(date text, city text, weather text)')         await db.commit()   async def save_to_db(city, weather):     async with aiosqlite.connect('weather.db') as db:         sql = f'INSERT INTO requests VALUES (\"{datetime.now()}\", \"{city}\", \"{weather}\")'          await db.execute(sql)         await db.commit()   async def get_weather(city):     async with ClientSession() as session:         url = f'http:\/\/api.openweathermap.org\/data\/2.5\/weather' \\               f'?q={city}&amp;APPID=2a4ff86f9aaa70041ec8e82db64abf56'          async with session.get(url) as response:             weather_json = await response.json()             try:                 return weather_json[\"weather\"][0][\"main\"]             except KeyError:                 logger.error(f'\u041d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u043e\u0433\u043e\u0434\u0443 \u0434\u043b\u044f \u0433\u043e\u0440\u043e\u0434\u0430: {city}')                 return '\u041d\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0445'   async def get_translation(text, source, target):     await logger.info(f'\u041f\u043e\u0441\u0442\u0443\u043f\u0438\u043b \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u043d\u0430 \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u043b\u043e\u0432\u0430: {text}')      async with ClientSession() as session:         url = 'https:\/\/libretranslate.de\/translate'          data = {'q': text, 'source': source, 'target': target, 'format': 'text'}          async with session.post(url, json=data) as response:             translate_json = await<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-334586","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/334586","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=334586"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/334586\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=334586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=334586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=334586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}