{"id":336164,"date":"2022-07-25T15:00:39","date_gmt":"2022-07-25T15:00:39","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=336164"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=336164","title":{"rendered":"<span>\u042d\u0432\u043e\u043b\u044e\u0446\u0438\u044f \u0438\u0433\u0440\u043e\u0432\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u043e\u0433\u043e \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430 \u043d\u0430 Python. \u0427\u0430\u0441\u0442\u044c 1 \u0438\u0437 2. \u0421\u043b\u043e\u0438 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b<\/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>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u043f\u043b\u0430\u043d\u044b, \u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0438\u0433\u0440\u043e\u0432\u044b\u0445 \u0436\u0430\u043d\u0440\u043e\u0432. \u041e\u0434\u043d\u0430\u043a\u043e, \u043f\u0440\u0435\u0436\u0434\u0435, \u0447\u0435\u043c \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u043a \u044d\u0442\u043e\u043c\u0443, \u043d\u0443\u0436\u043d\u043e \u0445\u043e\u0440\u043e\u0448\u0435\u043d\u044c\u043a\u043e \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u0442\u044c\u0441\u044f. \u041d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0442\u0430\u043a\u0443\u044e \u043e\u0441\u043d\u043e\u0432\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u044b \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u043b\u0430 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0433\u0440\u044b, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0442\u043e\u043c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u043d\u0438\u0447\u0435\u0433\u043e \u043f\u0435\u0440\u0435\u0434\u0435\u043b\u044b\u0432\u0430\u0442\u044c \u043d\u0430 \u043f\u043e\u043b-\u043f\u0443\u0442\u0438. \u0412 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u0438 \u0432\u0441\u0435 \u0441\u0434\u0435\u043b\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u0438\u0433\u0440\u044b. <\/p>\n<p>\u0413\u043b\u0430\u0432\u043d\u0430\u044f \u043d\u0430\u0448\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u2014 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043d\u0435\u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0435, \u0432\u044b\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0442\u0430\u043a\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0431\u044b \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u043b\u043e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0438\u0433\u0440\u043e\u0432\u044b\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043b\u044e\u0431\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 <strong>\u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e<\/strong> \u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e <strong>\u0431\u044b\u0441\u0442\u0440\u043e<\/strong>! \u0421\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u0435 \u044d\u0442\u0438\u0445 \u043d\u0435\u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b\u0445, \u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0431\u044b, \u0443\u0441\u043b\u043e\u0432\u0438\u0439 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442 \u043d\u0430\u043c \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a, \u044d\u0432\u043e\u043b\u044e\u0446\u0438\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u044b \u0438 \u043d\u0430\u043c\u0435\u0440\u0435\u043d\u044b \u0437\u0434\u0435\u0441\u044c \u043f\u0440\u043e\u0441\u043b\u0435\u0434\u0438\u0442\u044c. \u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u0443\u0434\u0435\u0442 \u043e\u043f\u0438\u0441\u0430\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043d\u043e\u0433\u043e \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430, \u0430 \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u2014 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043b\u043e\u0433\u0438\u043a\u0438 \u043d\u0430 \u0435\u0433\u043e \u043e\u0441\u043d\u043e\u0432\u0435. \u0412\u0441\u0435\u0433\u043e \u2014 \u0434\u0432\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u043d\u0430 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u043e\u043b\u043e\u0433\u0438\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0432\u0441\u0435\u0439 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u043e\u0439 \u0447\u0430\u0441\u0442\u0438.<\/p>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u044f\u0437\u044b\u043a\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u044b\u0431\u0435\u0440\u0435\u043c Python \u0437\u0430 \u0435\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u0443 \u0438 \u044d\u043b\u0435\u0433\u0430\u043d\u0442\u043d\u043e\u0441\u0442\u044c. \u041c\u044b \u043d\u0430\u0447\u043d\u0435\u043c \u0432 \u0441\u043e\u043a\u0435\u0442\u043e\u0432 (<code>asyncio<\/code>), \u0430 \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043c HTTP-\u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c. \u041d\u0430\u0448\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043a\u043e\u0434 \u043b\u043e\u0433\u0438\u043a\u0438 \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0435\u043b \u043e\u0442 \u0442\u0438\u043f\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0438 \u0437\u0430\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0441\u0435\u0442\u0435\u0432\u044b\u0445 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u043e\u0432.<\/p>\n<ol>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/post\/678658\/#section1\" rel=\"noopener noreferrer nofollow\">\u0418\u0441\u0445\u043e\u0434\u043d\u0430\u044f \u0442\u043e\u0447\u043a\u0430<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/post\/678658\/#section2\" rel=\"noopener noreferrer nofollow\">\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043b\u043e\u0433\u0438\u043a\u0438 \u0438 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/post\/678658\/#section3\" rel=\"noopener noreferrer nofollow\">\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/post\/678658\/#section4\" rel=\"noopener noreferrer nofollow\">\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043b\u043e\u0433\u0438\u043a\u0438 \u043d\u0430 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/post\/678658\/#section5\" rel=\"noopener noreferrer nofollow\">\u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/post\/678658\/#section6\" rel=\"noopener noreferrer nofollow\">\u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/post\/678658\/#section7\" rel=\"noopener noreferrer nofollow\">HTTP-\u0441\u0435\u0440\u0432\u0435\u0440<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/post\/678658\/#section8\" rel=\"noopener noreferrer nofollow\">\u0412\u044b\u0432\u043e\u0434\u044b<\/a><\/p>\n<\/li>\n<\/ol>\n<p><a class=\"anchor\" name=\"section1\" id=\"section1\"><\/a><\/p>\n<h2>\u0418\u0441\u0445\u043e\u0434\u043d\u0430\u044f \u0442\u043e\u0447\u043a\u0430<\/h2>\n<p>\u041f\u0440\u0438 \u0440\u0430\u0437\u0431\u043e\u0440\u0435 <a href=\"https:\/\/habr.com\/ru\/post\/677312\/\" rel=\"noopener noreferrer nofollow\">\u043a\u043b\u0438\u0435\u043d\u0442\u0430<\/a> \u043c\u044b \u043f\u0440\u043e\u0441\u043b\u0435\u0434\u0438\u043b\u0438, \u043a\u0430\u043a \u0438\u0437 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \u2014 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u043e\u0434\u0435\u043b\u044c, \u0430 \u043f\u043e\u0442\u043e\u043c \u0438 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440. \u041f\u043e\u0442\u043e\u043c \u043c\u044b \u0432\u0441\u0435 \u0438\u0433\u0440\u043e\u0432\u044b\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0432\u043c\u0435\u0441\u0442\u043e \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u043f\u0440\u043e\u0441\u0442\u044b\u043c\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c\u0438-\u043a\u043e\u043c\u0430\u043d\u0434\u0430\u043c\u0438. \u0422\u0430\u043a \u043a\u0430\u043a \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0438 \u043b\u043e\u0433\u0438\u043a\u0430 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0442\u0430\u043a\u0438\u043c\u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u0430\u043c\u0438, \u0442\u043e \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044e \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e, \u0433\u0434\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \u2014 \u0432 \u0442\u043e\u043c \u0436\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0438\u043b\u0438 \u0432 \u0434\u0440\u0443\u0433\u043e\u043c, \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u043d\u043e\u043c \u043d\u0430 \u0438\u043d\u043e\u0439 \u043c\u0430\u0448\u0438\u043d\u0435, \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0439 \u0437\u0430 \u0442\u044b\u0441\u044f\u0447\u0438 \u043a\u0438\u043b\u043e\u043c\u0435\u0442\u0440\u043e\u0432. \u041d\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0431\u0435\u0437 \u043b\u043e\u0433\u0438\u043a\u0438, \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0448\u043b\u044e\u0437 \u043c\u0435\u0436\u0434\u0443 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435\u043c \u0438 \u043b\u043e\u0433\u0438\u043a\u043e\u0439, \u043c\u0435\u0436\u0434\u0443 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u043c \u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c.<\/p>\n<p>\u041d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435, \u043f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0434\u043e\u0439\u0434\u0443\u0442 \u0434\u043e \u043b\u043e\u0433\u0438\u043a\u0438, \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u043c \u0438 \u043f\u0440\u0438\u043d\u044f\u0442\u044c \u043e\u0442 \u043d\u0435\u0433\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f. \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043c\u044b \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u0443\u0435\u043c \u0438\u0445 \u0432 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043a\u043e\u043c\u0430\u043d\u0434 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0438\u0445 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438 \u043d\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0432\u043e\u043a\u0440\u0443\u0433 \u043b\u043e\u0433\u0438\u043a\u0438 \u0432\u044b\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0446\u0435\u043b\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0430\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u043e\u0438\u0442 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0442\u044c. \u0427\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0432 \u043d\u0435\u0439 \u0441\u0430\u043c\u044b\u043c \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043d\u0430\u0447\u043d\u0435\u043c \u0441 \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0441\u043e\u043a\u0435\u0442-\u0441\u0435\u0440\u0432\u0435\u0440\u0430.<\/p>\n<p>\u041f\u0440\u043e \u0442\u043e, \u043a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u0441\u043e\u043a\u0435\u0442\u044b \u0432 Python, \u043c\u044b \u043f\u0438\u0441\u0430\u043b\u0438 \u0432 <a href=\"https:\/\/habr.com\/ru\/post\/676110\/\" rel=\"noopener noreferrer nofollow\">\u0434\u0440\u0443\u0433\u043e\u043c \u043c\u0435\u0441\u0442\u0435<\/a>. \u0422\u0443\u0442 \u043c\u044b \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u043c \u0441 \u0442\u043e\u0433\u043e, \u043d\u0430 \u0447\u0435\u043c \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043b\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/676144\/\" rel=\"noopener noreferrer nofollow\">\u0442\u0430\u043c<\/a> \u2014 \u0441 TCP-\u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043d\u0430 <a href=\"https:\/\/docs.python.org\/3\/library\/asyncio.html\" rel=\"noopener noreferrer nofollow\">asyncio<\/a>. \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0432 \u043d\u0435\u0433\u043e \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u0445 \u0434\u0440\u0443\u0433\u0438\u043c \u043a\u043b\u0438\u0435\u043d\u0442\u0430\u043c. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0447\u0435\u0433\u043e \u043d\u0430\u0448\u0430 \u043f\u0435\u0440\u0432\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">last_index = 0 writers = []  async def handle_connection(reader, writer):     global last_index     global writers     writers.append(writer)     last_index += 1     index = last_index     print(\"+Connected\")     unparsed_bytes = b\"\"     while True:         # Receive         try:             request_bytes = await reader.read(1024)         except ConnectionError:             break         if reader.at_eof():             break  # Disconnected by client         request_bytes = unparsed_bytes + request_bytes         request_bytes_list = request_bytes.split(b\"\\x00\")         unparsed_bytes = request_bytes_list.pop()          # Process         for request_bytes in request_bytes_list:             if not request_bytes:                 continue             request = request_bytes.decode(\"utf8\")             print(\" >> Received: {repr(request)}\")             try:                 command = json.loads(request)                 to_self_command, to_all_command = await handle_command(index, command)             except Exception as e:                 print(f\"[SERVER#{index}] Error while parsing or processing: {e}\")                 to_self_command, to_all_command = {\"error\": str(e)}, None             self_response = json.dumps(to_self_command) if to_self_command else None             all_response = json.dumps(to_all_command) if to_all_command else None             print(f\" &lt;&lt; Send: {repr(self_response)} as self_response and commands: \"                   f\"{repr(all_response)} to all {len(writers)} connections\")             if self_response:                 to_self_bytes = self_response.encode(\"utf8\") + b\"\\x00\"                 try:                     writer.write(to_self_bytes)                     await writer.drain()                 except ConnectionError:                     pass  # Yet must send to others             if all_response:                 to_all_bytes = all_response.encode(\"utf8\") + b\"\\x00\"                 for w in writers:                     try:                         w.write(to_all_bytes)                     except ConnectionError:                         continue                 await asyncio.gather(w.drain() for w in self.writers)     writers.remove(writer)     writer.close()     print(f\"-Disconnected\")  async def main(host, port):     print(f\"Start server: {host}:{port}\")     server = await asyncio.start_server(handle_connection, host, port)     async with server:         await server.serve_forever()  HOST, PORT = \"\", 5554 if __name__ == \"__main__\":     asyncio.run(main(HOST, PORT))<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0431\u044b\u043b \u043a\u043e\u0434, \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0449\u0438\u0439 \u0437\u0430 \u043f\u0435\u0440\u0435\u0441\u044b\u043b\u043a\u0443 \u0438 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u0443\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439. \u0411\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u0432 <code>handle_command()<\/code>. \u0412 \u043d\u0435\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e \u0432\u0441\u0435\u0433\u043e \u0442\u0440\u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u044b: \u0432\u0437\u044f\u0442\u044c, \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0438 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c (get, set, update). \u0422\u0430\u043a \u043a\u0430\u043a \u0432 \u043b\u043e\u0433\u0438\u043a\u0435 \u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u0431\u0430\u0437\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u043c \u0438\u043b\u0438 \u0432\u043d\u0435\u0448\u043d\u0438\u043c \u0441\u0435\u0440\u0432\u0438\u0441\u0430\u043c, \u0442\u043e, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043d\u0430 \u044d\u0442\u043e \u0432\u0440\u0435\u043c\u044f, \u0434\u0430\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0442\u0430\u043a\u0436\u0435 \u0441\u0434\u0435\u043b\u0430\u043d\u0430 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0439 (<code>async<\/code>):<\/p>\n<pre><code class=\"python\">storage = {}  async def handle_command(index, command):     global storage     key = command.get(\"key\")     code = command.get(\"code\")     if code == \"get\":         state = storage.get(key)         return {\"success\": True, **command, \"state\": state}, None     elif code == \"set\":         state = command.get(\"state\")         storage[key] = state         return {\"success\": True, **command}, None     elif code == \"update\":         index = command.get(\"index\")         value = command.get(\"value\")         if not isinstance(index, int) or not isinstance(value, int):             return {\"success\": False, **command}, None         state = storage.get(key)         if state is None:             storage[key] = state = []         if index >= len(state):             state += [0] * (index - len(state) + 1)         state[index] = value         return None, {\"success\": True, **command}     return None, None<\/code><\/pre>\n<p>\u041f\u043e\u043a\u0430 \u0447\u0442\u043e \u0432\u0441\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 <code>writers<\/code>, \u0430 \u0432 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0435 \u043a\u043e\u043c\u0430\u043d\u0434 (<code>handle_command()<\/code>) \u043c\u044b \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u0435\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u0432\u0430 \u0442\u0438\u043f\u0430 \u0430\u0434\u0440\u0435\u0441\u0430\u0442\u043e\u0432 \u0434\u043b\u044f \u043e\u0442\u0432\u0435\u0442\u043d\u044b\u0445 \u043a\u043e\u043c\u0430\u043d\u0434: \u043d\u0430\u0437\u0430\u0434 \u043a \u0441\u0435\u0431\u0435 (<code>to_self_command<\/code>), \u0438 \u043a\u043e \u0432\u0441\u0435\u043c (<code>to_all_command<\/code>). \u042d\u0442\u043e \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0438 \u0432 \u043e\u0434\u043d\u043e\u043c \u0438\u0437 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u043c\u044b \u0437\u0430\u043c\u0435\u043d\u0438\u043c \u0435\u0433\u043e \u043d\u0430 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0438\u043d\u0434\u0438\u0432\u0438\u0434\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u0430\u0434\u0440\u0435\u0441\u0430\u0442\u043e\u0432 \u043f\u043e \u0438\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c. \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043a\u043e\u043c\u0430\u043d\u0434 \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0442\u0435\u0436\u0435\u0439, \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439, \u0430 \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u043a\u043e\u043c\u0430\u043d\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u043c \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0430\u044e\u0442\u0441\u044f: <code>result = [(indexes, commands), (indexes2, commands2)]<\/code>. \u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0432\u0441\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u0443\u0434\u0443\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0435 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 <code>writers<\/code>, \u043a\u0430\u043a \u0440\u0430\u043d\u044c\u0448\u0435, \u0430 \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u0435 <code>writer_by_index<\/code>. \u041d\u043e \u043f\u043e\u043a\u0430 \u0432\u0435\u0440\u043d\u0435\u043c\u0441\u044f \u043a \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<p><a class=\"anchor\" name=\"section2\" id=\"section2\"><\/a><\/p>\n<h2>\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043b\u043e\u0433\u0438\u043a\u0438 \u0438 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b<\/h2>\n<p>\u0421\u0435\u0439\u0447\u0430\u0441 \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0446\u0435\u043b\u044c\u043d\u043e\u0435 \u0438 \u043d\u0435\u0440\u0430\u0437\u0434\u0435\u043b\u0438\u043c\u043e\u0435, \u0430 \u043f\u043e\u0442\u043e\u043c\u0443 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e &#171;\u043d\u0435\u0440\u0435\u044e\u0437\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0435&#187;. \u0414\u0440\u0443\u0433\u0438\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0435\u0433\u043e \u0447\u0430\u0441\u0442\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e, \u0442\u0430\u043a \u043a\u0430\u043a \u0432\u0441\u0435 \u043e\u043d\u0438 \u0436\u0435\u0441\u0442\u043a\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u044b \u0434\u0440\u0443\u0433 \u0441 \u0434\u0440\u0443\u0433\u043e\u043c. \u0425\u043e\u0442\u044c \u043b\u043e\u0433\u0438\u043a\u0430 \u0438 \u0432\u044b\u043d\u0435\u0441\u0435\u043d\u0430 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>handle_command()<\/code>, \u043d\u043e \u0432 <code>handle_connection()<\/code> \u0432\u043c\u0435\u0441\u0442\u043e \u043d\u0435\u0435 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0434\u0440\u0443\u0433\u0443\u044e. \u0415\u0441\u043b\u0438 \u043c\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u043e\u0432\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441 \u0434\u0440\u0443\u0433\u043e\u0439 \u043b\u043e\u0433\u0438\u043a\u043e\u0439, \u0442\u043e \u043d\u0430\u043c \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 <code>handle_connection()<\/code>. \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u043d\u0430\u0441 \u044d\u0442\u043e \u043d\u0435 \u0443\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u0442.<\/p>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0441 \u043b\u043e\u0433\u0438\u043a\u043e\u0439 (<code>handle_command()<\/code>) \u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u0445 <code>handle_connection()<\/code>. \u0418\u043b\u0438 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u043e\u0431\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u043a\u043b\u0430\u0441\u0441, \u0430 \u0432 \u043f\u043e\u0434\u043a\u043b\u0430\u0441\u0441\u0430\u0445 \u0438\u0445 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c. \u041d\u043e \u043b\u0443\u0447\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u043b\u043e\u0433\u0438\u043a\u0443 (\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043a\u043e\u043c\u0430\u043d\u0434) \u0438 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 (\u043e\u0431\u043c\u0435\u043d \u043a\u043e\u043c\u0430\u043d\u0434\u0430\u043c\u0438 \u043f\u043e \u0441\u0435\u0442\u0438) \u0432\u043e\u043e\u0431\u0449\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0434\u0432\u0443\u0445 \u0440\u0430\u0437\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u0430\u0445.<\/p>\n<p>\u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u043e \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u043f\u0435\u0440\u0435\u0434 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0432 \u043d\u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0438\u0437 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445-\u0447\u043b\u0435\u043d\u043e\u0432 (\u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432), \u0430 \u0442\u0430\u043a\u0436\u0435 \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u0432 \u043f\u043e\u0434\u043a\u043b\u0430\u0441\u0441\u0430\u0445 \u043c\u0435\u0442\u043e\u0434\u044b. <\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/81d\/e21\/c7b\/81de21c7b21dc0a5bb3e8103a8b62b08.png\" alt=\"\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 (Server) \u0438 \u043b\u043e\u0433\u0438\u043a\u0443 (Logic)\" title=\"\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 (Server) \u0438 \u043b\u043e\u0433\u0438\u043a\u0443 (Logic)\" width=\"141\" height=\"192\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/81d\/e21\/c7b\/81de21c7b21dc0a5bb3e8103a8b62b08.png\"\/><figcaption>\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 (Server) \u0438 \u043b\u043e\u0433\u0438\u043a\u0443 (Logic)<\/figcaption><\/figure>\n<p>\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043d\u0430 \u0434\u0432\u0430 \u043a\u043b\u0430\u0441\u0441\u0430 \u0437\u0430\u0434\u0430\u0435\u0442 \u0434\u0432\u0430 \u043c\u0430\u0433\u0438\u0441\u0442\u0440\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0443\u0442\u0438 \u0440\u0430\u0437\u0432\u0438\u0442\u0438\u044f \u0432 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430. \u0421 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0438 \u0432\u0435\u0440\u0441\u0438\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u0430 \u043f\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 (Server), \u0441 \u0434\u0440\u0443\u0433\u043e\u0439 \u2014 \u043d\u0430\u0434\u0435\u043b\u0430\u0442\u044c \u043a\u0443\u0447\u0443 \u0440\u0430\u0437\u043d\u044b\u0445 \u0438\u0433\u0440 (Logic). \u0418 \u0432\u0441\u0435 \u044d\u0442\u043e \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e \u0434\u0440\u0443\u0433 \u043e\u0442 \u0434\u0440\u0443\u0433\u0430. \u0415\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0441\u043e\u0431\u043b\u044e\u0434\u0430\u0442\u044c\u0441\u044f, \u044d\u0442\u043e \u0447\u0442\u043e\u0431\u044b \u043a\u043b\u0430\u0441\u0441 \u043b\u043e\u0433\u0438\u043a\u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043b \u043c\u0435\u0442\u043e\u0434 \u0441 \u0442\u0430\u043a\u043e\u0439 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u043e\u0439: <code>handle_command(self, index, command)<\/code>:<\/p>\n<pre><code class=\"python\">class SocketServer:     def __init__(self, logic, host, port):         self.logic = logic         self.host = host         self.port = port         self.writers = []      def run(self):         asyncio.run(self.main())      async def main(self):         print(f\"Start server: {self.host}:{self.port}\")         server = await asyncio.start_server(handle_connection, host, port)         async with server:             await server.serve_forever()      async def handle_connection(self, reader, writer):         ...         # Call:         # to_self_command, to_all_command = await self.logic.handle_command(index, command)         # instead of:         # to_self_command, to_all_command = await handle_command(index, command)  class MyLogic:     # global storage -> self.storage     def __init__(self):       self.storage = {}      async def handle_command(self, index, command):     ...<\/code><\/pre>\n<p>\u041a\u043e\u0434 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0441\u044f:<\/p>\n<pre><code class=\"python\">HOST, PORT = \"\", 5000 if __name__ == \"__main__\":     server = SocketServer(MyLogic(), HOST, PORT)     server.run()<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0441\u0435\u0440\u0432\u0435\u0440 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u043b\u043e\u0433\u0438\u043a\u0438 \u0438 \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043a \u043d\u0435\u043c\u0443 \u0441\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0443. \u0421\u0432\u044f\u0437\u044c \u044d\u0442\u0430 \u043e\u0434\u043d\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f, \u0442\u0430\u043a \u043a\u0430\u043a \u043b\u043e\u0433\u0438\u043a\u0430 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0437\u043d\u0430\u0435\u0442 \u043e\u0431 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435.<\/p>\n<p>\u042d\u0442\u043e \u0431\u044b\u043b \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0435\u0440\u0432\u044b\u0439 \u0448\u0430\u0433. \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0431\u0443\u0434\u0435\u0442 \u2014 \u043e\u0442\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u0430 \u043f\u043e \u0442\u0440\u0430\u043d\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 (\u0441\u043e\u043a\u0435\u0442\u044b) \u043e\u0442 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0438\u0445 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f (JSON, YML, XML).<\/p>\n<p><a class=\"anchor\" name=\"section3\" id=\"section3\"><\/a><\/p>\n<h2>\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439<\/h2>\n<p>\u041e\u0442\u0434\u0435\u043b\u0438\u0442\u044c \u043b\u043e\u0433\u0438\u043a\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043e\u0442 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0431\u044b\u043b\u043e \u0445\u043e\u0440\u043e\u0448\u0435\u0439 \u0438\u0434\u0435\u0435\u0439. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043b\u043e\u0433\u0438\u043a\u0430 \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442 \u043e\u0434\u043d\u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0432 \u0434\u0440\u0443\u0433\u0438\u0435, \u043f\u043e\u043f\u0443\u0442\u043d\u043e \u043c\u0435\u043d\u044f\u044f \u0441\u0432\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043e\u043d\u0430 \u043d\u0435 \u0437\u0430\u0434\u0443\u043c\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u043e\u0442\u043a\u0443\u0434\u0430 \u043e\u043d\u0438 \u0431\u0435\u0440\u0443\u0442\u0441\u044f \u0438 \u043a\u0443\u0434\u0430 \u0434\u0435\u0432\u0430\u044e\u0442\u0441\u044f. \u0417\u0430 \u0432\u0441\u0435 \u044d\u0442\u043e \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u043a\u043b\u0430\u0441\u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 (<code>SocketServer<\/code>). \u0412\u043e\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c \u0441\u0435\u0439\u0447\u0430\u0441 \u0438 \u0437\u0430\u0439\u043c\u0435\u043c\u0441\u044f. \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043d\u0430 \u043a\u0430\u043a\u0438\u0435 \u0447\u0430\u0441\u0442\u0438, \u0432 \u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0440\u0430\u0441\u043f\u0430\u0434\u0430\u0435\u0442\u0441\u044f \u043e\u043d \u0441\u0430\u043c. <\/p>\n<p>\u0417\u0430\u0433\u043b\u044f\u043d\u0443\u0432 \u0432\u043d\u0443\u0442\u0440\u044c \u043d\u0435\u0433\u043e, \u043c\u044b \u0443\u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 (JSON) \u0442\u0430\u043c \u0436\u0435\u0441\u0442\u043a\u043e \u0437\u0430\u0432\u044f\u0437\u0430\u043d \u043d\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0438\u0445 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 (TCP-\u0441\u043e\u043a\u0435\u0442\u044b). \u0418 \u0435\u0441\u043b\u0438 \u043c\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0440\u0443\u0433\u043e\u0439 \u0444\u043e\u0440\u043c\u0430\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u0442\u043e \u043d\u0430\u043c \u0437\u0430\u043e\u0434\u043d\u043e \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0443\u0447\u0443 \u043a\u043e\u0434\u0430, \u043a \u0444\u043e\u0440\u043c\u0430\u0442\u0443 \u043d\u0435 \u043e\u0442\u043d\u043e\u0441\u044f\u0449\u0435\u0433\u043e\u0441\u044f.<\/p>\n<p>\u041f\u0435\u0440\u0432\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u043d\u0430 \u0443\u043c \u2014 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u0430\u0445 (\u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b <code>parse()<\/code> \u0438 <code>serialize()<\/code>):<\/p>\n<pre><code class=\"python\">class SocketServer:     def __init__(self, logic, host, port) -> None:         self.logic = logic         self.host = host         self.port = port         self.last_index = 0         self.writer_by_index = {}      async def handle_connection(self, reader, writer):         self.last_index += 1         index = self.last_index         self.writer_by_index[index] = writer         print(f\"[SERVER#{index}] +Connected\")         unparsed_bytes = b\"\"         while True:             # Receive             try:                 request_bytes = await reader.read(1024)             except ConnectionError:                 break             if reader.at_eof():                 print(f\"[SERVER#{index}] EOF. Connection closed\")                 break             request_bytes = unparsed_bytes + request_bytes             # Handle             result, unparsed_bytes = await self.handle_bytes(index, request_bytes)             # Send response             await self.send(result)         print(f\"[SERVER#{index}] -Disconnected\")         del self.writer_by_index[index]         writer.close()  async def handle_bytes(self, index, request_bytes):       # Decode request       request, unparsed_bytes = self.parse(request_bytes)       # Make response       try:           # Parse request           command = json.loads(request)           # Process request           result = self.logic.handle_command(index, command)       except Exception as e:           print(f\"[SERVER#{index}] Error while parsing or processing: {request} {traceback.format_exc()}\")           result = [([index], [{\"error\": str(e)}])]       return result, unparsed_bytes      async def send(self, result):         if not result:             return         # Serialize         result = [(indexes, self.serialize(commands))                   for indexes, commands in result]         # Send         wait_writers = []         for indexes, response_bytes in result:             for i in indexes:                 writer = self.writer_by_index.get(i)                 if writer:                     try:                         writer.write(response_bytes)                         wait_writers.append(writer)                     except ConnectionError:                         continue         await asyncio.gather(writer.drain() for writer in wait_writers)      def parse(self, data_bytes):         request_bytes, unparsed_bytes = request_bytes.split(b\"\\x00\")         request = request_bytes.decode(\"utf8\")         return request, unparsed_bytes      def serialize(self, data):         return json.dumps(commands).encode(\"utf8\") + b\"\\x00\"<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043c\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u043c \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c JSON \u043d\u0430 YML, \u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0442 SocketServer \u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c <code>parse()<\/code> \u0438 <code>serialize()<\/code>. \u041d\u043e \u043f\u0440\u0438 \u0442\u0430\u043a\u043e\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0435 \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0432\u0441\u0435 \u0442\u043e\u0442 \u0436\u0435 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a \u043f\u0440\u0438 \u043a\u043e\u043c\u0431\u0438\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0440\u0430\u0437\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u043e\u0432. \u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0438 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441: JSONTCPSocketServer, YMLUDPSocketServer, JSONHTTPServer \u0438 \u0442\u0430\u043a \u0434\u0430\u043b\u0435\u0435. \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0439 \u0432\u043e\u043f\u0440\u043e\u0441 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435.<\/p>\n<p>\u041a\u043e\u0434 \u043f\u043e \u0434\u043e\u0441\u0442\u0430\u0432\u043a\u0435 (\u0441\u043e\u043a\u0435\u0442\u044b) \u0438 \u043a\u043e\u0434 \u043f\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044e (JSON) \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0432 \u043e\u0434\u043d\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435. \u042d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441\u044b \u0434\u043b\u044f \u0434\u0432\u0443\u0445 \u0442\u0438\u043f\u043e\u0432 \u0441\u043e\u043a\u0435\u0442\u043e\u0432 (TCP \u0438 UDP) \u0438 \u0442\u0440\u0435\u0445 \u0432\u0438\u0434\u043e\u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u043e\u0432 (JSON, YML, XML), \u0442\u043e \u0432 \u0438\u0442\u043e\u0433\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c 2 * 3 = 6 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0439. \u0425\u043e\u0442\u044f \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u043f\u043e \u0438\u0434\u0435\u0435 2 + 3 = 5. \u041f\u0443\u0441\u0442\u044c 6 \u0438 5 \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u043d\u0435 \u0441\u0438\u043b\u044c\u043d\u043e, \u043d\u043e \u0438\u043c\u0435\u0442\u044c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0437\u0430\u043a\u043e\u043d\u0430 \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044f \u043a\u043e\u0434\u0430 \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435 \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0434\u0430\u0435\u0442 \u0443\u0436\u0435 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u044d\u0442\u0430\u043f\u0435 \u0438\u0437\u0431\u044b\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u0432 33 % (3 * 3 = 9, 3 + 3 = 6). \u0418 \u0442\u043e, \u0447\u0442\u043e \u043a\u043e\u0434 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043d\u0435 \u0434\u0443\u0431\u043b\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u2014 \u0437\u0430\u0441\u043b\u0443\u0433\u0430 Python&#8217;\u0430 (\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435), \u0430 \u043d\u0435 \u043d\u0430\u0448\u0430.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/20b\/141\/286\/20b141286738cb536d760a8f39f45afc.png\" alt=\"\u0412\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u0440\u0441\u0435\u0440\u0430 \u043a\u0430\u043a \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u044f\" title=\"\u0412\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u0440\u0441\u0435\u0440\u0430 \u043a\u0430\u043a \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u044f\" width=\"228\" height=\"186\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/20b\/141\/286\/20b141286738cb536d760a8f39f45afc.png\"\/><figcaption>\u0412\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u0440\u0441\u0435\u0440\u0430 \u043a\u0430\u043a \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u044f<\/figcaption><\/figure>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u043f\u043e-\u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e\u043c\u0443 \u0438 \u0440\u0430\u0437\u043d\u0435\u0441\u0442\u0438 \u0434\u043e\u0441\u0442\u0430\u0432\u043a\u0443 \u0438 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u0443\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u043f\u043e \u0440\u0430\u0437\u043d\u044b\u043c \u043a\u043b\u0430\u0441\u0441\u0430\u043c. \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u0438\u0437 \u043a\u043b\u0430\u0441\u0441\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043f\u0430\u0440\u0441\u0435\u0440:<\/p>\n<pre><code class=\"python\">class SocketServer:     def __init__(self, parser, logic, host, port) -> None:         self.parser = parser         self.logic = logic         self.host = host         self.port = port         self.last_index = 0         self.writer_by_index = {} # ...     async def handle_bytes(self, index, request_bytes):         try:             # Parse             commands, unparsed_bytes = self.parser.parse(request_bytes)             # Handle             result = await self.logic.handle_commands(index, commands)         except Exception as e:             result = [([index], [{\"error\": str(e)}])] unparsed_bytes = b\"\"         return result, unparsed_bytes      async def send(self, result):         if not result:             return         # Serialize         result = [(indexes, self.parser.serialize(commands))                   for indexes, commands in result]         # Send         wait_writers = []         for indexes, response_bytes in result:             for i in indexes:                 writer = self.writer_by_index.get(i)                 if writer:                     try:                         writer.write(response_bytes)                         wait_writers.append(writer)                     except ConnectionError:                         continue         await asyncio.gather(writer.drain() for writer in wait_writers)  class Parser:     def parse(self, data_bytes):         return data_bytes, b\"\"      def serialize(self, data):         return data  class JSONParser(Parser):     def parse(self, data_bytes):         # Get unparsed_bytes         data_bytes, unparsed_bytes = data_bytes.rsplit(b\"\\x00\", 1)         # bytes -> list of str         data_str = data_bytes.decode(\"utf8\")         message_list = data_str.split(\"\\x00\")         # Parse JSON commands (suppose, a command cannot be a list)         result = []         for message in message_list:             if not message:                 continue             commands = json.loads(message)             if not commands:                 continue             if isinstance(commands, list):                 result.extend(commands)             else:                 result.append(commands)         return result, unparsed_bytes      def serialize(self, data):         if not data:             return b\"\"         data_str = json.dumps(data)         data_bytes = data_str.encode(\"utf8\") + b\"\\x00\"         return data_bytes  class MyLogic:     async def handle_commands(self, index, commands):         # Custom logic         result = []         for command in commands:             key = command.get(\"key\")             code = command.get(\"code\")             ... return result  HOST, PORT = \"\", 5000 if __name__ == \"__main__\":     server = SocketServer(JSONParser(), MyLogic(), HOST, PORT)     server.run()<\/code><\/pre>\n<p>\u041e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c\u0441\u044f \u043d\u0430 \u043a\u043e\u0435-\u043a\u0430\u043a\u0438\u0445 \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0434\u0435\u0442\u0430\u043b\u044f\u0445. <\/p>\n<p>\u0421\u0435\u0440\u0432\u0435\u0440 \u043c\u043e\u0436\u0435\u0442 \u0437\u0430 \u0440\u0430\u0437 \u043f\u0440\u0438\u043d\u044f\u0442\u044c \u043e\u0434\u043d\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435, \u0430 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u043d\u044f\u0442\u044c \u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u2014 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0442\u043e\u0433\u043e, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432 \u043f\u043e\u0442\u043e\u043a\u0435 \u0431\u0430\u0439\u0442\u043e\u0432 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u0435\u043b\u0435\u0439. \u0421\u043a\u043e\u043b\u044c\u043a\u043e \u0431\u044b\u043b\u043e \u0432 \u0431\u0443\u0444\u0435\u0440\u0435, \u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442. \u0414\u0430 \u0438 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0438, \u0432 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435, \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0441\u0440\u0430\u0437\u0443 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043c\u0430\u043d\u0434 \u0432\u043c\u0435\u0441\u0442\u043e \u043e\u0434\u043d\u043e\u0439. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043b\u043e\u0433\u0438\u0447\u043d\u043e \u0432 \u043f\u0430\u0440\u0441\u0435\u0440\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0441\u0440\u0430\u0437\u0443 \u0441\u043f\u0438\u0441\u043e\u043a \u043a\u043e\u043c\u0430\u043d\u0434 \u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0432\u0441\u0435\u0433\u0434\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u0431\u0443\u0434\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u043f\u0438\u0441\u043e\u043a. \u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0438 <code>handle_command()<\/code> \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u043f\u0438\u0441\u043a\u0430\u043c\u0438. \u041f\u043e\u0442\u043e\u043c\u0443 \u043e\u043d \u0438 \u043f\u0435\u0440\u0435\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d \u0442\u0435\u043f\u0435\u0440\u044c \u0432 <code>handle_commands()<\/code>.<\/p>\n<p>\u0422\u043e, \u0447\u0442\u043e \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0432 \u043f\u0430\u0440\u0441\u0435\u0440 \u0431\u0430\u0439\u0442\u044b, \u0430 \u043d\u0435 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0432 UTF-8 \u0441\u0442\u0440\u043e\u043a\u0438, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0432 \u043d\u0438\u0445 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0435 \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0435 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u044b. \u0420\u0430\u0437\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u043d\u0443\u043b\u0435\u0432\u044b\u043c \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u043c (<code>b\"\\x00\"<\/code>) \u043f\u043e\u043c\u0435\u0449\u0435\u043d\u043e \u0432 \u043f\u0430\u0440\u0441\u0435\u0440 \u043f\u043e \u044d\u0442\u043e\u0439 \u0436\u0435 \u043f\u0440\u0438\u0447\u0438\u043d\u0435. \u0415\u0441\u043b\u0438 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u0440\u0430\u0441\u043f\u0430\u0440\u0441\u0438\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u0443, \u043e\u043d\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f. \u0415\u0441\u043b\u0438 \u043d\u0435\u0442 \u2014 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0442\u0441\u044f \u0431\u0430\u0439\u0442\u044b, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0437\u0436\u0435 \u043a \u043d\u0438\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0435 \u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u0438\u0442\u044c \u043f\u043e\u043f\u044b\u0442\u043a\u0443.<\/p>\n<p>\u041a\u0430\u043a \u0432\u044b \u043c\u043e\u0433\u043b\u0438 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u043c\u044b \u043f\u0435\u0440\u0435\u0448\u043b\u0438 \u043e\u0442 \u043c\u0430\u0441\u0441\u043e\u0432\u043e\u0439 \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u043a \u0442\u043e\u0447\u0435\u0447\u043d\u043e\u0439, \u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0443\u0436\u0435 \u0433\u043e\u0432\u043e\u0440\u0438\u043b\u0438 \u0432\u044b\u0448\u0435. \u0422\u043e \u0435\u0441\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u043e \u043e\u0442\u0441\u044b\u043b\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u0441\u0435\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f\u043c \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 <code>writers<\/code>, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0442\u0435\u043f\u0435\u0440\u044c \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u043f\u043e \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u043c \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439 \u0438\u0437 \u0441\u043b\u043e\u0432\u0430\u0440\u044f <code>writer_by_index<\/code>. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e <code>handle_commands()<\/code> \u043a \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u0443 \u043a\u043e\u043c\u0430\u043d\u0434 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0435\u0449\u0435 \u0441\u043f\u0438\u0441\u043e\u043a \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043e\u043d\u0438 \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u044b.<\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u043e\u0432\u0443\u044e \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0443, \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0432\u0441\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0432 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u043f\u0440\u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0438, \u0430 \u043f\u0440\u0438 \u043e\u0442\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0438 \u2014 \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u0438\u0445 \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u043a\u043b\u0430\u0441\u0441 \u043b\u043e\u0433\u0438\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0435\u0449\u0435 \u043f\u0430\u0440\u0430 \u043c\u0435\u0442\u043e\u0434\u043e\u0432: <code>on_connect()<\/code> \u0438 <code>on_disconnect()<\/code>:<\/p>\n<pre><code class=\"python\">class SocketServer: # ...     async def handle_connection(self, reader, writer):         ...         result = []         await self.logic.on_connect(index, result)         await self.send(result)         unparsed_bytes = b\"\"         while True:             ...         result = []         await self.logic.on_disconnect(index, result)         await self.send(result)         del self.writer_by_index[index]         writer.close()  class MyLogic:     def __init__(self):       self.storage = {}      async def on_connect(self, index, result):         indexes = self.storage.get(\"indexes\")         if indexes is None:           self.storage[\"indexes\"] = [index]         else:           indexes.append(index)      async def on_disconnect(self, index, result):         indexes = self.storage.get(\"indexes\")         if indexes and index in indexes:           indexes.remove(index)      async def handle_commands(self, index, commands):         # Custom logic         result = []         all_indexes = self.storage.get(\"indexes\")         for command in commands:             key = command.get(\"key\")             code = command.get(\"code\")             if code == \"get\":                 state = storage.get(key)                 result.append((all_indexes, [{\"success\": True, **command, \"state\": state}]))             elif code == \"set\":                 state = command.get(\"state\")                 storage[key] = state                 result.append(([index], [{\"success\": True, **command}]))             elif code == \"update\":                 index = command.get(\"index\")                 value = command.get(\"value\")                 if not isinstance(index, int) or not isinstance(value, int):                     result.append(([index], [{\"success\": False, **command}]))                     continue                 state = storage.get(key)                 if state is None:                   storage[key] = state = []                 if index >= len(state):                   state += [0] * (index - len(state) + 1)                 state[index] = value                 result.append((all_indexes, [{\"success\": True, **command}])) return result<\/code><\/pre>\n<p><a class=\"anchor\" name=\"section4\" id=\"section4\"><\/a><\/p>\n<h2>\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043b\u043e\u0433\u0438\u043a\u0438 \u043d\u0430 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b<\/h2>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u0434\u0430\u043b\u0435\u0435, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440\u044b \u043f\u043e \u0438\u0433\u0440\u0435 \u0432 \u0448\u0430\u0445\u043c\u0430\u0442\u044b, \u0448\u0430\u0448\u043a\u0438, \u043a\u0440\u0435\u0441\u0442\u0438\u043a\u0438-\u043d\u043e\u043b\u0438\u043a\u0438&#8230; \u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u043b\u043e\u0433\u0438\u043a\u0438, \u0433\u0434\u0435 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438\u0448\u044c \u043e\u0434\u0438\u043d \u043c\u0435\u0442\u043e\u0434 <code>handle_commands()<\/code> \u2014 \u0438 \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u0434\u0430. \u041f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 <code>SocketServer<\/code> \u043f\u0435\u0440\u0432\u044b\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c \u043e\u0431\u044a\u0435\u043a\u0442 \u043b\u043e\u0433\u0438\u043a\u0438, \u0438 \u0441\u0435\u0440\u0432\u0435\u0440 \u0433\u043e\u0442\u043e\u0432 \u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e:<\/p>\n<pre><code class=\"python\">class ChessLogic(MyLogic):     async def handle_commands(self, index, commands):     ...  class CheckersLogic(MyLogic):     async def handle_commands(self, index, commands):     ...  HOST, PORT = \"\", 5000 if __name__ == \"__main__\":     server = SocketServer(JSONParser(), CheckersLogic(), HOST, PORT)     server.run()<\/code><\/pre>\n<p>\u041d\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0443 \u0432\u0430\u0441 \u0443\u0436\u0435 \u0432\u043e\u0437\u043d\u0438\u043a \u0437\u0430\u043a\u043e\u043d\u043e\u043c\u0435\u0440\u043d\u044b\u0439 \u0432\u043e\u043f\u0440\u043e\u0441. \u0410 \u0447\u0442\u043e, \u0435\u0441\u043b\u0438 \u043c\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440, \u0433\u0434\u0435 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u043f\u043e \u0432\u044b\u0431\u043e\u0440\u0443 \u0438\u0433\u0440\u0430\u0442\u044c \u0438 \u0432 \u0448\u0430\u0445\u043c\u0430\u0442\u044b, \u0438 \u0432 \u0448\u0430\u0448\u043a\u0438? \u0412\u043e\u0442 \u0442\u0443\u0442 \u0443\u0436\u0435 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0438\u0437\u0433\u0430\u043b\u044f\u0442\u044c\u0441\u044f. \u0417\u0434\u0435\u0441\u044c \u043d\u0443\u0436\u0435\u043d \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043a\u043b\u0430\u0441\u0441-\u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440 \u043b\u043e\u0433\u0438\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u043a \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c\u0443 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443:<\/p>\n<pre><code class=\"python\">class ComboLogic:     def __init__(self) -> None:         self.chess = ChessLogic()         self.checkers = CheckersLogic()         # Use same storage for all         self.chess.storage = self.checkers.storage = self.storage = {}      async def on_connect(self, index, result):     ...      async def on_disconnect(self, index, result):     ...      async def handle_commands(self, index, commands):         # Custom logic         result = []         for command in commands:             key = command.get(\"key\")             if key == \"chess\":                 result.extend(await self.chess.handle_commands(index, commands))             elif key == \"checkers\":                 result.extend(await self.checkers.handle_commands(index, commands)) return result  HOST, PORT = \"\", 5000 if __name__ == \"__main__\":     server = SocketServer(JSONParser(), ComboLogic(), HOST, PORT)     server.run()<\/code><\/pre>\n<p>\u041d\u0435\u0441\u043b\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043a\u043e\u043c\u0430\u043d\u0434 \u044f\u0432\u043d\u043e\u0435 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u0434\u0430 (<code>handle_commands()<\/code> \u0438 <code>extend()<\/code>). \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0435\u0433\u043e \u0443\u0441\u0442\u0440\u0430\u043d\u0438\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u043b\u043e\u0432\u0430\u0440\u044f:<\/p>\n<pre><code class=\"python\">class ComboLogic:     def __init__(self) -> None:         self.logic_by_key = {             \"chess\": ChessLogic(parser),             \"checkers\": CheckersLogic(parser),         }         self.storage = {}         # Provide common storage         for logic in self.logic_by_key.items():             logic.storage = self.storage # ...     async def handle_commands(self, index, commands):     # Custom logic     result = []     for command in commands:         key = command.get(\"key\")         logic = self.logic_by_key.get(key)         if logic:             result.extend(await logic.handle_commands(i, [command])) return result  HOST, PORT = \"\", 5000 if __name__ == \"__main__\":     logic_by_key = {         \"chess\": ChessLogic(),         \"checkers\": CheckersLogic(),     }     server = SocketServer(JSONParser(), ComboLogic(logic_by_key), HOST, PORT)     server.run()<\/code><\/pre>\n<p>\u0421\u0435\u0439\u0447\u0430\u0441 \u043a\u043b\u0430\u0441\u0441 \u043b\u043e\u0433\u0438\u043a\u0438 \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0438\u0437 \u0441\u0432\u043e\u0439\u0441\u0442\u0432 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u2014 <code>key<\/code>. \u041d\u043e \u0432\u043f\u043e\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0438, \u043a\u043e\u0433\u0434\u0430 \u0431\u0443\u0434\u0443\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u043a\u043e\u043c\u043d\u0430\u0442\u044b (rooms) \u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0438\u0433\u0440\u043e\u043a\u043e\u0432 \u043f\u043e \u043d\u0438\u043c, \u043c\u043e\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u043f\u043e \u0442\u043e\u043c\u0443, \u0432 \u043a\u0430\u043a\u043e\u0439 \u043a\u043e\u043c\u043d\u0430\u0442\u0435, \u0432 \u043a\u0430\u043a\u043e\u0439 \u0438\u0433\u0440\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c. \u0415\u0441\u043b\u0438 \u043e\u043d \u0432 \u043f\u043e\u043a\u0435\u0440-\u0440\u0443\u043c\u0435, \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0431\u0435\u0440\u0435\u0442\u0441\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \u043f\u043e\u043a\u0435\u0440\u0430, \u0435\u0441\u043b\u0438 \u0437\u0430 \u0448\u0430\u0445\u043c\u0430\u0442\u043d\u044b\u043c \u0441\u0442\u043e\u043b\u043e\u043c \u2014 \u043b\u043e\u0433\u0438\u043a\u0430 \u0448\u0430\u0445\u043c\u0430\u0442.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/544\/6e3\/6d9\/5446e36d904b7428db636734e08db466.png\" alt=\"\u0412\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0438\u0437 \u043b\u043e\u0433\u0438\u043a\u0438 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0430 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u043e\u0432 (Application) \u043a\u0430\u043a \u0447\u0430\u0441\u0442\u044c \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b\" title=\"\u0412\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0438\u0437 \u043b\u043e\u0433\u0438\u043a\u0438 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0430 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u043e\u0432 (Application) \u043a\u0430\u043a \u0447\u0430\u0441\u0442\u044c \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b\" width=\"262\" height=\"245\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/544\/6e3\/6d9\/5446e36d904b7428db636734e08db466.png\"\/><figcaption>\u0412\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0438\u0437 \u043b\u043e\u0433\u0438\u043a\u0438 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0430 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u043e\u0432 (Application) \u043a\u0430\u043a \u0447\u0430\u0441\u0442\u044c \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b<\/figcaption><\/figure>\n<p>\u041d\u0430\u043b\u0438\u0446\u043e \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043b\u043e\u0433\u0438\u043a\u0438 \u043d\u0430 \u0434\u0432\u0430 \u0442\u0438\u043f\u0430 \u043a\u043b\u0430\u0441\u0441\u043e\u0432: \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440 (<code>ComboLogic<\/code>) \u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043b\u043e\u0433\u0438\u043a\u0438 (<code>ChessLogic<\/code>, <code>CheckersLogic<\/code>). \u0423 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 \u0438\u0437 \u043e\u0431\u0449\u0435\u0433\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0441 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u043e\u0439 \u043c\u0435\u0442\u043e\u0434\u0430 <code>handle_commands()<\/code>. \u0410 \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440 \u0432\u0441\u0435\u0433\u0434\u0430 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0438 \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f. \u0424\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u043d \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u043b\u0441\u044f \u0432 \u0434\u0432\u0438\u0436\u043e\u043a \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0435\u0433\u043e \u0443\u043c\u0435\u0441\u0442\u043d\u0435\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u0442\u044c \u0432 <code>Engine<\/code>, \u0438\u043b\u0438 \u043b\u0443\u0447\u0448\u0435 \u2014 \u0432 <code>Application<\/code>. \u0410 \u043b\u043e\u0433\u0438\u043a\u0443 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0438\u0433\u0440 \u0442\u043e\u0433\u0434\u0430 \u2014 \u0432 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b:<\/p>\n<pre><code class=\"python\">class Application:     def __init__(self, default_controller, controller_by_key=None) -> None:         self.default_controller = default_controller         self.controller_by_key = controller_by_key or {}         self.storage = {}  # App state      async def on_connect(self, index, result):         if self.default_controller:           self.default_controller.on_connect(storage, index, result)      async def on_disconnect(self, index, result):         if self.default_controller:           self.default_controller.on_disconnect(storage, index, result)      async def handle_commands(self, index, commands):         result = []         # Handle         for command in commands:             key = command.get(\"key\")             controller = self.controller_by_key.get(key, self.default_controller)             if controller:                 await controller.handle_command(self.storage, index, command, result)         return result  class MyController: # To be able to send commands to all current connections     async def on_connect(self, storage, index, result):         indexes = storage.get(\"indexes\")         if indexes is None:             storage[\"indexes\"] = [index]         else:             indexes.append(index)      async def on_disconnect(self, storage, index, result):         indexes = storage.get(\"indexes\")         if indexes and index in indexes:          indexes.remove(index)      async def handle_command(self, storage, index, command, result):     ...  class ChessController:     async def handle_command(self, storage, index, command, result):     ...  class CheckersController:     async def handle_command(self, storage, index, command, result):     ...  HOST, PORT = \"\", 5000 if __name__ == \"__main__\":     controller_by_key = {       \"chess\": ChessLogic(),       \"checkers\": CheckersLogic(),     }     app = Application(MyController(), controller_by_key)     server = SocketServer(JSONParser(), app, HOST, PORT)     server.run()<\/code><\/pre>\n<p>\u0421\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 <code>on_connect()<\/code> \u0438 <code>on_disconnect()<\/code> \u0431\u044b\u043b\u043e \u0432\u044b\u043d\u0435\u0441\u0435\u043d\u043e \u0438\u0437 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0432 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e, \u0442\u0430\u043a \u043a\u0430\u043a \u0434\u0430\u043d\u043d\u0430\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \u0432\u0435\u0441\u044c\u043c\u0430 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0438 \u043c\u043e\u0436\u0435\u0442 \u043c\u0435\u043d\u044f\u0442\u044c\u0441\u044f \u043e\u0442 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043a \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044e. \u041c\u044b \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043a\u043b\u0430\u0441\u0441 <code>Application<\/code>.<\/p>\n<p>\u041e\u0442\u043c\u0435\u0442\u0438\u043c \u0442\u0430\u043a\u0436\u0435, \u0447\u0442\u043e \u0432 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430\u0445 \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0435 <code>handle_commands()<\/code>, \u0430 <code>handle_command()<\/code>. \u0422\u043e \u0435\u0441\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043f\u043e \u043e\u0434\u043d\u043e\u0439. \u042d\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u0435\u0435, \u0442\u0430\u043a \u043a\u0430\u043a \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0434\u0435\u043b\u0430\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0432 \u0446\u0438\u043a\u043b\u0435. \u0418 \u0433\u043b\u0430\u0432\u043d\u043e\u0435 \u2014 \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440 \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043f\u043e \u043e\u0434\u043d\u043e\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u0435, \u0442\u0430\u043a \u043a\u0430\u043a \u043b\u044e\u0431\u0430\u044f \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 \u043c\u043e\u0436\u0435\u0442 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u0435\u0433\u043e \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430.<\/p>\n<p>\u0415\u0449\u0435, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 return, \u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u0432\u0438\u0434\u0435 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u0432 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430\u0445. \u042d\u0442\u043e \u0442\u043e\u0436\u0435 \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432.<\/p>\n<p>\u0418 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435. \u0422\u0430\u043a \u043a\u0430\u043a \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b \u2014 \u044d\u0442\u043e \u0432 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u0447\u0430\u0441\u0442\u044c \u043e\u0431\u0449\u0435\u0439 \u043b\u043e\u0433\u0438\u043a\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0442\u043e \u0432\u0441\u0435 \u043e\u043d\u0438 \u0434\u043e\u043b\u0436\u043d\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u043d\u043e \u043e\u0431\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (<code>storage<\/code>). \u0421\u0430\u043c\u0438 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043d\u0435 \u0438\u043c\u0435\u044e\u0442 \u0438 \u0438\u043c\u0435\u0442\u044c \u043d\u0435 \u043c\u043e\u0433\u0443\u0442. \u041e\u043d\u0438 \u2014 \u043b\u043e\u0433\u0438\u043a\u0430 \u0432 \u0441\u0430\u043c\u043e\u043c \u0447\u0438\u0441\u0442\u043e\u043c \u0432\u0438\u0434\u0435. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 <code>handle_command()<\/code> \u0441\u0440\u0435\u0434\u0438 \u043f\u0440\u043e\u0447\u0438\u0445 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0438 \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<p><a class=\"anchor\" name=\"section5\" id=\"section5\"><\/a><\/p>\n<h2>\u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435<\/h2>\n<p>\u0421\u043a\u0430\u0436\u0435\u043c \u043d\u0430\u043f\u043e\u0441\u043b\u0435\u0434\u043e\u043a \u043f\u0430\u0440\u0443 \u0441\u043b\u043e\u0432 \u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u0438 \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u043e\u043d\u043e \u0443 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0442\u0430\u043a\u0438\u043c, \u043a\u0430\u043a\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c. <\/p>\n<p>\u0412 \u041e\u041e\u041f \u043f\u043e\u0434\u0445\u043e\u0434\u0435 \u0432\u0441\u0435 \u0441\u0430\u043c\u043e \u0441\u043e\u0431\u043e\u0439 \u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0442\u0430\u043a, \u0447\u0442\u043e \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0441 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438 \u0438 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438. \u0412 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u0445 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438, \u0430 \u0432 \u043c\u0435\u0442\u043e\u0434\u0430\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044d\u0442\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0438\u0437\u043c\u0435\u043d\u044f\u044e\u0442.<\/p>\n<p>\u041f\u0440\u0438 \u0442\u0430\u043a\u043e\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0435 \u0432\u0441\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0440\u0430\u0437\u043c\u0430\u0437\u0430\u043d\u043e \u0442\u043e\u043d\u043a\u0438\u043c \u0441\u043b\u043e\u0435\u043c \u043f\u043e \u0434\u0435\u0441\u044f\u0442\u043a\u0430\u043c \u0438 \u0441\u043e\u0442\u043d\u044f\u043c \u0442\u0430\u043a\u0438\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432. \u0427\u0442\u043e\u0431\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0432\u0441\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0432 \u0444\u0430\u0439\u043b, \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b, \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u0432\u0441\u0435 \u0438\u0445 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u0438 \u043f\u0435\u0440\u0435\u0432\u0435\u0441\u0442\u0438 \u0432 \u043f\u0440\u043e\u0441\u0442\u044b\u0435 JSON-\u043e\u0431\u044a\u0435\u043a\u0442\u044b. \u0410 \u0447\u0442\u043e\u0431\u044b \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c, \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430, \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0432\u043e\u0441\u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u043e \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438 JSON-\u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u044e \u043d\u0430\u0448\u0438\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0443\u0436\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e, \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u043e\u0432, \u0438 \u043f\u043e\u0442\u043e\u043c \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u0435\u0433\u043e \u0441\u0432\u043e\u0439\u0441\u0442\u0432 (\u0434\u0430\u0436\u0435 \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u0445). \u0412 \u043e\u0431\u0449\u0435\u043c, \u044f\u0441\u043d\u043e, \u0447\u0442\u043e \u044d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u0438 \u043e\u0447\u0435\u043d\u044c \u0441\u043b\u043e\u0436\u043d\u043e \u0438 \u043c\u0443\u0442\u043e\u0440\u043d\u043e.<\/p>\n<p>\u0422\u0443\u0442 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u044f\u0432\u0438\u0442\u044c\u0441\u044f \u0438\u0434\u0435\u044f, \u0447\u0442\u043e \u0432\u0441\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u0435. \u0418 \u043d\u0435 \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0442\u044c \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u043a\u043e\u0433\u0434\u0430 \u0435\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c, \u0430 \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0441\u043b\u043e\u0432\u0430\u0440\u044c. \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u043c\u044b\u0441\u043b\u044c\u044e \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0432\u043e\u043f\u0440\u043e\u0441. \u0410 \u0437\u0430\u0447\u0435\u043c \u043d\u0430\u043c \u0432\u043e\u043e\u0431\u0449\u0435 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u0442\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u2014 \u0438\u0445 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u044e \u0438 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435? \u041f\u043e\u0447\u0435\u043c\u0443 \u043d\u0435 \u043e\u043f\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u0447\u0438\u0441\u0442\u043e\u0439 JSON-\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439? \u0422\u043e\u0433\u0434\u0430 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043d\u0438\u043a\u0430\u043a\u0438\u0435 \u043d\u0443\u0436\u043d\u044b \u043d\u0435 \u0431\u0443\u0434\u0443\u0442, \u0430 \u0431\u0443\u0434\u0443\u0442 \u043e\u0434\u043d\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u041f\u0440\u043e\u0441\u0442\u043e\u0439 \u043d\u0430\u0431\u043e\u0440 \u0444\u0443\u043d\u043a\u0446\u0438\u0439.<\/p>\n<p>\u041f\u043e \u0441\u0447\u0430\u0441\u0442\u044c\u044e, Python \u043c\u0443\u043b\u044c\u0442\u0438\u043f\u0430\u0440\u0430\u0434\u0438\u0433\u043c\u0435\u043d\u043d\u044b\u0439 \u044f\u0437\u044b\u043a \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u0438 \u043d\u0430 \u043d\u0435\u043c \u043c\u043e\u0436\u043d\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u0438 \u0432 \u041e\u041e\u041f-\u0441\u0442\u0438\u043b\u0435, \u0438 \u0432 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u043d\u043e\u043c, \u0438 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u043c. \u041c\u044b \u043d\u0430\u0447\u0430\u043b\u0438 \u0441 \u0441\u0430\u043c\u043e\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u2014 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u043d\u043e\u0439. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0443 \u043d\u0430\u0441 \u0431\u044b\u043b\u043e \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u0441\u043b\u043e\u0432\u0430\u0440\u0435, \u043e\u0431\u0449\u0435\u043c \u043d\u0430 \u0432\u0441\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435.<\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 \u043c\u044b \u043f\u0435\u0440\u0435\u0448\u043b\u0438 \u043a \u041e\u041e\u041f, \u043c\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0446\u0435\u043d\u0442\u0440\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f. \u041c\u044b \u043d\u0435 \u0441\u0442\u0430\u043b\u0438 \u0435\u0433\u043e \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043f\u043e \u043a\u043b\u0430\u0441\u0441\u0430\u043c, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432 \u044d\u0442\u043e\u043c \u043d\u0435 \u0431\u044b\u043b\u043e \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u0441\u043c\u044b\u0441\u043b\u0430. \u041a\u043b\u0430\u0441\u0441\u044b \u043c\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u043b\u0438 \u043b\u0438\u0448\u044c \u0434\u043b\u044f \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u0434\u043c\u0435\u043d\u044f\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0438\u0437 \u043d\u0438\u0445 \u0432 \u043f\u043e\u0434\u043a\u043b\u0430\u0441\u0441\u0430\u0445 (\u0441\u043c. \u043f\u0430\u0442\u0442\u0435\u0440\u043d <a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%A8%D0%B0%D0%B1%D0%BB%D0%BE%D0%BD%D0%BD%D1%8B%D0%B9_%D0%BC%D0%B5%D1%82%D0%BE%D0%B4_(%D1%88%D0%B0%D0%B1%D0%BB%D0%BE%D0%BD_%D0%BF%D1%80%D0%BE%D0%B5%D0%BA%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D1%8F)\" rel=\"noopener noreferrer nofollow\">\u0448\u0430\u0431\u043b\u043e\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434<\/a>). (\u0415\u0441\u043b\u0438 \u0436\u0435 \u043c\u044b \u0441\u043c\u043e\u0436\u0435\u043c \u0442\u0430\u043a \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b, \u0447\u0442\u043e\u0431\u044b \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0435 \u043c\u0435\u043d\u044f\u0442\u044c \u0432 \u043d\u0438\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u0442\u043e \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u2014 \u043a \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u043c\u0443 \u0441\u0442\u0438\u043b\u044e.)<\/p>\n<p>\u041f\u043e\u0432\u0435\u0437\u043b\u043e \u043d\u0430\u043c \u0441 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c? \u041d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c. \u0412\u0441\u0435 \u0434\u0435\u043b\u043e \u0432 \u043c\u0435\u0442\u043e\u0434\u0438\u043a\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438. \u0412\u0441\u0435\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u0441 \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438, \u0430 \u043f\u043e\u0442\u043e\u043c \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0432 \u043d\u0435\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e, \u0431\u0435\u0437 \u0447\u0435\u0433\u043e \u043d\u0435\u043b\u044c\u0437\u044f \u043e\u0431\u043e\u0439\u0442\u0438\u0441\u044c. \u0422\u043e\u0433\u0434\u0430 \u043f\u0440\u043e \u043c\u043d\u043e\u0433\u0438\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0432\u044b \u0434\u0430\u0436\u0435 \u0438 \u043d\u0435 \u0443\u0437\u043d\u0430\u0435\u0442\u0435, \u0447\u0442\u043e \u043e\u043d\u0438 \u0431\u044b\u0432\u0430\u044e\u0442.<\/p>\n<p><a class=\"anchor\" name=\"section6\" id=\"section6\"><\/a><\/p>\n<h2>\u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439<\/h2>\n<p>\u0412 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u0445 \u043a\u043e\u043c\u0430\u043d\u0434 \u043f\u043e\u043c\u0438\u043c\u043e \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c, \u0442\u0430\u043a\u0436\u0435 \u043e\u0431\u044b\u0447\u043d\u043e \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432 \u044d\u0442\u043e\u043c \u0443\u0447\u0430\u0441\u0442\u0432\u0443\u044e\u0442. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0447\u0442\u043e\u0431\u044b \u043f\u0435\u0440\u0435\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u043d\u0430 \u0438\u0433\u0440\u043e\u0432\u043e\u043c \u043f\u043e\u043b\u0435, \u043d\u0443\u0436\u043d\u043e \u044f\u0432\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u0447\u0442\u043e \u0432\u044b \u0431\u0443\u0434\u0435\u0442\u0435 \u0434\u0432\u0438\u0433\u0430\u0442\u044c \u0438 \u043a\u0443\u0434\u0430. \u0415\u0441\u043b\u0438 \u043f\u043e\u043b\u0435 \u0438\u043c\u0435\u0435\u0442 \u0434\u0435\u043a\u0430\u0440\u0442\u043e\u0432\u0443 \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442, \u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043c\u043e\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u044b. \u0412 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0435 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u044b (id). \u0412\u043f\u0440\u043e\u0447\u0435\u043c \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u044b \u043c\u043e\u0436\u043d\u043e \u0447\u0430\u0441\u0442\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u0438 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e \u0441 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u0430\u043c\u0438 (\u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0438\u043b\u0438 \u0442\u043e, \u0438\u043b\u0438 \u0434\u0440\u0443\u0433\u043e\u0435).<\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u043a\u043e\u043c\u0430\u043d\u0434\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043a \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u0430. \u0422\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u044f \u0432 \u043a\u043e\u043c\u0430\u043d\u0434\u0435 \u043c\u043e\u0433\u0443\u0442 \u0438\u043c\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0444\u043e\u0440\u043c\u0430\u0442: <code>\"{id}.{property}\"<\/code>. \u0422\u0430\u043a\u0438\u043c \u0436\u0435 \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u0438 \u043a \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440: <code>\"id1.inner_id2.inner_id3\"<\/code>. <\/p>\n<p>\u0421\u0435\u0439\u0447\u0430\u0441 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u044b\u043c \u043a\u043b\u0430\u0441\u0441\u043e\u043c <code>dict<\/code>. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u043a\u043e\u0434\u0435 \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c: <code>object = storage.get(\"id1\").get(\"inner_id2\").get(\"inner_id3\")<\/code>, \u0442\u0430\u043a \u043a\u0430\u043a \u043a\u0430\u043a\u043e\u0433\u043e-\u0442\u043e \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u043e\u0436\u0435\u0442 \u0438 \u043d\u0435 \u0431\u044b\u0442\u044c, \u0438 \u0442\u043e\u0433\u0434\u0430 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0435\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435. \u041d\u043e \u0438 \u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e id \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c, \u0442\u0430\u043a \u043a\u0430\u043a \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043c\u043d\u043e\u0433\u043e &#171;\u0433\u043b\u0443\u043f\u043e\u0433\u043e&#187; \u043a\u043e\u0434\u0430. \u041c\u044b \u043d\u0435 \u0445\u043e\u0442\u0438\u043c \u0437\u0430\u0433\u0440\u043e\u043c\u043e\u0436\u0434\u0430\u0442\u044c \u043d\u0430\u0448\u0443 \u043b\u043e\u0433\u0438\u043a\u0443 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0434\u0443\u0440\u0430\u0446\u043a\u0438\u043c\u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430\u043c\u0438, \u043d\u043e \u0438 \u043d\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0438\u0445 \u0442\u043e\u0436\u0435 \u043d\u0435 \u043c\u043e\u0436\u0435\u043c. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u0438\u0434\u0435\u0430\u043b\u0435 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f: <code>object = storage.get(\"id1.inner_id2.inner_id3\")<\/code> (\u0432\u0441\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0438 \u043f\u0440\u043e\u0447\u0430\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \u0434\u043e\u043b\u0436\u043d\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0432 \u043d\u0435\u0439 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438). \u0410 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430:<\/p>\n<pre><code class=\"python\">class Storage:     def __init__(self):         self.storage = {}      def get(self, path):         return resolve_path(self.storage, path)      def set(self, path, value):         ...      def update(self, path, value):       ...      def delete(self, path):       ...  def resolve_path(target, path=None):     if not path or target is None:         return target     current = target     keys = path.split(\".\")     for key in keys:         if isinstance(current, dict):             current = current.get(key)         else:             return None     return current<\/code><\/pre>\n<p>\u0410\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e <code>get()<\/code> \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u044b \u0438 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b: <code>set()<\/code>, <code>update()<\/code>, <code>delete()<\/code>. \u0415\u0441\u043b\u0438 \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0439 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043b\u0438\u0431\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c <code>None<\/code>, \u043b\u0438\u0431\u043e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u0443\u0441\u0442\u043e\u0439 <code>dict<\/code> \u043d\u0430 \u0435\u0433\u043e \u043c\u0435\u0441\u0442\u0435. \u0422\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 <code>get()<\/code> \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e (<code>get(path, default=None)<\/code>), \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0431\u0443\u0434\u0435\u0442 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043e (<code>set()<\/code>), \u0435\u0441\u043b\u0438 <code>get(path)<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 <code>None<\/code>.<\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0433\u043e, \u043d\u0430\u043c \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043f\u0440\u043e\u0449\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0432 \u043d\u0435\u0433\u043e \u043d\u043e\u0432\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u043b\u0438 \u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0430\u0432\u0442\u043e\u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0432\u0441\u0435\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0432 \u0444\u0430\u0439\u043b, \u0447\u0442\u043e\u0431\u044b \u0438\u0433\u0440\u0443 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0438 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c \u043f\u0430\u0434\u0435\u043d\u0438\u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u0430.<\/p>\n<p>\u0412 <code>Application<\/code>, \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430, \u043b\u0438\u0448\u044c \u0431\u044b \u043e\u043d\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u0442\u043e\u0442 \u0436\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441. \u0422\u043e\u0433\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043d\u0443\u0436\u043d\u0443\u044e \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044e \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 (\u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0438 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435) \u0431\u0435\u0437 \u0432\u0441\u044f\u043a\u0438\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u0441\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u043e\u0432. \u0411\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0434\u0430\u0436\u0435 \u043f\u043e\u0434\u043e\u0437\u0440\u0435\u0432\u0430\u0442\u044c \u043e\u0442\u043a\u0443\u0434\u0430 \u0431\u0435\u0440\u0443\u0442\u0441\u044f \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044e\u0442\u0441\u044f \u043b\u0438 \u043e\u043d\u0438 \u0432 \u0411\u0414 \u0438\u043b\u0438 \u043d\u0435\u0442. \u042d\u0442\u043e \u043d\u0435 \u0438\u0445 \u0437\u0430\u0431\u043e\u0442\u0430.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/9c0\/309\/8e6\/9c03098e6c31ad51e4c2f4e499214ae4.png\" alt=\"\u0412\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430 (Repository) \u043a\u0430\u043a \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u044f \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b\" title=\"\u0412\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430 (Repository) \u043a\u0430\u043a \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u044f \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b\" width=\"386\" height=\"240\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/9c0\/309\/8e6\/9c03098e6c31ad51e4c2f4e499214ae4.png\"\/><figcaption>\u0412\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430 (Repository) \u043a\u0430\u043a \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u044f \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b<\/figcaption><\/figure>\n<p>\u0412 <a href=\"https:\/\/en.wikipedia.org\/wiki\/Domain-driven_design\" rel=\"noopener noreferrer nofollow\">DDD<\/a> \u0442\u0430\u043a\u043e\u0439 \u0444\u0430\u0441\u0430\u0434 \u0434\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0434\u0430\u043d\u043d\u044b\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043a \u0442\u043e\u043c\u0443 \u0436\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0438\u0445 \u0446\u0435\u043b\u043e\u0441\u0442\u043d\u043e\u0441\u0442\u044c \u0438 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c, \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u043c (Repository). \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u0432\u043f\u043e\u043b\u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438 \u044d\u0442\u043e \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0432\u043c\u0435\u0441\u0442\u043e Storage.<\/p>\n<p>\u0421\u044e\u0434\u0430 \u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0427\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u0443\u0442\u0430\u0442\u044c \u0438\u0445 \u0441 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0434\u043b\u044f \u043d\u0438\u0445 \u043c\u0435\u0442\u043e\u0434: <code>getconfig(path)<\/code>. \u0412\u0441\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435 (\u043b\u0443\u0447\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0444\u043e\u0440\u043c\u0430\u0442 YML) \u0438 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u043f\u0440\u0438 \u0435\u0433\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0435. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u0435\u0442\u043e\u0434\u044b <code>setconfig()<\/code> \u0438 <code>updateconfig()<\/code> \u043d\u0435 \u043d\u0443\u0436\u043d\u044b.<\/p>\n<p>\u041d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u0445, \u043e\u0442\u043a\u0443\u0434\u0430 \u043e\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f. \u041d\u0430 \u044d\u0442\u043e\u0442 \u0441\u043b\u0443\u0447\u0430\u0439 \u043c\u043e\u0436\u043d\u043e \u0442\u0430\u043a\u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434:<\/p>\n<pre><code class=\"python\">class Repository:     # ...     def create(self, config_path=None, initial=None):         if self.state is None:             return None         # Get initial         config = self.getconfig(config_path, {}) if config_path else {}         initial = {**config, **initial}         id = initial.get(\"id\")         if id is None:             # Generate id             ...         # Set         return self.set(id, initial)<\/code><\/pre>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u0435\u0442 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0430\u0440\u0438\u0430\u0446\u0438\u0439 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430, \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0438\u043c\u0435\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u0434\u0430\u0442\u044c \u043e\u0434\u043d\u0443 \u0438\u0437 \u043d\u0438\u0445 \u043f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"python\">class Application:     def __init__(self, default_controller, controller_by_key=None, storage=None) -> None:         self.default_controller = default_controller         self.controller_by_key = controller_by_key or {}         self.storage = storage if storage else Repository()  # App state  HOST, PORT = \"\", 5000 if __name__ == \"__main__\":     controller_by_key = {         \"chess\": ChessLogic(),         \"checkers\": CheckersLogic(),     }     app = Application(MyController(), controller_by_key, Repository())     server = SocketServer(JSONParser(), app, HOST, PORT)     server.run()<\/code><\/pre>\n<p><a class=\"anchor\" name=\"section7\" id=\"section7\"><\/a><\/p>\n<h2>HTTP-\u0441\u0435\u0440\u0432\u0435\u0440<\/h2>\n<p>\u0411\u0435\u0437 \u0441\u043e\u043a\u0435\u0442-\u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043d\u0435 \u043e\u0431\u043e\u0439\u0442\u0438\u0441\u044c, \u0435\u0441\u043b\u0438 \u0438\u0433\u0440\u0430 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0430\u044f, \u0438 \u043d\u0443\u0436\u043d\u043e \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u043e\u043f\u043e\u0432\u0435\u0449\u0430\u0442\u044c \u0432\u0441\u0435\u0445 \u0443\u0447\u0430\u0441\u0442\u043d\u0438\u043a\u043e\u0432 \u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u044f\u0445. \u041d\u043e \u0435\u0441\u043b\u0438 \u043c\u044b \u0441\u0442\u0440\u043e\u0438\u043c \u0444\u0435\u0440\u043c\u0443 \u0438\u043b\u0438 \u043d\u0430\u0440\u044f\u0436\u0430\u0435\u043c \u0451\u043b\u043e\u0447\u043a\u0443, \u0442\u043e \u043d\u0430\u043c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e HTTP-\u0441\u0435\u0440\u0432\u0435\u0440\u0430, \u0432\u0435\u0434\u044c \u0432\u0441\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u043c\u044b \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0441\u0430\u043c\u0438 \u043d\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0435. \u0414\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0447\u0442\u043e-\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0441\u0430\u043c\u043e \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435, \u0442\u043e \u044d\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u0438\u0440\u0443\u0435\u043c\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c. \u0410 \u0437\u043d\u0430\u0447\u0438\u0442, \u0432 \u043e\u0434\u043d\u043e\u043c \u0438\u0437 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0441\u0435\u0440\u0432\u0435\u0440 \u0434\u0430\u0441\u0442 \u0437\u043d\u0430\u0442\u044c \u043a\u043b\u0438\u0435\u043d\u0442\u0443, \u0432 \u043a\u0430\u043a\u043e\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043d\u0435 \u0441\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u043b\u0438 \u0447\u0435\u0433\u043e. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0438\u043b\u044c\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0441\u044f \u043d\u0430\u0448\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 HTTP-\u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430.<\/p>\n<p>HTTP-\u0441\u0435\u0440\u0432\u0435\u0440 \u2014 \u044d\u0442\u043e \u0442\u043e\u0436\u0435 \u0441\u043e\u043a\u0435\u0442-\u0441\u0435\u0440\u0432\u0435\u0440, \u043d\u043e \u0441 \u0442\u0435\u043c \u043e\u0442\u043b\u0438\u0447\u0438\u0435\u043c, \u0447\u0442\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u0441\u043b\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0436\u0435 \u043e\u0442\u0432\u0435\u0442\u043d\u043e\u0433\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f. \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0435\u0433\u043e \u0440\u0430\u0431\u043e\u0442\u044b \u0442\u0430\u043a\u043e\u0439: \u0441\u043e\u043a\u0435\u0442-\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u0437\u0430\u043f\u0440\u043e\u0441 \u043e\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0430, \u043e\u043d \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u0438 \u043e\u0442\u0441\u044b\u043b\u0430\u0435\u0442\u0441\u044f \u043e\u0442\u0432\u0435\u0442\u043d\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043d\u0430 \u043a\u043b\u0438\u0435\u043d\u0442. \u0412 \u043a\u043e\u043d\u0446\u0435 \u043a\u0430\u0436\u0434\u043e\u0439 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0442\u0443\u0442 \u0436\u0435 \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f.<\/p>\n<p>\u0418\u0437\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043b\u043e\u0433\u0438\u043a\u0438 \u043e\u0442 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043b\u043e\u0433\u0438\u043a\u0443 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0441 \u043b\u044e\u0431\u044b\u043c \u0442\u0438\u043f\u043e\u043c \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u0432 \u0431\u0435\u0437 \u0432\u0441\u044f\u043a\u0438\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u0432 \u043a\u043b\u0430\u0441\u0441\u0430\u0445 \u043b\u043e\u0433\u0438\u043a\u0438. \u0415\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435, \u0447\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043c\u0435\u043d\u044f\u0442\u044c\u0441\u044f \u2014 \u044d\u0442\u043e \u043a\u043e\u0435-\u043a\u0430\u043a\u0438\u0435 \u0441\u043b\u043e\u0438 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b. \u0412 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u043f\u0435\u0440\u0432\u044b\u043c \u0434\u0435\u043b\u043e\u043c \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0441\u043f\u043e\u0441\u043e\u0431 \u0442\u0440\u0430\u043d\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u2014 \u0441\u043b\u043e\u0439 Server. <\/p>\n<p>HTTP \u2014 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u0438 \u0448\u0438\u0440\u043e\u043a\u043e \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0439. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439 \u0442\u0430\u043a\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u0432, \u0432 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u0438 \u043d\u0430 Python: Django, Twisted, Tornado. \u041c\u044b \u0432\u044b\u0431\u0435\u0440\u0435\u043c \u043e\u0434\u0438\u043d \u0438\u0437 \u0441\u0430\u043c\u044b\u0445 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u043d\u044b\u0445 \u2014 Flask. \u041f\u0440\u0438\u043d\u0446\u0438\u043f \u0435\u0433\u043e \u0440\u0430\u0431\u043e\u0442\u044b \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0438\u043b\u043b\u044e\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u043c:<\/p>\n<pre><code class=\"python\">import json from flask import Flask, send_file, request  app = Flask(__name__)  def handle(request):     return {}  @app.route(\"\/storage\/&lt;key>\") def storage(key):     response = handle(request)     return response<\/code><\/pre>\n<p>\u0413\u043b\u0430\u0432\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u043e\u0432 \u2014 \u044d\u0442\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u044b\u0445 HTTP-\u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0432 \u043e\u0431\u044a\u0435\u043a\u0442\u044b request \u0438 response. \u0427\u0442\u043e\u0431\u044b \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0432 \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u044b\u0435 \u043d\u0430\u043c \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 <code>FlaskParser<\/code>. \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043a\u043b\u0438\u0435\u043d\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 RESTful API (\u044d\u0442\u043e \u043a\u043e\u0433\u0434\u0430 \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u2014 \u0432\u0437\u044f\u0442\u044c, \u0437\u0430\u0434\u0430\u0442\u044c, \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u2014 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u043c: get, post, patch):<\/p>\n<pre><code class=\"python\">class FlaskParser(Parser):     command_by_alias = {         \"GET\": \"get\",         \"POST\": \"save\",         \"PATCH\": \"update\",     }      def parse(self, request):         # Parse         values = request.values         data_str = values.get(\"data\")         data = json.loads(data_str) if data_str else None         if data is None:             data = {}         # Prepare command         code = data.get(\"code\")         if not code:             code = values.get(\"_method\") or request.method         data[\"code\"] = self.command_by_alias.get(code, code)         data[\"key\"] = request.view_args.get(\"key\")         return data, b\"\"      # No real serialization needed here     def serialize(self, command):         return command<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 HTTP-\u0441\u0435\u0440\u0432\u0435\u0440 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0437\u0430\u0447\u0430\u0441\u0442\u0443\u044e \u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u0441\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0442\u044c \u043e\u0431\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u0442\u043e \u043d\u0430\u043c \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439, \u0445\u0440\u0430\u043d\u044f\u0449\u0438\u0439 \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0435 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438, \u0430 \u0432 \u043a\u0430\u043a\u043e\u0439-\u043d\u0438\u0431\u0443\u0434\u044c \u0411\u0414.<\/p>\n<p>\u0418\u0442\u043e\u0433\u043e, \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0432\u0441\u0435\u0433\u043e \u0442\u0440\u0438 \u0441\u043b\u043e\u044f: \u0441\u0435\u0440\u0432\u0435\u0440, \u043f\u0430\u0440\u0441\u0435\u0440 \u0438 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439. \u0421 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u043c \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u043e\u0432\u043e\u0433\u043e \u2014 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0441 \u0411\u0414 \u0447\u0430\u0441\u0442\u043e \u0431\u044b\u0432\u0430\u0435\u0442 \u043d\u0443\u0436\u043d\u043e \u0438 \u0432 \u0441\u043e\u043a\u0435\u0442-\u0441\u0435\u0440\u0432\u0435\u0440\u0430\u0445. \u041a\u043b\u0430\u0441\u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043c\u043e\u0436\u043d\u043e \u0442\u0430\u043a\u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0430\u0440\u044b\u0439, \u0442\u0430\u043a \u043a\u0430\u043a \u0442\u0430\u043c \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u043c\u0435\u0442\u043e\u0434 <code>handle_bytes()<\/code>. \u0414\u043b\u044f \u0443\u043d\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u0442\u044c \u0432 \u0431\u043e\u043b\u0435\u0435 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0439 <code>handle_requests()<\/code> \u0438 \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u0432\u0435\u0441\u044c \u043a\u043e\u0434, \u043a\u0440\u043e\u043c\u0435 <code>handle_connection()<\/code> \u0432 \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 <code>Server<\/code>. \u0412 \u043a\u043e\u043d\u0446\u0435 \u043a\u043e\u043d\u0446\u043e\u0432 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u043c \u0434\u043b\u044f HTTP-\u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043a\u043b\u0430\u0441\u0441\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0430\u0440\u0441\u0435\u0440:<\/p>\n<pre><code class=\"python\">import json from flask import Flask, send_file, request  app = Flask(__name__) controller_by_key = {     \"chess\": ChessLogic(),     \"checkers\": CheckersLogic(), } application = Application(MyController(), controller_by_key, DBRepository()) server = Server(FlaskParser(), application)  @app.route(\"\/storage\/&lt;key>\") async def storage(key):     return await server.handle_bytes(key, request)[0]<\/code><\/pre>\n<p>\u041e\u0434\u043d\u043e \u0438\u0437 \u0438\u0441\u043f\u044b\u0442\u0430\u043d\u0438\u0439 \u043d\u0430 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043d\u0430\u0448\u0430 \u0441\u0445\u0435\u043c\u0430 \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0432\u044b\u0434\u0435\u0440\u0436\u0430\u043b\u0430.<\/p>\n<p><a class=\"anchor\" name=\"section8\" id=\"section8\"><\/a><\/p>\n<h2>\u0412\u044b\u0432\u043e\u0434\u044b<\/h2>\n<p>\u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u0445 \u0434\u0440\u0443\u0433 \u043e\u0442 \u0434\u0440\u0443\u0433\u0430 \u0441\u043b\u043e\u0435\u0432 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043a\u043b\u0430\u0441\u0441\u044b \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437 \u043d\u0438\u0445 \u0440\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u043e\u0442 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u0441\u043b\u043e\u0435\u0432. \u0412\u0441\u0435, \u0447\u0442\u043e \u043e\u0442 \u043d\u0438\u0445 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u2014 \u044d\u0442\u043e \u0434\u0435\u0440\u0436\u0430\u0442\u044c\u0441\u044f \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u043d\u0438\u0445 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432. \u0415\u0441\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b \u043e\u0441\u0442\u0430\u044e\u0442\u0441\u044f \u043d\u0435\u043f\u0440\u0438\u043a\u043e\u0441\u043d\u043e\u0432\u0435\u043d\u043d\u044b\u043c\u0438, \u0442\u043e \u043b\u044e\u0431\u044b\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u043b\u043e\u044f \u043d\u0438\u043a\u0430\u043a \u043d\u0435 \u043e\u0442\u0440\u0430\u0437\u044f\u0442\u0441\u044f \u043d\u0430 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445. \u0412 \u044d\u0442\u043e\u043c \u0438 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432\u0441\u044f \u043f\u0440\u0435\u043b\u0435\u0441\u0442\u044c \u0441\u043b\u043e\u0438\u0441\u0442\u043e\u0439 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c55\/414\/4c9\/c554144c956de30f23e83b599bd12e42.png\" alt=\"\u0427\u0435\u0442\u044b\u0440\u0435 \u0441\u043b\u043e\u044f \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0438 \u0438\u0445 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435 \u043a \u043b\u043e\u0433\u0438\u043a\u0435 (Controller)\" title=\"\u0427\u0435\u0442\u044b\u0440\u0435 \u0441\u043b\u043e\u044f \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0438 \u0438\u0445 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435 \u043a \u043b\u043e\u0433\u0438\u043a\u0435 (Controller)\" width=\"617\" height=\"399\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c55\/414\/4c9\/c554144c956de30f23e83b599bd12e42.png\"\/><figcaption>\u0427\u0435\u0442\u044b\u0440\u0435 \u0441\u043b\u043e\u044f \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0438 \u0438\u0445 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435 \u043a \u043b\u043e\u0433\u0438\u043a\u0435 (Controller)<\/figcaption><\/figure>\n<p>\u041f\u0440\u0438 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0438 \u0446\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430 \u0441\u043b\u043e\u0438 \u043c\u044b \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u0434\u0435\u043b\u0438\u043b\u0438 \u0434\u0432\u0435 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0447\u0430\u0441\u0442\u0438: \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0438 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0443. \u041f\u0435\u0440\u0432\u0430\u044f \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0438 \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u043b\u0430\u0441\u044c \u043d\u0430 Server \u0438 Parser. \u0410 \u0438\u0437 \u0432\u0442\u043e\u0440\u043e\u0439 \u043e\u0442\u0434\u0435\u043b\u0438\u043b\u0438\u0441\u044c \u0435\u0449\u0435 \u0434\u0432\u0430 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043d\u044b\u0445 \u0441\u043b\u043e\u044f: Application \u0438 Repository. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043f\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0446\u0435\u043f\u043e\u0447\u043a\u0435:<\/p>\n<blockquote>\n<p>Server \u2192 Parser \u2192 Application \u2192 Controller \u2192 Repository<\/p>\n<\/blockquote>\n<p>\u0418\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u2014 \u044d\u0442\u043e \u0432\u0441\u0435 \u0442\u043e, \u0447\u0442\u043e \u043d\u0435 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043a \u043b\u043e\u0433\u0438\u043a\u0435, \u043d\u043e \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0435\u0439 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0441\u0432\u043e\u0438 \u0437\u0430\u0434\u0430\u0447\u0438. \u0411\u0443\u0434\u0443\u0447\u0438 \u043e\u0431\u0449\u0435\u0439 \u0434\u043b\u044f \u0441\u0430\u043c\u044b\u0445 \u0440\u0430\u0437\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447, \u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u043a\u043b\u0430\u0441\u0441\u043e\u0432. \u0410 \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u044d\u0442\u0438 \u043a\u043b\u0430\u0441\u0441\u044b \u0442\u0430\u043a\u0436\u0435 \u0437\u0430\u0434\u0430\u044e\u0442 \u0432\u0441\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0435\u0433\u043e \u043a\u0430\u0440\u043a\u0430\u0441, \u0442\u043e \u0435\u0433\u043e \u0441 \u043f\u043e\u043b\u043d\u044b\u043c \u043f\u0440\u0430\u0432\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0437\u0432\u0430\u0442\u044c \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u043e\u043c.<\/p>\n<p>\u041e\u0442\u0434\u0435\u043b\u0438\u0432 \u0432\u0441\u0435 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u0432\u044b\u043d\u0435\u0441\u044f \u0438\u0445 \u0432 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043d\u044b\u0439 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a, \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0443 \u0432 \u0447\u0438\u0441\u0442\u043e\u043c \u0432\u0438\u0434\u0435 \u2014 \u0432 \u0432\u0438\u0434\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u043e\u0432. \u041d\u0430\u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0438\u0441\u0442\u043e\u043c, \u0447\u0442\u043e \u043e\u043d\u0438 \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u044f\u0442 \u0434\u0430\u0436\u0435 \u043e\u0442 \u0441\u0430\u043c\u0438\u0445 \u0441\u0435\u0431\u044f (\u0442.\u0435. \u0434\u0440\u0443\u0433 \u043e\u0442 \u0434\u0440\u0443\u0433\u0430). \u041e \u0442\u043e\u043c, \u043a\u0430\u043a \u043f\u0438\u0441\u0430\u0442\u044c \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0443 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e, \u0438 \u043d\u0430 \u043a\u0430\u043a\u0438\u0435 \u0441\u043b\u043e\u0438 \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043e\u043d\u0430 \u0441\u0430\u043c\u0430, \u043c\u043e\u0436\u043d\u043e \u0443\u0437\u043d\u0430\u0442\u044c \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439, \u0437\u0430\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 <a href=\"https:\/\/habr.com\/ru\/post\/678666\/\" rel=\"noopener noreferrer nofollow\">\u0441\u0442\u0430\u0442\u044c\u0435<\/a>.<\/p>\n<p><a href=\"https:\/\/gitlab.com\/markelov-alex\/hx-py-framework-evolution\/-\/tree\/main\/f_models\/server_socket\/v3\/\" rel=\"noopener noreferrer nofollow\">\u0418\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438<\/a><\/p>\n<p><a href=\"https:\/\/habr.com\/ru\/post\/677854\/\" rel=\"noopener noreferrer nofollow\">&lt; \u041d\u0430\u0437\u0430\u0434<\/a>  |  <a href=\"https:\/\/habr.com\/ru\/post\/674754\/\" rel=\"noopener noreferrer nofollow\">\u041d\u0430\u0447\u0430\u043b\u043e<\/a>  |  <a href=\"https:\/\/habr.com\/ru\/post\/678666\/\" rel=\"noopener noreferrer nofollow\">\u0412\u043f\u0435\u0440\u0435\u0434 ><\/a><\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \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\/678658\/\"> https:\/\/habr.com\/ru\/post\/678658\/<\/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>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u043f\u043b\u0430\u043d\u044b, \u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0438\u0433\u0440\u043e\u0432\u044b\u0445 \u0436\u0430\u043d\u0440\u043e\u0432. \u041e\u0434\u043d\u0430\u043a\u043e, \u043f\u0440\u0435\u0436\u0434\u0435, \u0447\u0435\u043c \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u043a \u044d\u0442\u043e\u043c\u0443, \u043d\u0443\u0436\u043d\u043e \u0445\u043e\u0440\u043e\u0448\u0435\u043d\u044c\u043a\u043e \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u0442\u044c\u0441\u044f. \u041d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0442\u0430\u043a\u0443\u044e \u043e\u0441\u043d\u043e\u0432\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u044b \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u043b\u0430 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0433\u0440\u044b, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0442\u043e\u043c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u043d\u0438\u0447\u0435\u0433\u043e \u043f\u0435\u0440\u0435\u0434\u0435\u043b\u044b\u0432\u0430\u0442\u044c \u043d\u0430 \u043f\u043e\u043b-\u043f\u0443\u0442\u0438. \u0412 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u0438 \u0432\u0441\u0435 \u0441\u0434\u0435\u043b\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u0438\u0433\u0440\u044b. <\/p>\n<p>\u0413\u043b\u0430\u0432\u043d\u0430\u044f \u043d\u0430\u0448\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u2014 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043d\u0435\u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0435, \u0432\u044b\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0442\u0430\u043a\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0431\u044b \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u043b\u043e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0438\u0433\u0440\u043e\u0432\u044b\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043b\u044e\u0431\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 <strong>\u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e<\/strong> \u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e <strong>\u0431\u044b\u0441\u0442\u0440\u043e<\/strong>! \u0421\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u0435 \u044d\u0442\u0438\u0445 \u043d\u0435\u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b\u0445, \u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0431\u044b, \u0443\u0441\u043b\u043e\u0432\u0438\u0439 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442 \u043d\u0430\u043c \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a, \u044d\u0432\u043e\u043b\u044e\u0446\u0438\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u044b \u0438 \u043d\u0430\u043c\u0435\u0440\u0435\u043d\u044b \u0437\u0434\u0435\u0441\u044c \u043f\u0440\u043e\u0441\u043b\u0435\u0434\u0438\u0442\u044c. \u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u0443\u0434\u0435\u0442 \u043e\u043f\u0438\u0441\u0430\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043d\u043e\u0433\u043e \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430, \u0430 \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u2014 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043b\u043e\u0433\u0438\u043a\u0438 \u043d\u0430 \u0435\u0433\u043e \u043e\u0441\u043d\u043e\u0432\u0435. \u0412\u0441\u0435\u0433\u043e \u2014 \u0434\u0432\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u043d\u0430 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u043e\u043b\u043e\u0433\u0438\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0432\u0441\u0435\u0439 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u043e\u0439 \u0447\u0430\u0441\u0442\u0438.<\/p>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u044f\u0437\u044b\u043a\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u044b\u0431\u0435\u0440\u0435\u043c Python \u0437\u0430 \u0435\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u0443 \u0438 \u044d\u043b\u0435\u0433\u0430\u043d\u0442\u043d\u043e\u0441\u0442\u044c. \u041c\u044b \u043d\u0430\u0447\u043d\u0435\u043c \u0432 \u0441\u043e\u043a\u0435\u0442\u043e\u0432 (<code>asyncio<\/code>), \u0430 \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043c HTTP-\u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c. \u041d\u0430\u0448\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043a\u043e\u0434 \u043b\u043e\u0433\u0438\u043a\u0438 \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0435\u043b \u043e\u0442 \u0442\u0438\u043f\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0438 \u0437\u0430\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0441\u0435\u0442\u0435\u0432\u044b\u0445 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u043e\u0432.<\/p>\n<ol>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/post\/678658\/#section1\" rel=\"noopener noreferrer nofollow\">\u0418\u0441\u0445\u043e\u0434\u043d\u0430\u044f \u0442\u043e\u0447\u043a\u0430<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/post\/678658\/#section2\" rel=\"noopener noreferrer nofollow\">\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043b\u043e\u0433\u0438\u043a\u0438 \u0438 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/post\/678658\/#section3\" rel=\"noopener noreferrer nofollow\">\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/post\/678658\/#section4\" rel=\"noopener noreferrer nofollow\">\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043b\u043e\u0433\u0438\u043a\u0438 \u043d\u0430 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/post\/678658\/#section5\" rel=\"noopener noreferrer nofollow\">\u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/post\/678658\/#section6\" rel=\"noopener noreferrer nofollow\">\u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/post\/678658\/#section7\" rel=\"noopener noreferrer nofollow\">HTTP-\u0441\u0435\u0440\u0432\u0435\u0440<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/post\/678658\/#section8\" rel=\"noopener noreferrer nofollow\">\u0412\u044b\u0432\u043e\u0434\u044b<\/a><\/p>\n<\/li>\n<\/ol>\n<p><a class=\"anchor\" name=\"section1\" id=\"section1\"><\/a><\/p>\n<h2>\u0418\u0441\u0445\u043e\u0434\u043d\u0430\u044f \u0442\u043e\u0447\u043a\u0430<\/h2>\n<p>\u041f\u0440\u0438 \u0440\u0430\u0437\u0431\u043e\u0440\u0435 <a href=\"https:\/\/habr.com\/ru\/post\/677312\/\" rel=\"noopener noreferrer nofollow\">\u043a\u043b\u0438\u0435\u043d\u0442\u0430<\/a> \u043c\u044b \u043f\u0440\u043e\u0441\u043b\u0435\u0434\u0438\u043b\u0438, \u043a\u0430\u043a \u0438\u0437 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \u2014 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u043e\u0434\u0435\u043b\u044c, \u0430 \u043f\u043e\u0442\u043e\u043c \u0438 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440. \u041f\u043e\u0442\u043e\u043c \u043c\u044b \u0432\u0441\u0435 \u0438\u0433\u0440\u043e\u0432\u044b\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0432\u043c\u0435\u0441\u0442\u043e \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u043f\u0440\u043e\u0441\u0442\u044b\u043c\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c\u0438-\u043a\u043e\u043c\u0430\u043d\u0434\u0430\u043c\u0438. \u0422\u0430\u043a \u043a\u0430\u043a \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0438 \u043b\u043e\u0433\u0438\u043a\u0430 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0442\u0430\u043a\u0438\u043c\u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u0430\u043c\u0438, \u0442\u043e \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044e \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e, \u0433\u0434\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \u2014 \u0432 \u0442\u043e\u043c \u0436\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0438\u043b\u0438 \u0432 \u0434\u0440\u0443\u0433\u043e\u043c, \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u043d\u043e\u043c \u043d\u0430 \u0438\u043d\u043e\u0439 \u043c\u0430\u0448\u0438\u043d\u0435, \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0439 \u0437\u0430 \u0442\u044b\u0441\u044f\u0447\u0438 \u043a\u0438\u043b\u043e\u043c\u0435\u0442\u0440\u043e\u0432. \u041d\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0431\u0435\u0437 \u043b\u043e\u0433\u0438\u043a\u0438, \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0448\u043b\u044e\u0437 \u043c\u0435\u0436\u0434\u0443 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435\u043c \u0438 \u043b\u043e\u0433\u0438\u043a\u043e\u0439, \u043c\u0435\u0436\u0434\u0443 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u043c \u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c.<\/p>\n<p>\u041d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435, \u043f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0434\u043e\u0439\u0434\u0443\u0442 \u0434\u043e \u043b\u043e\u0433\u0438\u043a\u0438, \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u043c \u0438 \u043f\u0440\u0438\u043d\u044f\u0442\u044c \u043e\u0442 \u043d\u0435\u0433\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f. \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043c\u044b \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u0443\u0435\u043c \u0438\u0445 \u0432 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043a\u043e\u043c\u0430\u043d\u0434 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0438\u0445 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438 \u043d\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0432\u043e\u043a\u0440\u0443\u0433 \u043b\u043e\u0433\u0438\u043a\u0438 \u0432\u044b\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0446\u0435\u043b\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0430\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u043e\u0438\u0442 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0442\u044c. \u0427\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0432 \u043d\u0435\u0439 \u0441\u0430\u043c\u044b\u043c \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043d\u0430\u0447\u043d\u0435\u043c \u0441 \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0441\u043e\u043a\u0435\u0442-\u0441\u0435\u0440\u0432\u0435\u0440\u0430.<\/p>\n<p>\u041f\u0440\u043e \u0442\u043e, \u043a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u0441\u043e\u043a\u0435\u0442\u044b \u0432 Python, \u043c\u044b \u043f\u0438\u0441\u0430\u043b\u0438 \u0432 <a href=\"https:\/\/habr.com\/ru\/post\/676110\/\" rel=\"noopener noreferrer nofollow\">\u0434\u0440\u0443\u0433\u043e\u043c \u043c\u0435\u0441\u0442\u0435<\/a>. \u0422\u0443\u0442 \u043c\u044b \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u043c \u0441 \u0442\u043e\u0433\u043e, \u043d\u0430 \u0447\u0435\u043c \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043b\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/676144\/\" rel=\"noopener noreferrer nofollow\">\u0442\u0430\u043c<\/a> \u2014 \u0441 TCP-\u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043d\u0430 <a href=\"https:\/\/docs.python.org\/3\/library\/asyncio.html\" rel=\"noopener noreferrer nofollow\">asyncio<\/a>. \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0432 \u043d\u0435\u0433\u043e \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u0445 \u0434\u0440\u0443\u0433\u0438\u043c \u043a\u043b\u0438\u0435\u043d\u0442\u0430\u043c. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0447\u0435\u0433\u043e \u043d\u0430\u0448\u0430 \u043f\u0435\u0440\u0432\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">last_index = 0 writers = []  async def handle_connection(reader, writer):     global last_index     global writers     writers.append(writer)     last_index += 1     index = last_index     print(\"+Connected\")     unparsed_bytes = b\"\"     while True:         # Receive         try:             request_bytes = await reader.read(1024)         except ConnectionError:             break         if reader.at_eof():             break  # Disconnected by client         request_bytes = unparsed_bytes + request_bytes         request_bytes_list = request_bytes.split(b\"\\x00\")         unparsed_bytes = request_bytes_list.pop()          # Process         for request_bytes in request_bytes_list:             if not request_bytes:                 continue             request = request_bytes.decode(\"utf8\")             print(\" >> Received: {repr(request)}\")             try:                 command = json.loads(request)                 to_self_command, to_all_command = await handle_command(index, command)             except Exception as e:                 print(f\"[SERVER#{index}] Error while parsing or processing: {e}\")                 to_self_command, to_all_command = {\"error\": str(e)}, None             self_response = json.dumps(to_self_command) if to_self_command else None             all_response = json.dumps(to_all_command) if to_all_command else None             print(f\" &lt;&lt; Send: {repr(self_response)} as self_response and commands: \"                   f\"{repr(all_response)} to all {len(writers)} connections\")             if self_response:                 to_self_bytes = self_response.encode(\"utf8\") + b\"\\x00\"                 try:                     writer.write(to_self_bytes)                     await writer.drain()                 except ConnectionError:                     pass  # Yet must send to others             if all_response:                 to_all_bytes = all_response.encode(\"utf8\") + b\"\\x00\"                 for w in writers:                     try:                         w.write(to_all_bytes)                     except ConnectionError:                         continue                 await asyncio.gather(w.drain() for w in self.writers)     writers.remove(writer)     writer.close()     print(f\"-Disconnected\")  async def main(host, port):     print(f\"Start server: {host}:{port}\")     server = await asyncio.start_server(handle_connection, host, port)     async with server:         await server.serve_forever()  HOST, PORT = \"\", 5554 if __name__ == \"__main__\":     asyncio.run(main(HOST, PORT))<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0431\u044b\u043b \u043a\u043e\u0434, \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0449\u0438\u0439 \u0437\u0430 \u043f\u0435\u0440\u0435\u0441\u044b\u043b\u043a\u0443 \u0438 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u0443\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439. \u0411\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u0432 <code>handle_command()<\/code>. \u0412 \u043d\u0435\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e \u0432\u0441\u0435\u0433\u043e \u0442\u0440\u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u044b: \u0432\u0437\u044f\u0442\u044c, \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0438 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c (get, set, update). \u0422\u0430\u043a \u043a\u0430\u043a \u0432 \u043b\u043e\u0433\u0438\u043a\u0435 \u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u0431\u0430\u0437\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u043c \u0438\u043b\u0438 \u0432\u043d\u0435\u0448\u043d\u0438\u043c \u0441\u0435\u0440\u0432\u0438\u0441\u0430\u043c, \u0442\u043e, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043d\u0430 \u044d\u0442\u043e \u0432\u0440\u0435\u043c\u044f, \u0434\u0430\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0442\u0430\u043a\u0436\u0435 \u0441\u0434\u0435\u043b\u0430\u043d\u0430 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0439 (<code>async<\/code>):<\/p>\n<pre><code class=\"python\">storage = {}  async def handle_command(index, command):     global storage     key = command.get(\"key\")     code = command.get(\"code\")     if code == \"get\":         state = storage.get(key)         return {\"success\": True, **command, \"state\": state}, None     elif code == \"set\":         state = command.get(\"state\")         storage[key] = state         return {\"success\": True, **command}, None     elif code == \"update\":         index = command.get(\"index\")         value = command.get(\"value\")         if not isinstance(index, int) or not isinstance(value, int):             return {\"success\": False, **command}, None         state = storage.get(key)         if state is None:             storage[key] = state = []         if index >= len(state):             state += [0] * (index - len(state) + 1)         state[index] = value         return None, {\"success\": True, **command}     return None, None<\/code><\/pre>\n<p>\u041f\u043e\u043a\u0430 \u0447\u0442\u043e \u0432\u0441\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 <code>writers<\/code>, \u0430 \u0432 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0435 \u043a\u043e\u043c\u0430\u043d\u0434 (<code>handle_command()<\/code>) \u043c\u044b \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u0435\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u0432\u0430 \u0442\u0438\u043f\u0430 \u0430\u0434\u0440\u0435\u0441\u0430\u0442\u043e\u0432 \u0434\u043b\u044f \u043e\u0442\u0432\u0435\u0442\u043d\u044b\u0445 \u043a\u043e\u043c\u0430\u043d\u0434: \u043d\u0430\u0437\u0430\u0434 \u043a \u0441\u0435\u0431\u0435 (<code>to_self_command<\/code>), \u0438 \u043a\u043e \u0432\u0441\u0435\u043c (<code>to_all_command<\/code>). \u042d\u0442\u043e \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0438 \u0432 \u043e\u0434\u043d\u043e\u043c \u0438\u0437 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u043c\u044b \u0437\u0430\u043c\u0435\u043d\u0438\u043c \u0435\u0433\u043e \u043d\u0430 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0438\u043d\u0434\u0438\u0432\u0438\u0434\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u0430\u0434\u0440\u0435\u0441\u0430\u0442\u043e\u0432 \u043f\u043e \u0438\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c. \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043a\u043e\u043c\u0430\u043d\u0434 \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0442\u0435\u0436\u0435\u0439, \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439, \u0430 \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u043a\u043e\u043c\u0430\u043d\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u043c \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0430\u044e\u0442\u0441\u044f: <code>result = [(indexes, commands), (indexes2, commands2)]<\/code>. \u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0432\u0441\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u0443\u0434\u0443\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0435 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 <code>writers<\/code>, \u043a\u0430\u043a \u0440\u0430\u043d\u044c\u0448\u0435, \u0430 \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u0435 <code>writer_by_index<\/code>. \u041d\u043e \u043f\u043e\u043a\u0430 \u0432\u0435\u0440\u043d\u0435\u043c\u0441\u044f \u043a \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<p><a class=\"anchor\" name=\"section2\" id=\"section2\"><\/a><\/p>\n<h2>\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043b\u043e\u0433\u0438\u043a\u0438 \u0438 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b<\/h2>\n<p>\u0421\u0435\u0439\u0447\u0430\u0441 \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0446\u0435\u043b\u044c\u043d\u043e\u0435 \u0438 \u043d\u0435\u0440\u0430\u0437\u0434\u0435\u043b\u0438\u043c\u043e\u0435, \u0430 \u043f\u043e\u0442\u043e\u043c\u0443 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e &#171;\u043d\u0435\u0440\u0435\u044e\u0437\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0435&#187;. \u0414\u0440\u0443\u0433\u0438\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0435\u0433\u043e \u0447\u0430\u0441\u0442\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e, \u0442\u0430\u043a \u043a\u0430\u043a \u0432\u0441\u0435 \u043e\u043d\u0438 \u0436\u0435\u0441\u0442\u043a\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u044b \u0434\u0440\u0443\u0433 \u0441 \u0434\u0440\u0443\u0433\u043e\u043c. \u0425\u043e\u0442\u044c \u043b\u043e\u0433\u0438\u043a\u0430 \u0438 \u0432\u044b\u043d\u0435\u0441\u0435\u043d\u0430 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>handle_command()<\/code>, \u043d\u043e \u0432 <code>handle_connection()<\/code> \u0432\u043c\u0435\u0441\u0442\u043e \u043d\u0435\u0435 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0434\u0440\u0443\u0433\u0443\u044e. \u0415\u0441\u043b\u0438 \u043c\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u043e\u0432\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441 \u0434\u0440\u0443\u0433\u043e\u0439 \u043b\u043e\u0433\u0438\u043a\u043e\u0439, \u0442\u043e \u043d\u0430\u043c \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 <code>handle_connection()<\/code>. \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u043d\u0430\u0441 \u044d\u0442\u043e \u043d\u0435 \u0443\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u0442.<\/p>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0441 \u043b\u043e\u0433\u0438\u043a\u043e\u0439 (<code>handle_command()<\/code>) \u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u0445 <code>handle_connection()<\/code>. \u0418\u043b\u0438 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u043e\u0431\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u043a\u043b\u0430\u0441\u0441, \u0430 \u0432 \u043f\u043e\u0434\u043a\u043b\u0430\u0441\u0441\u0430\u0445 \u0438\u0445 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c. \u041d\u043e \u043b\u0443\u0447\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u043b\u043e\u0433\u0438\u043a\u0443 (\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043a\u043e\u043c\u0430\u043d\u0434) \u0438 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 (\u043e\u0431\u043c\u0435\u043d \u043a\u043e\u043c\u0430\u043d\u0434\u0430\u043c\u0438 \u043f\u043e \u0441\u0435\u0442\u0438) \u0432\u043e\u043e\u0431\u0449\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0434\u0432\u0443\u0445 \u0440\u0430\u0437\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u0430\u0445.<\/p>\n<p>\u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u043e \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u043f\u0435\u0440\u0435\u0434 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0432 \u043d\u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0438\u0437 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445-\u0447\u043b\u0435\u043d\u043e\u0432 (\u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432), \u0430 \u0442\u0430\u043a\u0436\u0435 \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u0432 \u043f\u043e\u0434\u043a\u043b\u0430\u0441\u0441\u0430\u0445 \u043c\u0435\u0442\u043e\u0434\u044b. <\/p>\n<figure class=\"\"><figcaption>\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 (Server) \u0438 \u043b\u043e\u0433\u0438\u043a\u0443 (Logic)<\/figcaption><\/figure>\n<p>\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043d\u0430 \u0434\u0432\u0430 \u043a\u043b\u0430\u0441\u0441\u0430 \u0437\u0430\u0434\u0430\u0435\u0442 \u0434\u0432\u0430 \u043c\u0430\u0433\u0438\u0441\u0442\u0440\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0443\u0442\u0438 \u0440\u0430\u0437\u0432\u0438\u0442\u0438\u044f \u0432 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430. \u0421 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0438 \u0432\u0435\u0440\u0441\u0438\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u0430 \u043f\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 (Server), \u0441 \u0434\u0440\u0443\u0433\u043e\u0439 \u2014 \u043d\u0430\u0434\u0435\u043b\u0430\u0442\u044c \u043a\u0443\u0447\u0443 \u0440\u0430\u0437\u043d\u044b\u0445 \u0438\u0433\u0440 (Logic). \u0418 \u0432\u0441\u0435 \u044d\u0442\u043e \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e \u0434\u0440\u0443\u0433 \u043e\u0442 \u0434\u0440\u0443\u0433\u0430. \u0415\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0441\u043e\u0431\u043b\u044e\u0434\u0430\u0442\u044c\u0441\u044f, \u044d\u0442\u043e \u0447\u0442\u043e\u0431\u044b \u043a\u043b\u0430\u0441\u0441 \u043b\u043e\u0433\u0438\u043a\u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043b \u043c\u0435\u0442\u043e\u0434 \u0441 \u0442\u0430\u043a\u043e\u0439 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u043e\u0439: <code>handle_command(self, index, command)<\/code>:<\/p>\n<pre><code class=\"python\">class SocketServer:     def __init__(self, logic, host, port):         self.logic = logic         self.host = host         self.port = port         self.writers = []      def run(self):         asyncio.run(self.main())      async def main(self):         print(f\"Start server: {self.host}:{self.port}\")         server = await asyncio.start_server(handle_connection, host, port)         async with server:             await server.serve_forever()      async def handle_connection(self, reader, writer):         ...         # Call:         # to_self_command, to_all_command = await self.logic.handle_command(index, command)         # instead of:         # to_self_command, to_all_command = await handle_command(index, command)  class MyLogic:     # global storage -> self.storage     def __init__(self):       self.storage = {}      async def handle_command(self, index, command):     ...<\/code><\/pre>\n<p>\u041a\u043e\u0434 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0441\u044f:<\/p>\n<pre><code class=\"python\">HOST, PORT = \"\", 5000 if __name__ == \"__main__\":     server = SocketServer(MyLogic(), HOST, PORT)     server.run()<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0441\u0435\u0440\u0432\u0435\u0440 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u043b\u043e\u0433\u0438\u043a\u0438 \u0438 \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043a \u043d\u0435\u043c\u0443 \u0441\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0443. \u0421\u0432\u044f\u0437\u044c \u044d\u0442\u0430 \u043e\u0434\u043d\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f, \u0442\u0430\u043a \u043a\u0430\u043a \u043b\u043e\u0433\u0438\u043a\u0430 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0437\u043d\u0430\u0435\u0442 \u043e\u0431 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435.<\/p>\n<p>\u042d\u0442\u043e \u0431\u044b\u043b \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0435\u0440\u0432\u044b\u0439 \u0448\u0430\u0433. \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0431\u0443\u0434\u0435\u0442 \u2014 \u043e\u0442\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u0430<\/p>\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-336164","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/336164","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=336164"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/336164\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=336164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=336164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=336164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}