/usr/share/doc/python-limits-doc/html/index.html is in python-flask-limiter-doc 0.9.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Flask-Limiter — Flask-Limiter unknown documentation</title>
<link rel="stylesheet" href="_static/limiter.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: 'unknown',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="shortcut icon" href="_static/tap-icon.png"/>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="top" title="Flask-Limiter unknown documentation" href="#" />
<link rel="stylesheet" href="_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head>
<body role="document">
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="flask-limiter">
<h1>Flask-Limiter<a class="headerlink" href="#flask-limiter" title="Permalink to this headline">¶</a></h1>
<div class="section" id="usage">
<h2>Usage<a class="headerlink" href="#usage" title="Permalink to this headline">¶</a></h2>
<div class="section" id="quick-start">
<h3>Quick start<a class="headerlink" href="#quick-start" title="Permalink to this headline">¶</a></h3>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">flask</span> <span class="kn">import</span> <span class="n">Flask</span>
<span class="kn">from</span> <span class="nn">flask_limiter</span> <span class="kn">import</span> <span class="n">Limiter</span>
<span class="kn">from</span> <span class="nn">flask_limiter.util</span> <span class="kn">import</span> <span class="n">get_remote_address</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>
<span class="n">limiter</span> <span class="o">=</span> <span class="n">Limiter</span><span class="p">(</span>
<span class="n">app</span><span class="p">,</span>
<span class="n">key_func</span><span class="o">=</span><span class="n">get_remote_address</span><span class="p">,</span>
<span class="n">global_limits</span><span class="o">=</span><span class="p">[</span><span class="s2">"200 per day"</span><span class="p">,</span> <span class="s2">"50 per hour"</span><span class="p">])</span>
<span class="p">)</span>
<span class="nd">@app.route</span><span class="p">(</span><span class="s2">"/slow"</span><span class="p">)</span>
<span class="nd">@limiter.limit</span><span class="p">(</span><span class="s2">"1 per day"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">slow</span><span class="p">():</span>
<span class="k">return</span> <span class="s2">"24"</span>
<span class="nd">@app.route</span><span class="p">(</span><span class="s2">"/fast"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">fast</span><span class="p">():</span>
<span class="k">return</span> <span class="s2">"42"</span>
<span class="nd">@app.route</span><span class="p">(</span><span class="s2">"/ping"</span><span class="p">)</span>
<span class="nd">@limiter.exempt</span>
<span class="k">def</span> <span class="nf">ping</span><span class="p">():</span>
<span class="k">return</span> <span class="s2">"PONG"</span>
</pre></div>
</div>
<p>The above Flask app will have the following rate limiting characteristics:</p>
<ul class="simple">
<li>Rate limiting by <cite>remote_address</cite> of the request</li>
<li>A global rate limit of 200 per day, and 50 per hour applied to all routes.</li>
<li>The <code class="docutils literal"><span class="pre">slow</span></code> route having an explicit rate limit decorator will bypass the global
rate limit and only allow 1 request per day.</li>
<li>The <code class="docutils literal"><span class="pre">ping</span></code> route will be exempt from any global rate limits.</li>
</ul>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The built in flask static files routes are also exempt from rate limits.</p>
</div>
<p>Every time a request exceeds the rate limit, the view function will not get called and instead
a <a class="reference external" href="http://tools.ietf.org/html/rfc6585#section-4">429</a> http error will be raised.</p>
</div>
<div class="section" id="the-flask-limiter-extension">
<h3>The Flask-Limiter extension<a class="headerlink" href="#the-flask-limiter-extension" title="Permalink to this headline">¶</a></h3>
<p>The extension can be initialized with the <code class="xref py py-class docutils literal"><span class="pre">flask.Flask</span></code> application
in the usual ways.</p>
<p>Using the constructor</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">flask_limiter</span> <span class="kn">import</span> <span class="n">Limiter</span>
<span class="kn">from</span> <span class="nn">flask_limiter.util</span> <span class="kn">import</span> <span class="n">get_remote_address</span>
<span class="o">....</span>
<span class="n">limiter</span> <span class="o">=</span> <span class="n">Limiter</span><span class="p">(</span><span class="n">app</span><span class="p">,</span> <span class="n">key_func</span><span class="o">=</span><span class="n">get_remote_address</span><span class="p">)</span>
</pre></div>
</div>
</div></blockquote>
<p>Using <code class="docutils literal"><span class="pre">init_app</span></code></p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">limiter</span> <span class="o">=</span> <span class="n">Limiter</span><span class="p">(</span><span class="n">key_func</span><span class="o">=</span><span class="n">get_remote_address</span><span class="p">)</span>
<span class="n">limiter</span><span class="o">.</span><span class="n">init_app</span><span class="p">(</span><span class="n">app</span><span class="p">)</span>
</pre></div>
</div>
</div></blockquote>
</div>
<div class="section" id="rate-limit-domain">
<span id="ratelimit-domain"></span><h3>Rate Limit Domain<a class="headerlink" href="#rate-limit-domain" title="Permalink to this headline">¶</a></h3>
<p>Each <a class="reference internal" href="#flask_limiter.Limiter" title="flask_limiter.Limiter"><code class="xref py py-class docutils literal"><span class="pre">Limiter</span></code></a> instance is initialized with a <cite>key_func</cite> which returns the bucket
in which each request is put into when evaluating whether it is within the rate limit or not.</p>
<div class="admonition danger">
<p class="first admonition-title">Danger</p>
<p class="last">Earlier versions of Flask-Limiter defaulted the rate limiting domain to the requesting users’ ip-address retreived via the <a class="reference internal" href="#flask_limiter.util.get_ipaddr" title="flask_limiter.util.get_ipaddr"><code class="xref py py-func docutils literal"><span class="pre">flask_limiter.util.get_ipaddr()</span></code></a> function. This behavior is being deprecated (since version <cite>0.9.2</cite>) as it can be susceptible to ip spoofing with certain environment setups (more details at <a class="reference external" href="https://github.com/alisaifee/flask-limiter/issues/41">github issue #41</a> & <a class="reference external" href="http://esd.io/blog/flask-apps-heroku-real-ip-spoofing.html">flask apps and ip spoofing</a>).</p>
</div>
<p>It is now recommended to explicitly provide a keying function as part of the <a class="reference internal" href="#flask_limiter.Limiter" title="flask_limiter.Limiter"><code class="xref py py-class docutils literal"><span class="pre">Limiter</span></code></a>
initialization (<a class="reference internal" href="#keyfunc-customization"><span class="std std-ref">Rate Limit Key Functions</span></a>). Two utility methods are still provided:</p>
<ul class="simple">
<li><a class="reference internal" href="#flask_limiter.util.get_ipaddr" title="flask_limiter.util.get_ipaddr"><code class="xref py py-func docutils literal"><span class="pre">flask_limiter.util.get_ipaddr()</span></code></a>: uses the last ip address in the <cite>X-Forwarded-For</cite> header, else falls back to the <cite>remote_address</cite> of the request</li>
<li><a class="reference internal" href="#flask_limiter.util.get_remote_address" title="flask_limiter.util.get_remote_address"><code class="xref py py-func docutils literal"><span class="pre">flask_limiter.util.get_remote_address()</span></code></a>: uses the <cite>remote_address</cite> of the request.</li>
</ul>
</div>
<div class="section" id="decorators">
<h3>Decorators<a class="headerlink" href="#decorators" title="Permalink to this headline">¶</a></h3>
<p>The decorators made available as instance methods of the <a class="reference internal" href="#flask_limiter.Limiter" title="flask_limiter.Limiter"><code class="xref py py-class docutils literal"><span class="pre">Limiter</span></code></a>
instance are</p>
<dl class="docutils" id="ratelimit-decorator-limit">
<dt><a class="reference internal" href="#flask_limiter.Limiter.limit" title="flask_limiter.Limiter.limit"><code class="xref py py-meth docutils literal"><span class="pre">Limiter.limit()</span></code></a></dt>
<dd><p class="first">There are a few ways of using this decorator depending on your preference and use-case.</p>
<dl class="last docutils">
<dt>Single decorator</dt>
<dd><p class="first">The limit string can be a single limit or a delimiter separated string</p>
<blockquote class="last">
<div><div class="highlight-python"><div class="highlight"><pre><span></span><span class="nd">@app.route</span><span class="p">(</span><span class="s2">"...."</span><span class="p">)</span>
<span class="nd">@limiter.limit</span><span class="p">(</span><span class="s2">"100/day;10/hour;1/minute"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">my_route</span><span class="p">()</span>
<span class="o">...</span>
</pre></div>
</div>
</div></blockquote>
</dd>
<dt>Multiple decorators</dt>
<dd><p class="first">The limit string can be a single limit or a delimiter separated string
or a combination of both.</p>
<blockquote class="last">
<div><div class="highlight-python"><div class="highlight"><pre><span></span><span class="nd">@app.route</span><span class="p">(</span><span class="s2">"...."</span><span class="p">)</span>
<span class="nd">@limiter.limit</span><span class="p">(</span><span class="s2">"100/day"</span><span class="p">)</span>
<span class="nd">@limiter.limit</span><span class="p">(</span><span class="s2">"10/hour"</span><span class="p">)</span>
<span class="nd">@limiter.limit</span><span class="p">(</span><span class="s2">"1/minute"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">my_route</span><span class="p">():</span>
<span class="o">...</span>
</pre></div>
</div>
</div></blockquote>
</dd>
<dt>Custom keying function</dt>
<dd><p class="first">By default rate limits are applied based on the key function that the <a class="reference internal" href="#flask_limiter.Limiter" title="flask_limiter.Limiter"><code class="xref py py-class docutils literal"><span class="pre">Limiter</span></code></a> instance
was initialized with. You can implement your own function to retrieve the key to rate limit by
when decorating individual routes. Take a look at <a class="reference internal" href="#keyfunc-customization"><span class="std std-ref">Rate Limit Key Functions</span></a> for some examples..</p>
<blockquote class="last">
<div><div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">my_key_func</span><span class="p">():</span>
<span class="o">...</span>
<span class="nd">@app.route</span><span class="p">(</span><span class="s2">"..."</span><span class="p">)</span>
<span class="nd">@limiter.limit</span><span class="p">(</span><span class="s2">"100/day"</span><span class="p">,</span> <span class="n">my_key_func</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">my_route</span><span class="p">():</span>
<span class="o">...</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The key function is called from within a
<span class="xref std std-ref">flask request context</span>.</p>
</div>
</div></blockquote>
</dd>
<dt>Dynamically loaded limit string(s)</dt>
<dd><p class="first">There may be situations where the rate limits need to be retrieved from
sources external to the code (database, remote api, etc...). This can be
achieved by providing a callable to the decorator.</p>
<blockquote class="last">
<div><div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">rate_limit_from_config</span><span class="p">():</span>
<span class="k">return</span> <span class="n">current_app</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"CUSTOM_LIMIT"</span><span class="p">,</span> <span class="s2">"10/s"</span><span class="p">)</span>
<span class="nd">@app.route</span><span class="p">(</span><span class="s2">"..."</span><span class="p">)</span>
<span class="nd">@limiter.limit</span><span class="p">(</span><span class="n">rate_limit_from_config</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">my_route</span><span class="p">():</span>
<span class="o">...</span>
</pre></div>
</div>
<div class="admonition danger">
<p class="first admonition-title">Danger</p>
<p class="last">The provided callable will be called for every request
on the decorated route. For expensive retrievals, consider
caching the response.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The callable is called from within a
<span class="xref std std-ref">flask request context</span>.</p>
</div>
</div></blockquote>
</dd>
<dt>Exemption conditions</dt>
<dd><p class="first">Each limit can be exempted when given conditions are fulfilled. These
conditions can be specified by supplying a callable as an
<code class="docutils literal"><span class="pre">`exempt_when`</span></code> argument when defining the limit.</p>
<blockquote class="last">
<div><div class="highlight-python"><div class="highlight"><pre><span></span><span class="nd">@app.route</span><span class="p">(</span><span class="s2">"/expensive"</span><span class="p">)</span>
<span class="nd">@limiter.limit</span><span class="p">(</span><span class="s2">"100/day"</span><span class="p">,</span> <span class="n">exempt_when</span><span class="o">=</span><span class="k">lambda</span><span class="p">:</span> <span class="n">current_user</span><span class="o">.</span><span class="n">is_admin</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">expensive_route</span><span class="p">():</span>
<span class="o">...</span>
</pre></div>
</div>
</div></blockquote>
</dd>
</dl>
</dd>
</dl>
<dl class="docutils" id="ratelimit-decorator-shared-limit">
<dt><a class="reference internal" href="#flask_limiter.Limiter.shared_limit" title="flask_limiter.Limiter.shared_limit"><code class="xref py py-meth docutils literal"><span class="pre">Limiter.shared_limit()</span></code></a></dt>
<dd><p class="first">For scenarios where a rate limit should be shared by multiple routes
(For example when you want to protect routes using the same resource
with an umbrella rate limit).</p>
<p>Named shared limit</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">mysql_limit</span> <span class="o">=</span> <span class="n">limiter</span><span class="o">.</span><span class="n">shared_limit</span><span class="p">(</span><span class="s2">"100/hour"</span><span class="p">,</span> <span class="n">scope</span><span class="o">=</span><span class="s2">"mysql"</span><span class="p">)</span>
<span class="nd">@app.route</span><span class="p">(</span><span class="s2">".."</span><span class="p">)</span>
<span class="nd">@mysql_limit</span>
<span class="k">def</span> <span class="nf">r1</span><span class="p">():</span>
<span class="o">...</span>
<span class="nd">@app.route</span><span class="p">(</span><span class="s2">".."</span><span class="p">)</span>
<span class="nd">@mysql_limit</span>
<span class="k">def</span> <span class="nf">r2</span><span class="p">():</span>
<span class="o">...</span>
</pre></div>
</div>
</div></blockquote>
<p>Dynamic shared limit: when a callable is passed as scope, the return value
of the function will be used as the scope.</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">host_scope</span><span class="p">():</span>
<span class="k">return</span> <span class="n">request</span><span class="o">.</span><span class="n">host</span>
<span class="n">host_limit</span> <span class="o">=</span> <span class="n">limiter</span><span class="o">.</span><span class="n">shared_limit</span><span class="p">(</span><span class="s2">"100/hour"</span><span class="p">,</span> <span class="n">scope</span><span class="o">=</span><span class="n">host_scope</span><span class="p">)</span>
<span class="nd">@app.route</span><span class="p">(</span><span class="s2">".."</span><span class="p">)</span>
<span class="nd">@host_limit</span>
<span class="k">def</span> <span class="nf">r1</span><span class="p">():</span>
<span class="o">...</span>
<span class="nd">@app.route</span><span class="p">(</span><span class="s2">".."</span><span class="p">)</span>
<span class="nd">@host_limit</span>
<span class="k">def</span> <span class="nf">r2</span><span class="p">():</span>
<span class="o">...</span>
</pre></div>
</div>
</div></blockquote>
<div class="last admonition note">
<p class="first admonition-title">Note</p>
<p>Shared rate limits provide the same conveniences as individual rate limits</p>
<ul class="last simple">
<li>Can be chained with other shared limits or individual limits</li>
<li>Accept keying functions</li>
<li>Accept callables to determine the rate limit value</li>
</ul>
</div>
</dd>
</dl>
<dl class="docutils" id="ratelimit-decorator-exempt">
<dt><a class="reference internal" href="#flask_limiter.Limiter.exempt" title="flask_limiter.Limiter.exempt"><code class="xref py py-meth docutils literal"><span class="pre">Limiter.exempt()</span></code></a></dt>
<dd>This decorator simply marks a route as being exempt from any rate limits.</dd>
</dl>
<dl class="docutils" id="ratelimit-decorator-request-filter">
<dt><a class="reference internal" href="#flask_limiter.Limiter.request_filter" title="flask_limiter.Limiter.request_filter"><code class="xref py py-meth docutils literal"><span class="pre">Limiter.request_filter()</span></code></a></dt>
<dd><p class="first">This decorator simply marks a function as a filter for requests that are going
to be tested for rate limits. If any of the request filters return <code class="docutils literal"><span class="pre">True</span></code> no
rate limiting will be performed for that request. This mechanism can be used to
create custom white lists.</p>
<blockquote class="last">
<div><blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span></span><span class="nd">@limiter.request_filter</span>
<span class="k">def</span> <span class="nf">header_whitelist</span><span class="p">():</span>
<span class="k">return</span> <span class="n">request</span><span class="o">.</span><span class="n">headers</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"X-Internal"</span><span class="p">,</span> <span class="s2">""</span><span class="p">)</span> <span class="o">==</span> <span class="s2">"true"</span>
<span class="nd">@limiter.request_filter</span>
<span class="k">def</span> <span class="nf">ip_whitelist</span><span class="p">():</span>
<span class="k">return</span> <span class="n">request</span><span class="o">.</span><span class="n">remote_addr</span> <span class="o">==</span> <span class="s2">"127.0.0.1"</span>
</pre></div>
</div>
</div></blockquote>
<p>In the above example, any request that contains the header <code class="docutils literal"><span class="pre">X-Internal:</span> <span class="pre">true</span></code>
or originates from localhost will not be rate limited.</p>
</div></blockquote>
</dd>
</dl>
</div>
</div>
<div class="section" id="configuration">
<span id="ratelimit-conf"></span><h2>Configuration<a class="headerlink" href="#configuration" title="Permalink to this headline">¶</a></h2>
<p>The following flask configuration values are honored by
<a class="reference internal" href="#flask_limiter.Limiter" title="flask_limiter.Limiter"><code class="xref py py-class docutils literal"><span class="pre">Limiter</span></code></a>. If the corresponding configuration is passed in to the
<a class="reference internal" href="#flask_limiter.Limiter" title="flask_limiter.Limiter"><code class="xref py py-class docutils literal"><span class="pre">Limiter</span></code></a> constructor, those will take precedence.</p>
<table border="1" class="docutils">
<colgroup>
<col width="22%" />
<col width="78%" />
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">RATELIMIT_GLOBAL</span></code></td>
<td>A comma (or some other delimiter) separated string
that will be used to apply a global limit on all
routes. If not provided, the global limits can be
passed to the <a class="reference internal" href="#flask_limiter.Limiter" title="flask_limiter.Limiter"><code class="xref py py-class docutils literal"><span class="pre">Limiter</span></code></a> constructor
as well (the values passed to the constructor take precedence
over those in the config). <a class="reference internal" href="#ratelimit-string"><span class="std std-ref">Rate limit string notation</span></a> for details.</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">RATELIMIT_STORAGE_URL</span></code></td>
<td>One of <code class="docutils literal"><span class="pre">memory://</span></code> or <code class="docutils literal"><span class="pre">redis://host:port</span></code>
or <code class="docutils literal"><span class="pre">memcached://host:port</span></code>. Using the redis storage
requires the installation of the <a class="reference external" href="https://pypi.python.org/pypi/redis">redis</a> package
while memcached relies on the <a class="reference external" href="https://pypi.python.org/pypi/pymemcache">pymemcache</a> package.
(For details refer to <span class="xref std std-ref">storage-scheme</span>)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">RATELIMIT_STORAGE_OPTIONS</span></code></td>
<td>A dictionary to set extra options to be passed to the
storage implementation upon initialization. (Useful if you’re
subclassing <code class="xref py py-class docutils literal"><span class="pre">limits.storage.Storage</span></code> to create a
custom Storage backend.)</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">RATELIMIT_STRATEGY</span></code></td>
<td>The rate limiting strategy to use. <a class="reference internal" href="#ratelimit-strategy"><span class="std std-ref">Rate limiting strategies</span></a>
for details.</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">RATELIMIT_HEADERS_ENABLED</span></code></td>
<td>Enables returning <a class="reference internal" href="#ratelimit-headers"><span class="std std-ref">Rate-limiting Headers</span></a>. Defaults to <code class="docutils literal"><span class="pre">False</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">RATELIMIT_ENABLED</span></code></td>
<td>Overall kill switch for rate limits. Defaults to <code class="docutils literal"><span class="pre">True</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">RATELIMIT_HEADER_LIMIT</span></code></td>
<td>Header for the current rate limit. Defaults to <code class="docutils literal"><span class="pre">X-RateLimit-Limit</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">RATELIMIT_HEADER_RESET</span></code></td>
<td>Header for the reset time of the current rate limit. Defaults to <code class="docutils literal"><span class="pre">X-RateLimit-Reset</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">RATELIMIT_HEADER_REMAINING</span></code></td>
<td>Header for the number of requests remaining in the current rate limit. Defaults to <code class="docutils literal"><span class="pre">X-RateLimit-Remaining</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">RATELIMIT_HEADER_REMAINING</span></code></td>
<td>Header for when the client should retry the request. Defaults to <code class="docutils literal"><span class="pre">Retry-After</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">RATELIMIT_HEADER_REMAINING_VALUE</span></code></td>
<td>Allows configuration of how the value of the <cite>Retry-After</cite> header is rendered. One of <cite>http-date</cite> or <cite>delta-seconds</cite>. (<a class="reference external" href="https://tools.ietf.org/html/rfc2616#section-14.37">RFC2616</a>).</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">RATELIMIT_SWALLOW_ERRORS</span></code></td>
<td>Whether to allow failures while attempting to perform a rate limit
such as errors with downstream storage. Setting this value to <code class="docutils literal"><span class="pre">True</span></code>
will effectively disable rate limiting for requests where an error has
occurred.</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">RATELIMIT_IN_MEMORY_FALLBACK</span></code></td>
<td>A comma (or some other delimiter) separated string
that will be used when the configured storage is down.</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="rate-limit-string-notation">
<span id="ratelimit-string"></span><h2>Rate limit string notation<a class="headerlink" href="#rate-limit-string-notation" title="Permalink to this headline">¶</a></h2>
<p>Rate limits are specified as strings following the format:</p>
<blockquote>
<div>[count] [per|/] [n (optional)] [second|minute|hour|day|month|year]</div></blockquote>
<p>You can combine multiple rate limits by separating them with a delimiter of your
choice.</p>
<div class="section" id="examples">
<h3>Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>10 per hour</li>
<li>10/hour</li>
<li>10/hour;100/day;2000 per year</li>
<li>100/day, 500/7days</li>
</ul>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">If rate limit strings that are provided to the <a class="reference internal" href="#flask_limiter.Limiter.limit" title="flask_limiter.Limiter.limit"><code class="xref py py-meth docutils literal"><span class="pre">Limiter.limit()</span></code></a>
decorator are malformed and can’t be parsed the decorated route will fall back
to the global rate limit(s) and an <code class="docutils literal"><span class="pre">ERROR</span></code> log message will be emitted. Refer
to <a class="reference internal" href="#logging"><span class="std std-ref">Logging</span></a> for more details on capturing this information. Malformed
global rate limit strings will however raise an exception as they are evaluated
early enough to not cause disruption to a running application.</p>
</div>
</div>
</div>
<div class="section" id="rate-limiting-strategies">
<span id="ratelimit-strategy"></span><h2>Rate limiting strategies<a class="headerlink" href="#rate-limiting-strategies" title="Permalink to this headline">¶</a></h2>
<p>Flask-Limiter comes with three different rate limiting strategies built-in. Pick
the one that works for your use-case by specifying it in your flask config as
<code class="docutils literal"><span class="pre">RATELIMIT_STRATEGY</span></code> (one of <code class="docutils literal"><span class="pre">fixed-window</span></code>, <code class="docutils literal"><span class="pre">fixed-window-elastic-expiry</span></code>,
or <code class="docutils literal"><span class="pre">moving-window</span></code>), or as a constructor keyword argument. The default
configuration is <code class="docutils literal"><span class="pre">fixed-window</span></code>.</p>
<div class="section" id="fixed-window">
<h3>Fixed Window<a class="headerlink" href="#fixed-window" title="Permalink to this headline">¶</a></h3>
<p>This is the most memory efficient strategy to use as it maintains one counter
per resource and rate limit. It does however have its drawbacks as it allows
bursts within each window - thus allowing an ‘attacker’ to by-pass the limits.
The effects of these bursts can be partially circumvented by enforcing multiple
granularities of windows per resource.</p>
<p>For example, if you specify a <code class="docutils literal"><span class="pre">100/minute</span></code> rate limit on a route, this strategy will
allow 100 hits in the last second of one window and a 100 more in the first
second of the next window. To ensure that such bursts are managed, you could add a second rate limit
of <code class="docutils literal"><span class="pre">2/second</span></code> on the same route.</p>
</div>
<div class="section" id="fixed-window-with-elastic-expiry">
<h3>Fixed Window with Elastic Expiry<a class="headerlink" href="#fixed-window-with-elastic-expiry" title="Permalink to this headline">¶</a></h3>
<p>This strategy works almost identically to the Fixed Window strategy with the exception
that each hit results in the extension of the window. This strategy works well for
creating large penalties for breaching a rate limit.</p>
<p>For example, if you specify a <code class="docutils literal"><span class="pre">100/minute</span></code> rate limit on a route and it is being
attacked at the rate of 5 hits per second for 2 minutes - the attacker will be locked
out of the resource for an extra 60 seconds after the last hit. This strategy helps
circumvent bursts.</p>
</div>
<div class="section" id="moving-window">
<h3>Moving Window<a class="headerlink" href="#moving-window" title="Permalink to this headline">¶</a></h3>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">The moving window strategy is only implemented for the <code class="docutils literal"><span class="pre">redis</span></code> and <code class="docutils literal"><span class="pre">in-memory</span></code>
storage backends. The strategy requires using a list with fast random access which
is not very convenient to implement with a memcached storage.</p>
</div>
<p>This strategy is the most effective for preventing bursts from by-passing the
rate limit as the window for each limit is not fixed at the start and end of each time unit
(i.e. N/second for a moving window means N in the last 1000 milliseconds). There is
however a higher memory cost associated with this strategy as it requires <code class="docutils literal"><span class="pre">N</span></code> items to
be maintained in memory per resource and rate limit.</p>
</div>
</div>
<div class="section" id="rate-limiting-headers">
<span id="ratelimit-headers"></span><h2>Rate-limiting Headers<a class="headerlink" href="#rate-limiting-headers" title="Permalink to this headline">¶</a></h2>
<p>If the configuration is enabled, information about the rate limit with respect to the
route being requested will be added to the response headers. Since multiple rate limits
can be active for a given route - the rate limit with the lowest time granularity will be
used in the scenario when the request does not breach any rate limits.</p>
<table border="1" class="docutils">
<colgroup>
<col width="28%" />
<col width="72%" />
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">X-RateLimit-Limit</span></code></td>
<td>The total number of requests allowed for the
active window</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">X-RateLimit-Remaining</span></code></td>
<td>The number of requests remaining in the active
window.</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">X-RateLimit-Reset</span></code></td>
<td>UTC seconds since epoch when the window will be
reset.</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">Retry-After</span></code></td>
<td>Seconds to retry after or the http date when the
Rate Limit will be reset. The way the value is presented
depends on the configuration value set in <cite>RATELIMIT_HEADER_REMAINING_VALUE</cite>
and defaults to <cite>delta-seconds</cite>.</td>
</tr>
</tbody>
</table>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p>Enabling the headers has an additional cost with certain storage / strategy combinations.</p>
<ul class="last simple">
<li>Memcached + Fixed Window: an extra key per rate limit is stored to calculate
<code class="docutils literal"><span class="pre">X-RateLimit-Reset</span></code></li>
<li>Redis + Moving Window: an extra call to redis is involved during every request
to calculate <code class="docutils literal"><span class="pre">X-RateLimit-Remaining</span></code> and <code class="docutils literal"><span class="pre">X-RateLimit-Reset</span></code></li>
</ul>
</div>
<p>The header names can be customised if required by either using the flask configuration (<a class="reference internal" href="#ratelimit-conf"><span class="std std-ref">Configuration</span></a>)
values or by setting the <code class="docutils literal"><span class="pre">header_mapping</span></code> property of the <a class="reference internal" href="#flask_limiter.Limiter" title="flask_limiter.Limiter"><code class="xref py py-class docutils literal"><span class="pre">Limiter</span></code></a> as follows:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">flask.ext.limiter</span> <span class="k">import</span> <span class="n">Limiter</span><span class="p">,</span> <span class="n">HEADERS</span>
<span class="n">limiter</span> <span class="o">=</span> <span class="n">Limiter</span><span class="p">()</span>
<span class="n">limiter</span><span class="o">.</span><span class="n">header_mapping</span> <span class="o">=</span> <span class="p">{</span>
<span class="n">HEADERS</span><span class="o">.</span><span class="n">LIMIT</span> <span class="p">:</span> <span class="s2">"X-My-Limit"</span><span class="p">,</span>
<span class="n">HEADERS</span><span class="o">.</span><span class="n">RESET</span> <span class="p">:</span> <span class="s2">"X-My-Reset"</span><span class="p">,</span>
<span class="n">HEADERS</span><span class="o">.</span><span class="n">REMAINING</span><span class="p">:</span> <span class="s2">"X-My-Remaining"</span>
<span class="p">}</span>
<span class="c1"># or by only partially specifying the overrides</span>
<span class="n">limiter</span><span class="o">.</span><span class="n">header_mapping</span><span class="p">[</span><span class="n">HEADERS</span><span class="o">.</span><span class="n">LIMIT</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'X-My-Limit'</span>
</pre></div>
</div>
</div>
<div class="section" id="recipes">
<h2>Recipes<a class="headerlink" href="#recipes" title="Permalink to this headline">¶</a></h2>
<div class="section" id="rate-limit-key-functions">
<span id="keyfunc-customization"></span><h3>Rate Limit Key Functions<a class="headerlink" href="#rate-limit-key-functions" title="Permalink to this headline">¶</a></h3>
<p>You can easily customize your rate limits to be based on any
characteristic of the incoming request. Both the <a class="reference internal" href="#flask_limiter.Limiter" title="flask_limiter.Limiter"><code class="xref py py-class docutils literal"><span class="pre">Limiter</span></code></a> constructor
and the <a class="reference internal" href="#flask_limiter.Limiter.limit" title="flask_limiter.Limiter.limit"><code class="xref py py-meth docutils literal"><span class="pre">Limiter.limit()</span></code></a> decorator accept a keyword argument
<code class="docutils literal"><span class="pre">key_func</span></code> that should return a string (or an object that has a string representation).</p>
<p>Rate limiting a route by current user (using Flask-Login):</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="nd">@route</span><span class="p">(</span><span class="s2">"/test"</span><span class="p">)</span>
<span class="nd">@login_required</span>
<span class="nd">@limiter</span><span class="o">.</span><span class="n">limit</span><span class="p">(</span><span class="s2">"1 per day"</span><span class="p">,</span> <span class="n">key_func</span> <span class="o">=</span> <span class="k">lambda</span> <span class="p">:</span> <span class="n">current_user</span><span class="o">.</span><span class="n">username</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">test_route</span><span class="p">():</span>
<span class="k">return</span> <span class="s2">"42"</span>
</pre></div>
</div>
<p>Rate limiting all requests by country:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">flask</span> <span class="k">import</span> <span class="n">request</span><span class="p">,</span> <span class="n">Flask</span>
<span class="kn">import</span> <span class="nn">GeoIP</span>
<span class="n">gi</span> <span class="o">=</span> <span class="n">GeoIP</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">"GeoLiteCity.dat"</span><span class="p">,</span> <span class="n">GeoIP</span><span class="o">.</span><span class="n">GEOIP_INDEX_CACHE</span> <span class="o">|</span> <span class="n">GeoIP</span><span class="o">.</span><span class="n">GEOIP_CHECK_CACHE</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">get_request_country</span><span class="p">():</span>
<span class="k">return</span> <span class="n">gi</span><span class="o">.</span><span class="n">record_by_name</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">remote_addr</span><span class="p">)[</span><span class="s1">'region_name'</span><span class="p">]</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>
<span class="n">limiter</span> <span class="o">=</span> <span class="n">Limiter</span><span class="p">(</span><span class="n">app</span><span class="p">,</span> <span class="n">global_limits</span><span class="o">=</span><span class="p">[</span><span class="s2">"10/hour"</span><span class="p">],</span> <span class="n">key_func</span> <span class="o">=</span> <span class="n">get_request_country</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="custom-rate-limit-exceeded-responses">
<h3>Custom Rate limit exceeded responses<a class="headerlink" href="#custom-rate-limit-exceeded-responses" title="Permalink to this headline">¶</a></h3>
<p>The default configuration results in an <code class="docutils literal"><span class="pre">abort(429)</span></code> being called every time
a rate limit is exceeded for a particular route. The exceeded limit is added to
the response and results in an response body that looks something like:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>429 Too Many Requests</title>
<h1>Too Many Requests</h1>
<p>1 per 1 day</p>
</pre></div>
</div>
<p>If you want to configure the response you can register an error handler for the
<code class="docutils literal"><span class="pre">429</span></code> error code in a manner similar to the following example, which returns a
json response instead:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="nd">@app</span><span class="o">.</span><span class="n">errorhandler</span><span class="p">(</span><span class="mi">429</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">ratelimit_handler</span><span class="p">(</span><span class="n">e</span><span class="p">):</span>
<span class="k">return</span> <span class="n">make_response</span><span class="p">(</span>
<span class="n">jsonify</span><span class="p">(</span><span class="n">error</span><span class="o">=</span><span class="s2">"ratelimit exceeded </span><span class="si">%s</span><span class="s2">"</span> <span class="o">%</span> <span class="n">e</span><span class="o">.</span><span class="n">description</span><span class="p">)</span>
<span class="p">,</span> <span class="mi">429</span>
<span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="using-flask-pluggable-views">
<h3>Using Flask Pluggable Views<a class="headerlink" href="#using-flask-pluggable-views" title="Permalink to this headline">¶</a></h3>
<p>If you are using a class based approach to defining view function, the regular
method of decorating a view function to apply a per route rate limit will not
work. You can add rate limits to your view classes using the following approach.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>
<span class="n">limiter</span> <span class="o">=</span> <span class="n">Limiter</span><span class="p">(</span><span class="n">app</span><span class="p">,</span> <span class="n">key_func</span><span class="o">=</span><span class="n">get_remote_address</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">MyView</span><span class="p">(</span><span class="n">flask</span><span class="o">.</span><span class="n">views</span><span class="o">.</span><span class="n">MethodView</span><span class="p">):</span>
<span class="n">decorators</span> <span class="o">=</span> <span class="p">[</span><span class="n">limiter</span><span class="o">.</span><span class="n">limit</span><span class="p">(</span><span class="s2">"10/second"</span><span class="p">)]</span>
<span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="s2">"get"</span>
<span class="k">def</span> <span class="nf">put</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="s2">"put"</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This approach is limited to either sharing the same rate limit for
all http methods of a given <code class="xref py py-class docutils literal"><span class="pre">flask.views.View</span></code> or applying the declared
rate limit independently for each http method (to accomplish this, pass in <code class="docutils literal"><span class="pre">True</span></code> to
the <code class="docutils literal"><span class="pre">per_method</span></code> keyword argument to <a class="reference internal" href="#flask_limiter.Limiter.limit" title="flask_limiter.Limiter.limit"><code class="xref py py-meth docutils literal"><span class="pre">Limiter.limit()</span></code></a>). Alternatively, the limit
can be restricted to only certain http methods by passing them as a list to the <cite>methods</cite>
keyword argument.</p>
</div>
<p>The above approach has been tested with sub-classes of <code class="xref py py-class docutils literal"><span class="pre">flask.views.View</span></code>,
<code class="xref py py-class docutils literal"><span class="pre">flask.views.MethodView</span></code> and <code class="xref py py-class docutils literal"><span class="pre">flask.ext.restful.Resource</span></code>.</p>
</div>
<div class="section" id="rate-limiting-all-routes-in-a-flask-blueprint">
<h3>Rate limiting all routes in a <code class="xref py py-class docutils literal"><span class="pre">flask.Blueprint</span></code><a class="headerlink" href="#rate-limiting-all-routes-in-a-flask-blueprint" title="Permalink to this headline">¶</a></h3>
<p><a class="reference internal" href="#flask_limiter.Limiter.limit" title="flask_limiter.Limiter.limit"><code class="xref py py-meth docutils literal"><span class="pre">Limiter.limit()</span></code></a>, <a class="reference internal" href="#flask_limiter.Limiter.shared_limit" title="flask_limiter.Limiter.shared_limit"><code class="xref py py-meth docutils literal"><span class="pre">Limiter.shared_limit()</span></code></a> & <a class="reference internal" href="#flask_limiter.Limiter.exempt" title="flask_limiter.Limiter.exempt"><code class="xref py py-meth docutils literal"><span class="pre">Limiter.exempt()</span></code></a> can
all be applied to <code class="xref py py-class docutils literal"><span class="pre">flask.Blueprint</span></code> instances as well. In the following example
the <strong>login</strong> Blueprint has a special rate limit applied to all its routes, while the <strong>help</strong>
Blueprint is exempt from all rate limits. The <strong>regular</strong> Blueprint follows the global rate limits.</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>
<span class="n">login</span> <span class="o">=</span> <span class="n">Blueprint</span><span class="p">(</span><span class="s2">"login"</span><span class="p">,</span> <span class="vm">__name__</span><span class="p">,</span> <span class="n">url_prefix</span> <span class="o">=</span> <span class="s2">"/login"</span><span class="p">)</span>
<span class="n">regular</span> <span class="o">=</span> <span class="n">Blueprint</span><span class="p">(</span><span class="s2">"regular"</span><span class="p">,</span> <span class="vm">__name__</span><span class="p">,</span> <span class="n">url_prefix</span> <span class="o">=</span> <span class="s2">"/regular"</span><span class="p">)</span>
<span class="n">doc</span> <span class="o">=</span> <span class="n">Blueprint</span><span class="p">(</span><span class="s2">"doc"</span><span class="p">,</span> <span class="vm">__name__</span><span class="p">,</span> <span class="n">url_prefix</span> <span class="o">=</span> <span class="s2">"/doc"</span><span class="p">)</span>
<span class="nd">@doc.route</span><span class="p">(</span><span class="s2">"/"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">doc_index</span><span class="p">():</span>
<span class="k">return</span> <span class="s2">"doc"</span>
<span class="nd">@regular.route</span><span class="p">(</span><span class="s2">"/"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">regular_index</span><span class="p">():</span>
<span class="k">return</span> <span class="s2">"regular"</span>
<span class="nd">@login.route</span><span class="p">(</span><span class="s2">"/"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">login_index</span><span class="p">():</span>
<span class="k">return</span> <span class="s2">"login"</span>
<span class="n">limiter</span> <span class="o">=</span> <span class="n">Limiter</span><span class="p">(</span><span class="n">app</span><span class="p">,</span> <span class="n">global_limits</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"1/second"</span><span class="p">],</span> <span class="n">key_func</span><span class="o">=</span><span class="n">get_remote_address</span><span class="p">)</span>
<span class="n">limiter</span><span class="o">.</span><span class="n">limit</span><span class="p">(</span><span class="s2">"60/hour"</span><span class="p">)(</span><span class="n">login</span><span class="p">)</span>
<span class="n">limiter</span><span class="o">.</span><span class="n">exempt</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
<span class="n">app</span><span class="o">.</span><span class="n">register_blueprint</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
<span class="n">app</span><span class="o">.</span><span class="n">register_blueprint</span><span class="p">(</span><span class="n">login</span><span class="p">)</span>
<span class="n">app</span><span class="o">.</span><span class="n">register_blueprint</span><span class="p">(</span><span class="n">regular</span><span class="p">)</span>
</pre></div>
</div>
</div></blockquote>
</div>
<div class="section" id="logging">
<span id="id2"></span><h3>Logging<a class="headerlink" href="#logging" title="Permalink to this headline">¶</a></h3>
<p>Each <a class="reference internal" href="#flask_limiter.Limiter" title="flask_limiter.Limiter"><code class="xref py py-class docutils literal"><span class="pre">Limiter</span></code></a> instance has a <code class="docutils literal"><span class="pre">logger</span></code> instance variable that is by
default <strong>not</strong> configured with a handler. You can add your own handler to obtain
log messages emitted by <code class="xref py py-mod docutils literal"><span class="pre">flask_limiter</span></code>.</p>
<p>Simple stdout handler:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">limiter</span> <span class="o">=</span> <span class="n">Limiter</span><span class="p">(</span><span class="n">app</span><span class="p">,</span> <span class="n">key_func</span><span class="o">=</span><span class="n">get_remote_address</span><span class="p">)</span>
<span class="n">limiter</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">addHandler</span><span class="p">(</span><span class="n">StreamHandler</span><span class="p">())</span>
</pre></div>
</div>
<p>Reusing all the handlers of the <code class="docutils literal"><span class="pre">logger</span></code> instance of the <code class="xref py py-class docutils literal"><span class="pre">flask.Flask</span></code> app:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>
<span class="n">limiter</span> <span class="o">=</span> <span class="n">Limiter</span><span class="p">(</span><span class="n">app</span><span class="p">,</span> <span class="n">key_func</span><span class="o">=</span><span class="n">get_remote_address</span><span class="p">)</span>
<span class="k">for</span> <span class="n">handler</span> <span class="ow">in</span> <span class="n">app</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">handlers</span><span class="p">:</span>
<span class="n">limiter</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">addHandler</span><span class="p">(</span><span class="n">handler</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="custom-error-messages">
<h3>Custom error messages<a class="headerlink" href="#custom-error-messages" title="Permalink to this headline">¶</a></h3>
<p><a class="reference internal" href="#flask_limiter.Limiter.limit" title="flask_limiter.Limiter.limit"><code class="xref py py-meth docutils literal"><span class="pre">Limiter.limit()</span></code></a> & <a class="reference internal" href="#flask_limiter.Limiter.shared_limit" title="flask_limiter.Limiter.shared_limit"><code class="xref py py-meth docutils literal"><span class="pre">Limiter.shared_limit()</span></code></a> can be provided with an <cite>error_message</cite>
argument to over ride the default <cite>n per x</cite> error message that is returned to the calling client.
The <cite>error_message</cite> argument can either be a simple string or a callable that returns one.</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>
<span class="n">limiter</span> <span class="o">=</span> <span class="n">Limiter</span><span class="p">(</span><span class="n">app</span><span class="p">,</span> <span class="n">key_func</span><span class="o">=</span><span class="n">get_remote_address</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">error_handler</span><span class="p">():</span>
<span class="k">return</span> <span class="n">app</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"DEFAULT_ERROR_MESSAGE"</span><span class="p">)</span>
<span class="nd">@limiter.limit</span><span class="p">(</span><span class="s2">"1/second"</span><span class="p">,</span> <span class="n">error_message</span><span class="o">=</span><span class="s1">'chill!'</span><span class="p">)</span>
<span class="nd">@app.route</span><span class="p">(</span><span class="s2">"/"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">index</span><span class="p">():</span>
<span class="o">....</span>
<span class="nd">@limiter.limit</span><span class="p">(</span><span class="s2">"10/second"</span><span class="p">,</span> <span class="n">error_message</span><span class="o">=</span><span class="n">error_handler</span><span class="p">)</span>
<span class="nd">@app.route</span><span class="p">(</span><span class="s2">"/ping"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">ping</span><span class="p">():</span>
<span class="o">....</span>
</pre></div>
</div>
</div></blockquote>
</div>
</div>
<div class="section" id="api">
<h2>API<a class="headerlink" href="#api" title="Permalink to this headline">¶</a></h2>
<div class="section" id="core">
<h3>Core<a class="headerlink" href="#core" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="flask_limiter.Limiter">
<em class="property">class </em><code class="descclassname">flask_limiter.</code><code class="descname">Limiter</code><span class="sig-paren">(</span><em>app=None</em>, <em>key_func=None</em>, <em>global_limits=[]</em>, <em>headers_enabled=False</em>, <em>strategy=None</em>, <em>storage_uri=None</em>, <em>storage_options={}</em>, <em>auto_check=True</em>, <em>swallow_errors=False</em>, <em>in_memory_fallback=[]</em>, <em>retry_after=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/flask_limiter/extension.html#Limiter"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#flask_limiter.Limiter" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <code class="xref py py-class docutils literal"><span class="pre">object</span></code></p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>app</strong> – <code class="xref py py-class docutils literal"><span class="pre">flask.Flask</span></code> instance to initialize the extension
with.</li>
<li><strong>global_limits</strong> (<em>list</em>) – a variable list of strings denoting global
limits to apply to all routes. <a class="reference internal" href="#ratelimit-string"><span class="std std-ref">Rate limit string notation</span></a> for more details.</li>
<li><strong>key_func</strong> (<em>function</em>) – a callable that returns the domain to rate limit by.</li>
<li><strong>headers_enabled</strong> (<em>bool</em>) – whether <code class="docutils literal"><span class="pre">X-RateLimit</span></code> response headers are written.</li>
<li><strong>strategy</strong> (<em>str</em>) – the strategy to use. refer to <a class="reference internal" href="#ratelimit-strategy"><span class="std std-ref">Rate limiting strategies</span></a></li>
<li><strong>storage_uri</strong> (<em>str</em>) – the storage location. refer to <a class="reference internal" href="#ratelimit-conf"><span class="std std-ref">Configuration</span></a></li>
<li><strong>storage_options</strong> (<em>dict</em>) – kwargs to pass to the storage implementation upon
instantiation.</li>
<li><strong>auto_check</strong> (<em>bool</em>) – whether to automatically check the rate limit in the before_request
chain of the application. default <code class="docutils literal"><span class="pre">True</span></code></li>
<li><strong>swallow_errors</strong> (<em>bool</em>) – whether to swallow errors when hitting a rate limit.
An exception will still be logged. default <code class="docutils literal"><span class="pre">False</span></code></li>
<li><strong>in_memory_fallback</strong> (<em>list</em>) – a variable list of strings denoting fallback
limits to apply when the storage is down.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<dl class="method">
<dt id="flask_limiter.Limiter.check">
<code class="descname">check</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/flask_limiter/extension.html#Limiter.check"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#flask_limiter.Limiter.check" title="Permalink to this definition">¶</a></dt>
<dd><p>check the limits for the current request</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Raises:</th><td class="field-body">RateLimitExceeded</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="flask_limiter.Limiter.exempt">
<code class="descname">exempt</code><span class="sig-paren">(</span><em>obj</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/flask_limiter/extension.html#Limiter.exempt"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#flask_limiter.Limiter.exempt" title="Permalink to this definition">¶</a></dt>
<dd><p>decorator to mark a view or all views in a blueprint as exempt from rate limits.</p>
</dd></dl>
<dl class="method">
<dt id="flask_limiter.Limiter.init_app">
<code class="descname">init_app</code><span class="sig-paren">(</span><em>app</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/flask_limiter/extension.html#Limiter.init_app"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#flask_limiter.Limiter.init_app" title="Permalink to this definition">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>app</strong> – <code class="xref py py-class docutils literal"><span class="pre">flask.Flask</span></code> instance to rate limit.</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="flask_limiter.Limiter.limit">
<code class="descname">limit</code><span class="sig-paren">(</span><em>limit_value</em>, <em>key_func=None</em>, <em>per_method=False</em>, <em>methods=None</em>, <em>error_message=None</em>, <em>exempt_when=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/flask_limiter/extension.html#Limiter.limit"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#flask_limiter.Limiter.limit" title="Permalink to this definition">¶</a></dt>
<dd><p>decorator to be used for rate limiting individual routes or blueprints.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>limit_value</strong> – rate limit string or a callable that returns a string.
<a class="reference internal" href="#ratelimit-string"><span class="std std-ref">Rate limit string notation</span></a> for more details.</li>
<li><strong>key_func</strong> (<em>function</em>) – function/lambda to extract the unique identifier for
the rate limit. defaults to remote address of the request.</li>
<li><strong>per_method</strong> (<em>bool</em>) – whether the limit is sub categorized into the http
method of the request.</li>
<li><strong>methods</strong> (<em>list</em>) – if specified, only the methods in this list will be rate
limited (default: None).</li>
<li><strong>error_message</strong> – string (or callable that returns one) to override the
error message used in the response.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last"></p>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="flask_limiter.Limiter.request_filter">
<code class="descname">request_filter</code><span class="sig-paren">(</span><em>fn</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/flask_limiter/extension.html#Limiter.request_filter"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#flask_limiter.Limiter.request_filter" title="Permalink to this definition">¶</a></dt>
<dd><p>decorator to mark a function as a filter to be executed
to check if the request is exempt from rate limiting.</p>
</dd></dl>
<dl class="method">
<dt id="flask_limiter.Limiter.reset">
<code class="descname">reset</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/flask_limiter/extension.html#Limiter.reset"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#flask_limiter.Limiter.reset" title="Permalink to this definition">¶</a></dt>
<dd><p>resets the storage if it supports being reset</p>
</dd></dl>
<dl class="method">
<dt id="flask_limiter.Limiter.shared_limit">
<code class="descname">shared_limit</code><span class="sig-paren">(</span><em>limit_value</em>, <em>scope</em>, <em>key_func=None</em>, <em>error_message=None</em>, <em>exempt_when=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/flask_limiter/extension.html#Limiter.shared_limit"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#flask_limiter.Limiter.shared_limit" title="Permalink to this definition">¶</a></dt>
<dd><p>decorator to be applied to multiple routes sharing the same rate limit.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>limit_value</strong> – rate limit string or a callable that returns a string.
<a class="reference internal" href="#ratelimit-string"><span class="std std-ref">Rate limit string notation</span></a> for more details.</li>
<li><strong>scope</strong> – a string or callable that returns a string
for defining the rate limiting scope.</li>
<li><strong>key_func</strong> (<em>function</em>) – function/lambda to extract the unique identifier for
the rate limit. defaults to remote address of the request.</li>
<li><strong>error_message</strong> – string (or callable that returns one) to override the
error message used in the response.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
</dd></dl>
</div>
<div class="section" id="exceptions">
<h3>Exceptions<a class="headerlink" href="#exceptions" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="flask_limiter.RateLimitExceeded">
<code class="descclassname">flask_limiter.</code><code class="descname">RateLimitExceeded</code><a class="headerlink" href="#flask_limiter.RateLimitExceeded" title="Permalink to this definition">¶</a></dt>
<dd><p>alias of <code class="xref py py-class docutils literal"><span class="pre">TooManyRequests</span></code></p>
</dd></dl>
</div>
<div class="section" id="module-flask_limiter.util">
<span id="utils"></span><h3>Utils<a class="headerlink" href="#module-flask_limiter.util" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="flask_limiter.util.get_ipaddr">
<code class="descclassname">flask_limiter.util.</code><code class="descname">get_ipaddr</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/flask_limiter/util.html#get_ipaddr"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#flask_limiter.util.get_ipaddr" title="Permalink to this definition">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body">the ip address for the current request (or 127.0.0.1 if none found)
based on the X-Forwarded-For headers.</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="flask_limiter.util.get_remote_address">
<code class="descclassname">flask_limiter.util.</code><code class="descname">get_remote_address</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/flask_limiter/util.html#get_remote_address"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#flask_limiter.util.get_remote_address" title="Permalink to this definition">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body">the ip address for the current request (or 127.0.0.1 if none found)</td>
</tr>
</tbody>
</table>
</dd></dl>
</div>
</div>
<div class="section" id="changelog">
<h2>Changelog<a class="headerlink" href="#changelog" title="Permalink to this headline">¶</a></h2>
<div class="section" id="id3">
<h3>0.9.3 2016-03-14<a class="headerlink" href="#id3" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Allow <cite>reset</cite> of limiter storage if available</li>
</ul>
</div>
<div class="section" id="id4">
<h3>0.9.2 2016-03-04<a class="headerlink" href="#id4" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Deprecation warning for default <cite>key_func</cite> <cite>get_ipaddr</cite></li>
<li>Support for <cite>Retry-After</cite> header</li>
</ul>
</div>
<div class="section" id="id5">
<h3>0.9.1 2015-11-21<a class="headerlink" href="#id5" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Re-expose <cite>enabled</cite> property on <cite>Limiter</cite> instance.</li>
</ul>
</div>
<div class="section" id="id6">
<h3>0.9 2015-11-13<a class="headerlink" href="#id6" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>In-memory fallback option for unresponsive storage</li>
<li>Rate limit exemption option per limit</li>
</ul>
</div>
<div class="section" id="id7">
<h3>0.8.5 2015-10-05<a class="headerlink" href="#id7" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Bug fix for reported issues of missing (limits) dependency upon installation.</li>
</ul>
</div>
<div class="section" id="id8">
<h3>0.8.4 2015-10-03<a class="headerlink" href="#id8" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Documentation tweaks.</li>
</ul>
</div>
<div class="section" id="id9">
<h3>0.8.2 2015-09-17<a class="headerlink" href="#id9" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Remove outdated files from egg</li>
</ul>
</div>
<div class="section" id="id10">
<h3>0.8.1 2015-08-06<a class="headerlink" href="#id10" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Fixed compatibility with latest version of <strong>Flask-Restful</strong></li>
</ul>
</div>
<div class="section" id="id11">
<h3>0.8 2015-06-07<a class="headerlink" href="#id11" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>No functional change</li>
</ul>
</div>
<div class="section" id="id12">
<h3>0.7.9 2015-04-02<a class="headerlink" href="#id12" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Bug fix for case sensitive <cite>methods</cite> whitelist for <cite>limits</cite> decorator</li>
</ul>
</div>
<div class="section" id="id13">
<h3>0.7.8 2015-03-20<a class="headerlink" href="#id13" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Hotfix for dynamic limits with blueprints</li>
<li>Undocumented feature to pass storage options to underlying storage backend.</li>
</ul>
</div>
<div class="section" id="id14">
<h3>0.7.6 2015-03-02<a class="headerlink" href="#id14" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li><cite>methods</cite> keyword argument for <cite>limits</cite> decorator to specify specific http
methods to apply the rate limit to.</li>
</ul>
</div>
<div class="section" id="id15">
<h3>0.7.5 2015-02-16<a class="headerlink" href="#id15" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li><a class="reference external" href="http://flask-limiter.readthedocs.org/en/stable/#custom-error-messages">Custom error messages</a>.</li>
</ul>
</div>
<div class="section" id="id17">
<h3>0.7.4 2015-02-03<a class="headerlink" href="#id17" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Use Werkzeug TooManyRequests as the exception raised when available.</li>
</ul>
</div>
<div class="section" id="id18">
<h3>0.7.3 2015-01-30<a class="headerlink" href="#id18" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Bug Fix<ul>
<li><dl class="first docutils">
<dt>Fix for version comparison when monkey patching Werkzeug</dt>
<dd>(<a class="reference external" href="https://github.com/alisaifee/flask-limiter/issues/24">Issue 24</a>)</dd>
</dl>
</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="id19">
<h3>0.7.1 2015-01-09<a class="headerlink" href="#id19" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Refactor core storage & ratelimiting strategy out into the <a class="reference external" href="http://github.com/alisaifee/limits">limits</a> package.</li>
<li>Remove duplicate hits when stacked rate limits are in use and a rate limit is hit.</li>
</ul>
</div>
<div class="section" id="id20">
<h3>0.7 2015-01-09<a class="headerlink" href="#id20" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Refactoring of RedisStorage for extensibility (<a class="reference external" href="https://github.com/alisaifee/flask-limiter/issues/18">Issue 18</a>)</li>
<li>Bug fix: Correct default setting for enabling rate limit headers. (<a class="reference external" href="https://github.com/alisaifee/flask-limiter/issues/22">Issue 22</a>)</li>
</ul>
</div>
<div class="section" id="id21">
<h3>0.6.6 2014-10-21<a class="headerlink" href="#id21" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Bug fix<ul>
<li>Fix for responses slower than rate limiting window.
(<a class="reference external" href="https://github.com/alisaifee/flask-limiter/issues/17">Issue 17</a>.)</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="id22">
<h3>0.6.5 2014-10-01<a class="headerlink" href="#id22" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Bug fix: in memory storage thread safety</li>
</ul>
</div>
<div class="section" id="id23">
<h3>0.6.4 2014-08-31<a class="headerlink" href="#id23" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Support for manually triggering rate limit check</li>
</ul>
</div>
<div class="section" id="id24">
<h3>0.6.3 2014-08-26<a class="headerlink" href="#id24" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Header name overrides</li>
</ul>
</div>
<div class="section" id="id25">
<h3>0.6.2 2014-07-13<a class="headerlink" href="#id25" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li><a class="reference external" href="http://flask-limiter.readthedocs.org/en/latest/#rate-limiting-all-routes-in-a-flask-blueprint">Rate limiting for blueprints</a></li>
</ul>
</div>
<div class="section" id="id26">
<h3>0.6.1 2014-07-11<a class="headerlink" href="#id26" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>per http method rate limit separation (<a class="reference external" href="http://flask-limiter.readthedocs.org/en/latest/index.html#using-flask-pluggable-views">Recipe</a>)</li>
<li>documentation improvements</li>
</ul>
</div>
<div class="section" id="id27">
<h3>0.6 2014-06-24<a class="headerlink" href="#id27" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li><a class="reference external" href="http://flask-limiter.readthedocs.org/en/latest/index.html#ratelimit-decorator-shared-limit">Shared limits between routes</a></li>
</ul>
</div>
<div class="section" id="id28">
<h3>0.5 2014-06-13<a class="headerlink" href="#id28" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li><a class="reference external" href="http://flask-limiter.readthedocs.org/en/latest/index.html#ratelimit-decorator-request-filter">Request Filters</a></li>
</ul>
</div>
<div class="section" id="id29">
<h3>0.4.4 2014-06-13<a class="headerlink" href="#id29" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Bug fix<ul>
<li>Werkzeug < 0.9 Compatibility
(<a class="reference external" href="https://github.com/alisaifee/flask-limiter/issues/6">Issue 6</a>.)</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="id30">
<h3>0.4.3 2014-06-12<a class="headerlink" href="#id30" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Hotfix : use HTTPException instead of abort to play well with other
extensions.</li>
</ul>
</div>
<div class="section" id="id31">
<h3>0.4.2 2014-06-12<a class="headerlink" href="#id31" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Allow configuration overrides via extension constructor</li>
</ul>
</div>
<div class="section" id="id32">
<h3>0.4.1 2014-06-04<a class="headerlink" href="#id32" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Improved implementation of moving-window X-RateLimit-Reset value.</li>
</ul>
</div>
<div class="section" id="id33">
<h3>0.4 2014-05-28<a class="headerlink" href="#id33" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li><a class="reference external" href="http://flask-limiter.readthedocs.org/en/latest/#rate-limiting-headers">Rate limiting headers</a></li>
</ul>
</div>
<div class="section" id="id35">
<h3>0.3.2 2014-05-26<a class="headerlink" href="#id35" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Bug fix<ul>
<li>Memory leak when using <code class="docutils literal"><span class="pre">Limiter.storage.MemoryStorage</span></code>
(<a class="reference external" href="https://github.com/alisaifee/flask-limiter/issues/4">Issue 4</a>.)</li>
</ul>
</li>
<li>Improved test coverage</li>
</ul>
</div>
<div class="section" id="id36">
<h3>0.3.1 2014-02-20<a class="headerlink" href="#id36" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Strict version requirement on six</li>
<li>documentation tweaks</li>
</ul>
</div>
<div class="section" id="id37">
<h3>0.3.0 2014-02-19<a class="headerlink" href="#id37" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>improved logging support for multiple handlers</li>
<li>allow callables to be passed to <code class="docutils literal"><span class="pre">Limiter.limit</span></code> decorator to dynamically
load rate limit strings.</li>
<li>add a global kill switch in flask config for all rate limits.</li>
<li>Bug fixes<ul>
<li>default key function for rate limit domain wasn’t accounting for
X-Forwarded-For header.</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="id38">
<h3>0.2.2 2014-02-18<a class="headerlink" href="#id38" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>add new decorator to exempt routes from limiting.</li>
<li>Bug fixes<ul>
<li>versioneer.py wasn’t included in manifest.</li>
<li>configuration string for strategy was out of sync with docs.</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="id39">
<h3>0.2.1 2014-02-15<a class="headerlink" href="#id39" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>python 2.6 support via counter backport</li>
<li>source docs.</li>
</ul>
</div>
<div class="section" id="id40">
<h3>0.2 2014-02-15<a class="headerlink" href="#id40" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Implemented configurable strategies for rate limiting.</li>
<li>Bug fixes<ul>
<li>better locking for in-memory storage</li>
<li>multi threading support for memcached storage</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="id41">
<h3>0.1.1 2014-02-14<a class="headerlink" href="#id41" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Bug fixes<ul>
<li>fix initializing the extension without an app</li>
<li>don’t rate limit static files</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="id42">
<h3>0.1.0 2014-02-13<a class="headerlink" href="#id42" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>first release.</li>
</ul>
</div>
</div>
<div class="section" id="references">
<h2>References<a class="headerlink" href="#references" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li><a class="reference external" href="http://redis.io/commands/INCR">Redis rate limiting pattern #2</a></li>
<li><a class="reference external" href="https://github.com/DomainTools/rate-limit">DomainTools redis rate limiter</a></li>
<li><a class="reference external" href="https://limits.readthedocs.org">limits: python rate limiting utilities</a></li>
</ul>
</div>
<div class="section" id="contributions">
<h2>Contributions<a class="headerlink" href="#contributions" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li><a class="reference external" href="https://github.com/timotheeg">Timothee Groleau</a></li>
<li><a class="reference external" href="https://github.com/zehua">Zehua Liu</a></li>
<li><a class="reference external" href="https://github.com/g-p-g">Guilherme Polo</a></li>
<li><a class="reference external" href="https://github.com/mtsgrd">Mattias Granlund</a></li>
<li><a class="reference external" href="https://github.com/joshfriend">Josh Friend</a></li>
<li><a class="reference external" href="https://github.com/samihiltunen">Sami Hiltunen</a></li>
<li><a class="reference external" href="https://github.com/henningpeters">Henning Peters</a></li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="#">
<img class="logo" src="_static/tap-logo.png" alt="Logo"/>
</a></p><h3>About</h3>
<p>
Flask-Limiter provides rate limiting features to flask routes.
It has support for a configurable backend for storage
with current implementations for in-memory, redis and memcache.
</p>
<h3>Useful Links</h3>
<ul>
<li><a href="http://pypi.python.org/pypi/Flask-Limiter">Flask-Limiter @ PyPI</a></li>
<li><a href="http://github.com/alisaifee/flask-limiter">Flask-Limiter @ github</a></li>
<li><a href="http://github.com/alisaifee/flask-limiter/issues">Issue Tracker</a></li>
</ul>
<!--Start of Zopim Live Chat Script-->
<script type="text/javascript">
window.$zopim||(function(d,s){var z=$zopim=function(c){z._.push(c)},$=z.s=
d.createElement(s),e=d.getElementsByTagName(s)[0];z.set=function(o){z.set.
_.push(o)};z._=[];z.set._=[];$.async=!0;$.setAttribute('charset','utf-8');
$.src='//v2.zopim.com/?1v0eqqnJr8gbT2CcrbTs0U9wWGlosnWN';z.t=+new Date;$.
type='text/javascript';e.parentNode.insertBefore($,e)})(document,'script');
</script>
<!--End of Zopim Live Chat Script-->
<h3><a href="#">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Flask-Limiter</a><ul>
<li><a class="reference internal" href="#usage">Usage</a><ul>
<li><a class="reference internal" href="#quick-start">Quick start</a></li>
<li><a class="reference internal" href="#the-flask-limiter-extension">The Flask-Limiter extension</a></li>
<li><a class="reference internal" href="#rate-limit-domain">Rate Limit Domain</a></li>
<li><a class="reference internal" href="#decorators">Decorators</a></li>
</ul>
</li>
<li><a class="reference internal" href="#configuration">Configuration</a></li>
<li><a class="reference internal" href="#rate-limit-string-notation">Rate limit string notation</a><ul>
<li><a class="reference internal" href="#examples">Examples</a></li>
</ul>
</li>
<li><a class="reference internal" href="#rate-limiting-strategies">Rate limiting strategies</a><ul>
<li><a class="reference internal" href="#fixed-window">Fixed Window</a></li>
<li><a class="reference internal" href="#fixed-window-with-elastic-expiry">Fixed Window with Elastic Expiry</a></li>
<li><a class="reference internal" href="#moving-window">Moving Window</a></li>
</ul>
</li>
<li><a class="reference internal" href="#rate-limiting-headers">Rate-limiting Headers</a></li>
<li><a class="reference internal" href="#recipes">Recipes</a><ul>
<li><a class="reference internal" href="#rate-limit-key-functions">Rate Limit Key Functions</a></li>
<li><a class="reference internal" href="#custom-rate-limit-exceeded-responses">Custom Rate limit exceeded responses</a></li>
<li><a class="reference internal" href="#using-flask-pluggable-views">Using Flask Pluggable Views</a></li>
<li><a class="reference internal" href="#rate-limiting-all-routes-in-a-flask-blueprint">Rate limiting all routes in a <code class="docutils literal"><span class="pre">flask.Blueprint</span></code></a></li>
<li><a class="reference internal" href="#logging">Logging</a></li>
<li><a class="reference internal" href="#custom-error-messages">Custom error messages</a></li>
</ul>
</li>
<li><a class="reference internal" href="#api">API</a><ul>
<li><a class="reference internal" href="#core">Core</a></li>
<li><a class="reference internal" href="#exceptions">Exceptions</a></li>
<li><a class="reference internal" href="#module-flask_limiter.util">Utils</a></li>
</ul>
</li>
<li><a class="reference internal" href="#changelog">Changelog</a><ul>
<li><a class="reference internal" href="#id3">0.9.3 2016-03-14</a></li>
<li><a class="reference internal" href="#id4">0.9.2 2016-03-04</a></li>
<li><a class="reference internal" href="#id5">0.9.1 2015-11-21</a></li>
<li><a class="reference internal" href="#id6">0.9 2015-11-13</a></li>
<li><a class="reference internal" href="#id7">0.8.5 2015-10-05</a></li>
<li><a class="reference internal" href="#id8">0.8.4 2015-10-03</a></li>
<li><a class="reference internal" href="#id9">0.8.2 2015-09-17</a></li>
<li><a class="reference internal" href="#id10">0.8.1 2015-08-06</a></li>
<li><a class="reference internal" href="#id11">0.8 2015-06-07</a></li>
<li><a class="reference internal" href="#id12">0.7.9 2015-04-02</a></li>
<li><a class="reference internal" href="#id13">0.7.8 2015-03-20</a></li>
<li><a class="reference internal" href="#id14">0.7.6 2015-03-02</a></li>
<li><a class="reference internal" href="#id15">0.7.5 2015-02-16</a></li>
<li><a class="reference internal" href="#id17">0.7.4 2015-02-03</a></li>
<li><a class="reference internal" href="#id18">0.7.3 2015-01-30</a></li>
<li><a class="reference internal" href="#id19">0.7.1 2015-01-09</a></li>
<li><a class="reference internal" href="#id20">0.7 2015-01-09</a></li>
<li><a class="reference internal" href="#id21">0.6.6 2014-10-21</a></li>
<li><a class="reference internal" href="#id22">0.6.5 2014-10-01</a></li>
<li><a class="reference internal" href="#id23">0.6.4 2014-08-31</a></li>
<li><a class="reference internal" href="#id24">0.6.3 2014-08-26</a></li>
<li><a class="reference internal" href="#id25">0.6.2 2014-07-13</a></li>
<li><a class="reference internal" href="#id26">0.6.1 2014-07-11</a></li>
<li><a class="reference internal" href="#id27">0.6 2014-06-24</a></li>
<li><a class="reference internal" href="#id28">0.5 2014-06-13</a></li>
<li><a class="reference internal" href="#id29">0.4.4 2014-06-13</a></li>
<li><a class="reference internal" href="#id30">0.4.3 2014-06-12</a></li>
<li><a class="reference internal" href="#id31">0.4.2 2014-06-12</a></li>
<li><a class="reference internal" href="#id32">0.4.1 2014-06-04</a></li>
<li><a class="reference internal" href="#id33">0.4 2014-05-28</a></li>
<li><a class="reference internal" href="#id35">0.3.2 2014-05-26</a></li>
<li><a class="reference internal" href="#id36">0.3.1 2014-02-20</a></li>
<li><a class="reference internal" href="#id37">0.3.0 2014-02-19</a></li>
<li><a class="reference internal" href="#id38">0.2.2 2014-02-18</a></li>
<li><a class="reference internal" href="#id39">0.2.1 2014-02-15</a></li>
<li><a class="reference internal" href="#id40">0.2 2014-02-15</a></li>
<li><a class="reference internal" href="#id41">0.1.1 2014-02-14</a></li>
<li><a class="reference internal" href="#id42">0.1.0 2014-02-13</a></li>
</ul>
</li>
<li><a class="reference internal" href="#references">References</a></li>
<li><a class="reference internal" href="#contributions">Contributions</a></li>
</ul>
</li>
</ul>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/index.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
©2017, Ali-Akber Saifee.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 1.4.9</a>
& <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.8</a>
|
<a href="_sources/index.txt"
rel="nofollow">Page source</a>
</div>
</body>
</html>
|