{"id":231201,"date":"2014-07-27T15:44:03","date_gmt":"2014-07-27T11:44:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=231201"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=231201","title":{"rendered":"<span class=\"post_title\">\u0421\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u0442\u043e\u0440\u043d\u0430\u0434\u043e, \u0447\u0430\u0441\u0442\u044c 2: \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438<\/span>"},"content":{"rendered":"<div class=\"content html_format\">     \t<img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/ab8\/18d\/dd9\/ab818ddd96b7b751cabf23b1b267409d.jpg\" align=\"left\" height=\"100\"\/>\u0423\u043b\u0443\u0447\u0448\u0430\u0435\u043c \u043d\u0430\u0448 <a href=\"http:\/\/habrahabr.ru\/post\/230607\/\">\u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u043a\u0430\u0440\u0442\u0438\u043d\u043e\u043a<\/a>. \u0412 \u044d\u0442\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u043c\u044b \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043e \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c \u0437\u0430\u0449\u0438\u0442\u0443 \u043e\u0442 csrf. \u0417\u0430\u0442\u0435\u043c, \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043c\u0438\u043d\u0438\u0430\u0442\u044e\u0440 \u043a\u0430\u0440\u0442\u0438\u043d\u043e\u043a, \u043d\u0430\u0443\u0447\u0438\u043c\u0441\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u043c\u0438 \u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438, \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e \u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u044e\u0449\u0438\u0435 \u0432 \u043d\u0438\u0445 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f.<a name=\"habracut\"><\/a><\/p>\n<h3>\u041a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/h3>\n<p>    \u041a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 Application \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 keyword-\u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430\u043c\u0438. \u041c\u044b \u0443\u0436\u0435 \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0438\u0441\u044c \u0441 \u044d\u0442\u0438\u043c, \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u044f <code>debug=True<\/code> \u0432\u0442\u043e\u0440\u044b\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 Application. \u041e\u0434\u043d\u0430\u043a\u043e \u0445\u0430\u0440\u0434\u043a\u043e\u0434\u0438\u0442\u044c \u0442\u0430\u043a\u0438\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043d\u0435 \u0441\u0442\u043e\u0438\u0442, \u0438\u043d\u0430\u0447\u0435 \u043a\u0430\u043a \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0441\u043a\u0440\u0438\u043f\u0442 \u043d\u0430 \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u043d\u0435, \u0433\u0434\u0435 \u044d\u0442\u043e\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c <code>False<\/code>? \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u0434\u043b\u044f django \u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u043f\u0438\u0442\u043e\u043d-\u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u043e\u0432 \u043f\u0440\u0438\u0451\u043c \u2014 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043e\u0431\u0449\u0443\u044e \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e \u0432 \u0444\u0430\u0439\u043b\u0435 <code>settings.py<\/code>, \u0432 \u043a\u043e\u043d\u0446\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c <code>settings_local.py<\/code>, \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u044f \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u043d\u044b\u0435 \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438. \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u0432\u044b \u0432\u043f\u043e\u043b\u043d\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0442\u0440\u044e\u043a, \u043e\u0434\u043d\u0430\u043a\u043e \u0432 <a href=\"http:\/\/tornado.readthedocs.org\/\">tornado<\/a> \u0435\u0441\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u043a\u043e\u043c\u0430\u043d\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f: <\/p>\n<pre><code class=\"python\">from tornado.options import define, options    define('port', default=8000, help='run on the given port', type=int)  define('db_uri', default='localhost', help='mongodb uri')  define('db_name', default='habr_tornado', help='name of database')  define('debug', default=True, help='debug mode', type=bool)    options.parse_command_line()  db = motor.MotorClient(options.db_uri)[options.db_name]    <\/code><\/pre>\n<p>   C \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>define<\/code> \u043c\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0432 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0435 <a href=\"https:\/\/docs.python.org\/3.5\/library\/optparse.html\">optparse<\/a>. \u0410 \u0437\u0430\u0442\u0435\u043c \u0432 \u043d\u0443\u0436\u043d\u043e\u043c \u043c\u0435\u0441\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438\u0445 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e options. \u0412\u044b\u0437\u044b\u0432\u0430\u044f <code>options.parse_command_line()<\/code> \u043c\u044b \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0434\u0435\u0444\u043e\u043b\u0442\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0438\u0437 \u043a\u043e\u043c\u0430\u043d\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438. \u0422\u043e \u0435\u0441\u0442\u044c \u043d\u0430 \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u043d\u0435 \u043d\u0430\u043c \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c <code>--debug=False<\/code>. \u0410 \u0437\u0430\u043f\u0443\u0441\u043a \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c <code>--help<\/code> \u043f\u043e\u043a\u0430\u0436\u0435\u0442 \u043d\u0430\u043c \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b: <\/p>\n<pre><code class=\"python\">$python3 app.py --help  Usage: app.py [OPTIONS]    Options:      --db_name                        name of database (default habr_tornado)    --db_uri                         mongodb uri (default localhost)    --debug                          debug mode (default True)    --help                           show this help information    --port                           run on the given port (default 8000)    \/home\/imbolc\/.pyenv\/versions\/3.4.0\/lib\/python3.4\/site-packages\/tornado\/log.py options:      --log_file_max_size              max size of log files before rollover                                     (default 100000000)    --log_file_num_backups           number of log files to keep (default 10)    --log_file_prefix=PATH           Path prefix for log files. Note that if you                                     are running multiple tornado processes,                                     log_file_prefix must be different for each                                     of them (e.g. include the port number)    --log_to_stderr                  Send log output to stderr (colorized if                                     possible). By default use stderr if                                     --log_file_prefix is not set and no other                                     logging is configured.    --logging=debug|info|warning|error|none                                     Set the Python log level. If 'none', tornado                                     won't touch the logging configuration.                                     (default info)  <\/code><\/pre>\n<p>   \u041a\u0430\u043a \u0432\u0438\u0434\u0438\u0442\u0435 \u0442\u043e\u0440\u043d\u0430\u0434\u043e \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. <\/p>\n<h3>CSRF<\/h3>\n<p>    \u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043a \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f <code>xsrf_cookies=True<\/code>. \u041f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0432 \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u043d\u043e\u0432\u0443\u044e \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0443, \u043c\u044b \u0443\u0432\u0438\u0434\u0438\u043c \u043e\u0448\u0438\u0431\u043a\u0443: <code>HTTP 403: Forbidden ('\\_xsrf' argument missing from POST)<\/code>. \u042d\u0442\u043e \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0430 \u0437\u0430\u0449\u0438\u0442\u0430 \u043e\u0442 <a href=\"http:\/\/en.wikipedia.org\/wiki\/Cross-site_request_forgery\">csrf<\/a>. \u0414\u043b\u044f \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432 \u0444\u043e\u0440\u043c\u0443 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c <code>{% module xsrf_form_html() %}<\/code>, \u0432 \u0445\u0442\u043c\u043b-\u043a\u043e\u0434\u0435 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u044d\u0442\u043e \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u0441\u044f \u0432\u043e \u0447\u0442\u043e-\u0442\u043e \u0442\u0438\u043f\u0430: <code>&lt;input type=&quot;hidden&quot; name=&quot;_xsrf&quot; value=&quot;2|a52d8046|a83cbd25c8b7c06e2c3ac476338982d8|1406302123&quot;\/&gt;<\/code>. <\/p>\n<h3>\u041c\u0438\u043d\u0438\u0430\u0442\u044e\u0440\u044b \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439<\/h3>\n<p>    \u041f\u0440\u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438 \u043c\u0438\u043d\u0438\u0430\u0442\u044e\u0440 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 \u043a\u0430\u0440\u0442\u0438\u043d\u043e\u043a \u043c\u044b \u0434\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u043f\u043e\u043b\u043d\u044b\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f. \u041d\u0430\u0441\u0442\u0430\u043b\u043e \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442. \u041d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f <a href=\"https:\/\/pillow.readthedocs.org\/en\/latest\/\">pillow<\/a> (\u044d\u0442\u043e \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u0444\u043e\u0440\u043a PIL \u2014 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u043c\u0438): <\/p>\n<pre><code>pip3 install pillow  <\/code><\/pre>\n<p>   \u041e\u0434\u043d\u0430\u043a\u043e, \u0442\u043e\u0440\u043d\u0430\u0434\u043e \u043e\u0434\u043d\u043e\u043f\u043e\u0442\u043e\u0447\u0435\u043d \u0438 \u0442\u0430\u043a\u0430\u044f \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0451\u043c\u043a\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043a\u0430\u043a \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439, \u0441\u0432\u0435\u0434\u0451\u0442 \u043d\u0430 \u043d\u0435\u0442 \u0432\u0441\u0435 \u043d\u0430\u0448\u0438 \u043f\u043b\u044f\u0441\u043a\u0438 \u0441 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0441\u0442\u044c\u044e. \u0421\u0430\u043c\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u2014 \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u044d\u0442\u0443 \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0442\u0440\u0435\u0434: <\/p>\n<pre><code class=\"python\">import os  import io  from concurrent.futures import ThreadPoolExecutor  from PIL import Image    class UploadHandler(web.RequestHandler):      executor = ThreadPoolExecutor(max_workers=os.cpu_count())        @gen.coroutine      def post(self):          file = self.request.files['file'][0]          try:              thumbnail = yield self.make_thumbnail(file.body)          except OSError:              raise web.HTTPError(400, 'Cannot identify image file')          orig_id, thumb_id = yield [              gridfs.put(file.body, content_type=file.content_type),              gridfs.put(thumbnail, content_type='image\/png')]          yield db.imgs.save({'orig': orig_id, 'thumb': thumb_id})          self.redirect('')        @run_on_executor      def make_thumbnail(self, content):          im = Image.open(io.BytesIO(content))          im.convert('RGB')          im.thumbnail((128, 128), Image.ANTIALIAS)          with io.BytesIO() as output:              im.save(output, 'PNG')              return output.getvalue()  <\/code><\/pre>\n<p>   \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u043f\u0443\u043b\u043b \u0432\u043e\u0440\u043a\u0435\u0440\u043e\u0432 \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435\u043c \u0438\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u044f\u0434\u0435\u0440 cpu (\u044d\u0442\u043e \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u044f \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u043e-\u0451\u043c\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u0442\u0438\u043f\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439). \u0418 \u0435\u0441\u043b\u0438 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0436\u0434\u0430\u0442\u044c \u0441\u0432\u043e\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438. \u0417\u0430\u0442\u0435\u043c \u043c\u044b \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u043c\u0438\u043d\u0438\u0430\u0442\u044e\u0440\u0443, \u0432\u044b\u0437\u044b\u0432\u0430\u044f \u043d\u0430\u0448 \u043c\u0435\u0442\u043e\u0434 <code>make_thumbnail<\/code>, \u043e\u0431\u0451\u0440\u043d\u0443\u0442\u044b\u0439 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u043e\u043c <a href=\"http:\/\/tornado.readthedocs.org\/en\/stable\/concurrent.html#tornado.concurrent.run_on_executor\">run_on_executor<\/a>, \u0447\u0442\u043e \u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432 \u043e\u0434\u043d\u043e\u043c \u0438\u0437 \u0442\u0440\u0435\u0434\u043e\u0432 executor-\u0430. <\/p>\n<p>     \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u043a\u0430\u043a \u043a\u0440\u0430\u0441\u0438\u0432\u043e \u043c\u044b \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 <code>OSError<\/code> \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0431\u0440\u043e\u0441\u0430\u0435\u0442 pillow \u0435\u0441\u043b\u0438 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0442\u044c \u0444\u043e\u0440\u043c\u0430\u0442 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f. \u041d\u0430\u043c \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u044f\u0432\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0443 \u0432 \u043e\u0442\u0432\u0435\u0442\u0435 \u043a\u0430\u043a \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043a\u043e\u043b\u0431\u044d\u0447\u043d\u043e\u0439 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0441\u0442\u0438 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432 node.js). \u041f\u0440\u043e\u0441\u0442\u043e, \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0441 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u0432 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u043c \u0441\u0442\u0438\u043b\u0435. <\/p>\n<p>     \u0414\u0430\u043b\u0435\u0435 \u043c\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0438 \u043c\u0438\u043d\u0438\u0430\u0442\u044e\u0440\u0443 \u0432 <a href=\"http:\/\/docs.mongodb.org\/manual\/core\/gridfs\/\">gridfs<\/a>. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0432\u043c\u0435\u0441\u0442\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430: <\/p>\n<pre><code class=\"python\">orig_id = yield gridfs.put(file.body, content_type=file.content_type)  thumb_id = yield gridfs.put(thumbnail, content_type='image\/png')  <\/code><\/pre>\n<p>   \u041c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0439 <code>orig_id, thumb_id = yield [ ... ]<\/code>. \u0422\u043e \u0435\u0441\u0442\u044c \u0444\u0430\u0439\u043b\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044e\u0442\u0441\u044f \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e. \u0422\u0430\u043a\u043e\u0439 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u0438\u043c\u0435\u0435\u0442 \u0441\u043c\u044b\u0441\u043b \u043f\u0440\u0438 \u043b\u044e\u0431\u044b\u0445 \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u044f\u0449\u0438\u0445 \u0434\u0440\u0443\u0433 \u043e\u0442 \u0434\u0440\u0443\u0433\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u0445. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043c\u0438\u043d\u0438\u0430\u0442\u044e\u0440\u044b \u0441 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435\u043c \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u0430, \u043d\u043e \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043c\u0438\u043d\u0438\u0430\u0442\u044e\u0440\u044b \u043d\u0435 \u0443\u0434\u0430\u0441\u0442\u0441\u044f \u0442\u0430\u043a \u043a\u0430\u043a \u0432\u0442\u043e\u0440\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u043f\u0435\u0440\u0432\u043e\u0439. <\/p>\n<p>     \u0412 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435 \u043c\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e\u0431 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438 \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e <code>imgs<\/code>. \u042d\u0442\u0430 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f \u043d\u0443\u0436\u043d\u0430, \u0447\u0442\u043e\u0431\u044b \u0441\u0432\u044f\u0437\u0430\u0442\u044c \u043c\u0438\u043d\u0438\u0430\u0442\u044e\u0440\u0443 \u0438 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f. \u0422\u0430\u043a \u0436\u0435 \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u0442\u0430\u043c \u043c\u043e\u0436\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043b\u044e\u0431\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e\u0431 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438: \u0430\u0432\u0442\u043e\u0440\u0430, \u043f\u0440\u0430\u0432\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u0438 \u0442.\u043f. \u0421 \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0435\u043c \u044d\u0442\u043e\u0439 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u0441\u044f \u0438 \u043c\u0435\u0442\u043e\u0434\u044b \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u0438 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f: <\/p>\n<pre><code class=\"python\">class UploadHandler(web.RequestHandler):      ...        @gen.coroutine      def get(self):          imgs = yield db.imgs.find().sort('_id', -1).to_list(20)          self.render('upload.html', imgs=imgs)      class ShowImageHandler(web.RequestHandler):      @gen.coroutine      def get(self, img_id, size):          try:              img_id = bson.objectid.ObjectId(img_id)          except bson.errors.InvalidId:              raise web.HTTPError(404, 'Bad ObjectId')          img = yield db.imgs.find_one(img_id)          if not img:              raise web.HTTPError(404, 'Image not found')          gridout = yield gridfs.get(img[size])          self.set_header('Content-Type', gridout.content_type)          self.set_header('Content-Length', gridout.length)          yield gridout.stream_to_handler(self)  <\/code><\/pre>\n<p>   \u041a\u0430\u043a \u0432\u0438\u0434\u0438\u0442\u0435, <code>ShowImageHandler.get<\/code> \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>size<\/code>  \u2014 \u0443\u0442\u043e\u0447\u043d\u044f\u044e\u0449\u0438\u0439 \u0445\u043e\u0442\u0438\u043c \u043b\u0438 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043c\u0438\u043d\u0438\u0430\u0442\u044e\u0440\u0443 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0438\u043b\u0438 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b. \u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0430\u0441\u044c \u0438 \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u043a\u0430 url: <\/p>\n<pre><code class=\"python\">web.url(r'\/imgs\/([\\w\\d]+)\/(orig|thumb)', ShowImageHandler,          name='show_image'),  <\/code><\/pre>\n<p>   \u0418 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u0442\u0438\u0445 url \u0432 \u0448\u0430\u0431\u043b\u043e\u043d\u0435: <\/p>\n<pre><code class=\"html\">{% for img in imgs %}      &lt;a href=&quot;{{ reverse_url('show_image', img['_id'], 'orig') }}&quot;&gt;          &lt;img src=&quot;{{ reverse_url('show_image', img['_id'], 'thumb') }}&quot;&gt;      &lt;\/a&gt;  {% end %}  <\/code><\/pre>\n<p>  <\/p>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>    \u041d\u0430 \u0441\u0435\u0433\u043e\u0434\u043d\u044f \u0432\u0441\u0451, \u043a\u043e\u0434 \u044d\u0442\u043e\u0439 \u0438 <a href=\"http:\/\/habrahabr.ru\/post\/230607\/\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439<\/a> \u0447\u0430\u0441\u0442\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u043d\u0430 <a href=\"https:\/\/github.com\/imbolc\/habr-modern-tornado\">github<\/a>.      \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/231201\/\"> http:\/\/habrahabr.ru\/post\/231201\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">     \t<img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/ab8\/18d\/dd9\/ab818ddd96b7b751cabf23b1b267409d.jpg\" align=\"left\" height=\"100\"\/>\u0423\u043b\u0443\u0447\u0448\u0430\u0435\u043c \u043d\u0430\u0448 <a href=\"http:\/\/habrahabr.ru\/post\/230607\/\">\u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u043a\u0430\u0440\u0442\u0438\u043d\u043e\u043a<\/a>. \u0412 \u044d\u0442\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u043c\u044b \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043e \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c \u0437\u0430\u0449\u0438\u0442\u0443 \u043e\u0442 csrf. \u0417\u0430\u0442\u0435\u043c, \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043c\u0438\u043d\u0438\u0430\u0442\u044e\u0440 \u043a\u0430\u0440\u0442\u0438\u043d\u043e\u043a, \u043d\u0430\u0443\u0447\u0438\u043c\u0441\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u043c\u0438 \u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438, \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e \u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u044e\u0449\u0438\u0435 \u0432 \u043d\u0438\u0445 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f.<\/p>\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-231201","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/231201","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=231201"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/231201\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=231201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=231201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=231201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}