├── .gitignore ├── LICENSE ├── MANIFEST.in ├── README ├── docs ├── Makefile ├── _static │ └── itsdangerous.png ├── _themes │ ├── .gitignore │ ├── LICENSE │ ├── README │ ├── flask_small │ │ ├── layout.html │ │ ├── static │ │ │ └── flasky.css_t │ │ └── theme.conf │ └── flask_theme_support.py ├── conf.py ├── index.rst └── make.bat ├── geoip.py ├── geolite2 ├── MANIFEST.in ├── VERSION ├── _geoip_geolite2 │ └── __init__.py ├── download.sh └── setup.py ├── setup.cfg └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | dist 3 | build 4 | *.egg-info 5 | *.mmdb 6 | docs/_build 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 by Armin Ronacher, see AUTHORS for more details. 2 | 3 | Some rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are 7 | met: 8 | 9 | * Redistributions of source code must retain the above copyright 10 | notice, this list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above 13 | copyright notice, this list of conditions and the following 14 | disclaimer in the documentation and/or other materials provided 15 | with the distribution. 16 | 17 | * The names of the contributors may not be used to endorse or 18 | promote products derived from this software without specific 19 | prior written permission. 20 | 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include README LICENSE setup.cfg 2 | recursive-include _geoip_geolite2 * 3 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | python-geoip 2 | ============ 3 | 4 | python-geoip is a library that provides access to GeoIP databases. 5 | Currently it only supports accessing MaxMind databases. It's similar to 6 | other GeoIP libraries but comes under the very liberal BSD license and 7 | also provides an extra library that optionally ships a recent version of 8 | the Geolite2 database as provided by MaxMind. 9 | 10 | Documentation is available at http://pythonhosted.org/python-geoip/ 11 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | PAPER = 8 | BUILDDIR = _build 9 | 10 | # Internal variables. 11 | PAPEROPT_a4 = -D latex_paper_size=a4 12 | PAPEROPT_letter = -D latex_paper_size=letter 13 | ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 14 | 15 | .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest 16 | 17 | help: 18 | @echo "Please use \`make ' where is one of" 19 | @echo " html to make standalone HTML files" 20 | @echo " dirhtml to make HTML files named index.html in directories" 21 | @echo " singlehtml to make a single large HTML file" 22 | @echo " pickle to make pickle files" 23 | @echo " json to make JSON files" 24 | @echo " htmlhelp to make HTML files and a HTML help project" 25 | @echo " qthelp to make HTML files and a qthelp project" 26 | @echo " devhelp to make HTML files and a Devhelp project" 27 | @echo " epub to make an epub" 28 | @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" 29 | @echo " latexpdf to make LaTeX files and run them through pdflatex" 30 | @echo " text to make text files" 31 | @echo " man to make manual pages" 32 | @echo " changes to make an overview of all changed/added/deprecated items" 33 | @echo " linkcheck to check all external links for integrity" 34 | @echo " doctest to run all doctests embedded in the documentation (if enabled)" 35 | 36 | clean: 37 | -rm -rf $(BUILDDIR)/* 38 | 39 | html: 40 | $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html 41 | @echo 42 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." 43 | 44 | dirhtml: 45 | $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml 46 | @echo 47 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." 48 | 49 | singlehtml: 50 | $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml 51 | @echo 52 | @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." 53 | 54 | pickle: 55 | $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle 56 | @echo 57 | @echo "Build finished; now you can process the pickle files." 58 | 59 | json: 60 | $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json 61 | @echo 62 | @echo "Build finished; now you can process the JSON files." 63 | 64 | htmlhelp: 65 | $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp 66 | @echo 67 | @echo "Build finished; now you can run HTML Help Workshop with the" \ 68 | ".hhp project file in $(BUILDDIR)/htmlhelp." 69 | 70 | qthelp: 71 | $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp 72 | @echo 73 | @echo "Build finished; now you can run "qcollectiongenerator" with the" \ 74 | ".qhcp project file in $(BUILDDIR)/qthelp, like this:" 75 | @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/Classy.qhcp" 76 | @echo "To view the help file:" 77 | @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/Classy.qhc" 78 | 79 | devhelp: 80 | $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp 81 | @echo 82 | @echo "Build finished." 83 | @echo "To view the help file:" 84 | @echo "# mkdir -p $$HOME/.local/share/devhelp/Classy" 85 | @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/Classy" 86 | @echo "# devhelp" 87 | 88 | epub: 89 | $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub 90 | @echo 91 | @echo "Build finished. The epub file is in $(BUILDDIR)/epub." 92 | 93 | latex: 94 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 95 | @echo 96 | @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." 97 | @echo "Run \`make all-pdf' or \`make all-ps' in that directory to" \ 98 | "run these through (pdf)latex." 99 | 100 | latexpdf: latex 101 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 102 | @echo "Running LaTeX files through pdflatex..." 103 | make -C $(BUILDDIR)/latex all-pdf 104 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 105 | 106 | text: 107 | $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text 108 | @echo 109 | @echo "Build finished. The text files are in $(BUILDDIR)/text." 110 | 111 | man: 112 | $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man 113 | @echo 114 | @echo "Build finished. The manual pages are in $(BUILDDIR)/man." 115 | 116 | changes: 117 | $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes 118 | @echo 119 | @echo "The overview file is in $(BUILDDIR)/changes." 120 | 121 | linkcheck: 122 | $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck 123 | @echo 124 | @echo "Link check complete; look for any errors in the above output " \ 125 | "or in $(BUILDDIR)/linkcheck/output.txt." 126 | 127 | doctest: 128 | $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest 129 | @echo "Testing of doctests in the sources finished, look at the " \ 130 | "results in $(BUILDDIR)/doctest/output.txt." 131 | -------------------------------------------------------------------------------- /docs/_static/itsdangerous.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mitsuhiko/python-geoip/80b888b18cccaf57ab830befe1fc530e818c4d0a/docs/_static/itsdangerous.png -------------------------------------------------------------------------------- /docs/_themes/.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | *.pyo 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /docs/_themes/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010 by Armin Ronacher. 2 | 3 | Some rights reserved. 4 | 5 | Redistribution and use in source and binary forms of the theme, with or 6 | without modification, are permitted provided that the following conditions 7 | are met: 8 | 9 | * Redistributions of source code must retain the above copyright 10 | notice, this list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above 13 | copyright notice, this list of conditions and the following 14 | disclaimer in the documentation and/or other materials provided 15 | with the distribution. 16 | 17 | * The names of the contributors may not be used to endorse or 18 | promote products derived from this software without specific 19 | prior written permission. 20 | 21 | We kindly ask you to only use these themes in an unmodified manner just 22 | for Flask and Flask-related products, not for unrelated projects. If you 23 | like the visual style and want to use it for your own projects, please 24 | consider making some larger changes to the themes (such as changing 25 | font faces, sizes, colors or margins). 26 | 27 | THIS THEME IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 28 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 31 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 | ARISING IN ANY WAY OUT OF THE USE OF THIS THEME, EVEN IF ADVISED OF THE 37 | POSSIBILITY OF SUCH DAMAGE. 38 | -------------------------------------------------------------------------------- /docs/_themes/README: -------------------------------------------------------------------------------- 1 | Flask Sphinx Styles 2 | =================== 3 | 4 | This repository contains sphinx styles for Flask and Flask related 5 | projects. To use this style in your Sphinx documentation, follow 6 | this guide: 7 | 8 | 1. put this folder as _themes into your docs folder. Alternatively 9 | you can also use git submodules to check out the contents there. 10 | 2. add this to your conf.py: 11 | 12 | sys.path.append(os.path.abspath('_themes')) 13 | html_theme_path = ['_themes'] 14 | html_theme = 'flask' 15 | 16 | The following themes exist: 17 | 18 | - 'flask' - the standard flask documentation theme for large 19 | projects 20 | - 'flask_small' - small one-page theme. Intended to be used by 21 | very small addon libraries for flask. 22 | 23 | The following options exist for the flask_small theme: 24 | 25 | [options] 26 | index_logo = '' filename of a picture in _static 27 | to be used as replacement for the 28 | h1 in the index.rst file. 29 | index_logo_height = 120px height of the index logo 30 | github_fork = '' repository name on github for the 31 | "fork me" badge 32 | -------------------------------------------------------------------------------- /docs/_themes/flask_small/layout.html: -------------------------------------------------------------------------------- 1 | {% extends "basic/layout.html" %} 2 | {% block header %} 3 | 6 | {{ super() }} 7 | {% if pagename == 'index' %} 8 |
9 | {% endif %} 10 | {% endblock %} 11 | {% block footer %} 12 | {% if pagename == 'index' %} 13 |
14 | {% endif %} 15 | {% endblock %} 16 | {# do not display relbars #} 17 | {% block relbar1 %}{% endblock %} 18 | {% block relbar2 %} 19 | {% if theme_github_fork %} 20 | Fork me on GitHub 22 | {% endif %} 23 | {% endblock %} 24 | {% block sidebar1 %}{% endblock %} 25 | {% block sidebar2 %}{% endblock %} 26 | -------------------------------------------------------------------------------- /docs/_themes/flask_small/static/flasky.css_t: -------------------------------------------------------------------------------- 1 | /* 2 | * flasky.css_t 3 | * ~~~~~~~~~~~~ 4 | * 5 | * Sphinx stylesheet -- flasky theme based on nature theme. 6 | * 7 | * :copyright: Copyright 2007-2010 by the Sphinx team, see AUTHORS. 8 | * :license: BSD, see LICENSE for details. 9 | * 10 | */ 11 | 12 | @import url("basic.css"); 13 | @import url(http://fonts.googleapis.com/css?family=Lato:light,regular); 14 | 15 | /* -- page layout ----------------------------------------------------------- */ 16 | 17 | body { 18 | font-family: 'Ubuntu', sans-serif; 19 | font-weight: 300; 20 | font-size: 17px; 21 | color: #000; 22 | background: white; 23 | margin: 0; 24 | padding: 0; 25 | } 26 | 27 | div.documentwrapper { 28 | float: left; 29 | width: 100%; 30 | } 31 | 32 | div.bodywrapper { 33 | margin: 40px auto 0 auto; 34 | width: 700px; 35 | } 36 | 37 | hr { 38 | border: 1px solid #B1B4B6; 39 | } 40 | 41 | div.body { 42 | background-color: #ffffff; 43 | color: #3E4349; 44 | padding: 0 30px 30px 30px; 45 | } 46 | 47 | img.floatingflask { 48 | padding: 0 0 10px 10px; 49 | float: right; 50 | } 51 | 52 | div.footer { 53 | text-align: right; 54 | color: #888; 55 | padding: 10px; 56 | font-size: 14px; 57 | width: 650px; 58 | margin: 0 auto 40px auto; 59 | } 60 | 61 | div.footer a { 62 | color: #888; 63 | text-decoration: underline; 64 | } 65 | 66 | div.related { 67 | line-height: 32px; 68 | color: #888; 69 | } 70 | 71 | div.related ul { 72 | padding: 0 0 0 10px; 73 | } 74 | 75 | div.related a { 76 | color: #444; 77 | } 78 | 79 | /* -- body styles ----------------------------------------------------------- */ 80 | 81 | a { 82 | color: #215974; 83 | text-decoration: underline; 84 | } 85 | 86 | a:hover { 87 | color: #888; 88 | text-decoration: underline; 89 | } 90 | 91 | div.body { 92 | padding-bottom: 40px; /* saved for footer */ 93 | } 94 | 95 | div.body h1, 96 | div.body h2, 97 | div.body h3, 98 | div.body h4, 99 | div.body h5, 100 | div.body h6 { 101 | font-family: 'Lato', sans-serif; 102 | font-weight: 300; 103 | margin: 30px 0px 10px 0px; 104 | padding: 0; 105 | color: black; 106 | } 107 | 108 | {% if theme_index_logo %} 109 | div.indexwrapper h1 { 110 | text-indent: -999999px; 111 | background: url({{ theme_index_logo }}) no-repeat center center; 112 | height: {{ theme_index_logo_height }}; 113 | } 114 | {% endif %} 115 | 116 | div.body h2 { font-size: 180%; } 117 | div.body h3 { font-size: 150%; } 118 | div.body h4 { font-size: 130%; } 119 | div.body h5 { font-size: 100%; } 120 | div.body h6 { font-size: 100%; } 121 | 122 | a.headerlink { 123 | color: white; 124 | padding: 0 4px; 125 | text-decoration: none; 126 | } 127 | 128 | a.headerlink:hover { 129 | color: #444; 130 | background: #eaeaea; 131 | } 132 | 133 | div.body p, div.body dd, div.body li { 134 | line-height: 1.4em; 135 | } 136 | 137 | div.admonition { 138 | background: #fafafa; 139 | margin: 20px -30px; 140 | padding: 10px 30px; 141 | border-top: 1px solid #ccc; 142 | border-bottom: 1px solid #ccc; 143 | } 144 | 145 | div.admonition p.admonition-title { 146 | font-family: 'Garamond', 'Georgia', serif; 147 | font-weight: normal; 148 | font-size: 24px; 149 | margin: 0 0 10px 0; 150 | padding: 0; 151 | line-height: 1; 152 | } 153 | 154 | div.admonition p.last { 155 | margin-bottom: 0; 156 | } 157 | 158 | div.highlight{ 159 | background-color: white; 160 | } 161 | 162 | dt:target, .highlight { 163 | background: #FAF3E8; 164 | } 165 | 166 | div.note { 167 | background-color: #eee; 168 | border: 1px solid #ccc; 169 | } 170 | 171 | div.seealso { 172 | background-color: #ffc; 173 | border: 1px solid #ff6; 174 | } 175 | 176 | div.topic { 177 | background-color: #eee; 178 | } 179 | 180 | div.warning { 181 | background-color: #ffe4e4; 182 | border: 1px solid #f66; 183 | } 184 | 185 | p.admonition-title { 186 | display: inline; 187 | } 188 | 189 | p.admonition-title:after { 190 | content: ":"; 191 | } 192 | 193 | pre, tt { 194 | font-family: 'Consolas', 'Menlo', 'Deja Vu Sans Mono', 'Bitstream Vera Sans Mono', monospace; 195 | font-size: 0.85em; 196 | } 197 | 198 | img.screenshot { 199 | } 200 | 201 | tt.descname, tt.descclassname { 202 | font-size: 0.95em; 203 | } 204 | 205 | tt.descname { 206 | padding-right: 0.08em; 207 | } 208 | 209 | img.screenshot { 210 | -moz-box-shadow: 2px 2px 4px #eee; 211 | -webkit-box-shadow: 2px 2px 4px #eee; 212 | box-shadow: 2px 2px 4px #eee; 213 | } 214 | 215 | table.docutils { 216 | border: 1px solid #888; 217 | -moz-box-shadow: 2px 2px 4px #eee; 218 | -webkit-box-shadow: 2px 2px 4px #eee; 219 | box-shadow: 2px 2px 4px #eee; 220 | } 221 | 222 | table.docutils td, table.docutils th { 223 | border: 1px solid #888; 224 | padding: 0.25em 0.7em; 225 | } 226 | 227 | table.field-list, table.footnote { 228 | border: none; 229 | -moz-box-shadow: none; 230 | -webkit-box-shadow: none; 231 | box-shadow: none; 232 | } 233 | 234 | table.footnote { 235 | margin: 15px 0; 236 | width: 100%; 237 | border: 1px solid #eee; 238 | } 239 | 240 | table.field-list th { 241 | padding: 0 0.8em 0 0; 242 | } 243 | 244 | table.field-list td { 245 | padding: 0; 246 | } 247 | 248 | table.footnote td { 249 | padding: 0.5em; 250 | } 251 | 252 | dl { 253 | margin: 0; 254 | padding: 0; 255 | } 256 | 257 | dl dd { 258 | margin-left: 30px; 259 | } 260 | 261 | pre { 262 | padding: 0; 263 | margin: 15px -30px; 264 | padding: 8px; 265 | line-height: 1.3em; 266 | padding: 7px 30px; 267 | background: #eee; 268 | border-radius: 2px; 269 | -moz-border-radius: 2px; 270 | -webkit-border-radius: 2px; 271 | } 272 | 273 | dl pre { 274 | margin-left: -60px; 275 | padding-left: 60px; 276 | } 277 | 278 | tt { 279 | background-color: #ecf0f3; 280 | color: #222; 281 | /* padding: 1px 2px; */ 282 | } 283 | 284 | tt.xref, a tt { 285 | background-color: #FBFBFB; 286 | } 287 | 288 | a:hover tt { 289 | background: #EEE; 290 | } 291 | -------------------------------------------------------------------------------- /docs/_themes/flask_small/theme.conf: -------------------------------------------------------------------------------- 1 | [theme] 2 | inherit = basic 3 | stylesheet = flasky.css 4 | nosidebar = true 5 | pygments_style = flask_theme_support.FlaskyStyle 6 | 7 | [options] 8 | index_logo = '' 9 | index_logo_height = 160px 10 | github_fork = 11 | -------------------------------------------------------------------------------- /docs/_themes/flask_theme_support.py: -------------------------------------------------------------------------------- 1 | # flasky extensions. flasky pygments style based on tango style 2 | from pygments.style import Style 3 | from pygments.token import Keyword, Name, Comment, String, Error, \ 4 | Number, Operator, Generic, Whitespace, Punctuation, Other, Literal 5 | 6 | 7 | class FlaskyStyle(Style): 8 | background_color = "#f8f8f8" 9 | default_style = "" 10 | 11 | styles = { 12 | # No corresponding class for the following: 13 | #Text: "", # class: '' 14 | Whitespace: "underline #f8f8f8", # class: 'w' 15 | Error: "#a40000 border:#ef2929", # class: 'err' 16 | Other: "#000000", # class 'x' 17 | 18 | Comment: "italic #8f5902", # class: 'c' 19 | Comment.Preproc: "noitalic", # class: 'cp' 20 | 21 | Keyword: "bold #004461", # class: 'k' 22 | Keyword.Constant: "bold #004461", # class: 'kc' 23 | Keyword.Declaration: "bold #004461", # class: 'kd' 24 | Keyword.Namespace: "bold #004461", # class: 'kn' 25 | Keyword.Pseudo: "bold #004461", # class: 'kp' 26 | Keyword.Reserved: "bold #004461", # class: 'kr' 27 | Keyword.Type: "bold #004461", # class: 'kt' 28 | 29 | Operator: "#582800", # class: 'o' 30 | Operator.Word: "bold #004461", # class: 'ow' - like keywords 31 | 32 | Punctuation: "bold #000000", # class: 'p' 33 | 34 | # because special names such as Name.Class, Name.Function, etc. 35 | # are not recognized as such later in the parsing, we choose them 36 | # to look the same as ordinary variables. 37 | Name: "#000000", # class: 'n' 38 | Name.Attribute: "#c4a000", # class: 'na' - to be revised 39 | Name.Builtin: "#004461", # class: 'nb' 40 | Name.Builtin.Pseudo: "#3465a4", # class: 'bp' 41 | Name.Class: "#000000", # class: 'nc' - to be revised 42 | Name.Constant: "#000000", # class: 'no' - to be revised 43 | Name.Decorator: "#888", # class: 'nd' - to be revised 44 | Name.Entity: "#ce5c00", # class: 'ni' 45 | Name.Exception: "bold #cc0000", # class: 'ne' 46 | Name.Function: "#000000", # class: 'nf' 47 | Name.Property: "#000000", # class: 'py' 48 | Name.Label: "#f57900", # class: 'nl' 49 | Name.Namespace: "#000000", # class: 'nn' - to be revised 50 | Name.Other: "#000000", # class: 'nx' 51 | Name.Tag: "bold #004461", # class: 'nt' - like a keyword 52 | Name.Variable: "#000000", # class: 'nv' - to be revised 53 | Name.Variable.Class: "#000000", # class: 'vc' - to be revised 54 | Name.Variable.Global: "#000000", # class: 'vg' - to be revised 55 | Name.Variable.Instance: "#000000", # class: 'vi' - to be revised 56 | 57 | Number: "#990000", # class: 'm' 58 | 59 | Literal: "#000000", # class: 'l' 60 | Literal.Date: "#000000", # class: 'ld' 61 | 62 | String: "#4e9a06", # class: 's' 63 | String.Backtick: "#4e9a06", # class: 'sb' 64 | String.Char: "#4e9a06", # class: 'sc' 65 | String.Doc: "italic #8f5902", # class: 'sd' - like a comment 66 | String.Double: "#4e9a06", # class: 's2' 67 | String.Escape: "#4e9a06", # class: 'se' 68 | String.Heredoc: "#4e9a06", # class: 'sh' 69 | String.Interpol: "#4e9a06", # class: 'si' 70 | String.Other: "#4e9a06", # class: 'sx' 71 | String.Regex: "#4e9a06", # class: 'sr' 72 | String.Single: "#4e9a06", # class: 's1' 73 | String.Symbol: "#4e9a06", # class: 'ss' 74 | 75 | Generic: "#000000", # class: 'g' 76 | Generic.Deleted: "#a40000", # class: 'gd' 77 | Generic.Emph: "italic #000000", # class: 'ge' 78 | Generic.Error: "#ef2929", # class: 'gr' 79 | Generic.Heading: "bold #000080", # class: 'gh' 80 | Generic.Inserted: "#00A000", # class: 'gi' 81 | Generic.Output: "#888", # class: 'go' 82 | Generic.Prompt: "#745334", # class: 'gp' 83 | Generic.Strong: "bold #000000", # class: 'gs' 84 | Generic.Subheading: "bold #800080", # class: 'gu' 85 | Generic.Traceback: "bold #a40000", # class: 'gt' 86 | } 87 | -------------------------------------------------------------------------------- /docs/conf.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # python-geoip documentation build configuration file, created by 4 | # sphinx-quickstart on Mon Apr 26 19:53:01 2010. 5 | # 6 | # This file is execfile()d with the current directory set to its containing dir. 7 | # 8 | # Note that not all possible configuration values are present in this 9 | # autogenerated file. 10 | # 11 | # All configuration values have a default; values that are commented out 12 | # serve to show the default. 13 | 14 | import sys, os 15 | 16 | # If extensions (or modules to document with autodoc) are in another directory, 17 | # add these directories to sys.path here. If the directory is relative to the 18 | # documentation root, use os.path.abspath to make it absolute, like shown here. 19 | sys.path.append(os.path.abspath('_themes')) 20 | sys.path.append(os.path.abspath('..')) 21 | 22 | # -- General configuration ----------------------------------------------------- 23 | 24 | # If your documentation needs a minimal Sphinx version, state it here. 25 | #needs_sphinx = '1.0' 26 | 27 | # Add any Sphinx extension module names here, as strings. They can be extensions 28 | # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. 29 | extensions = ['sphinx.ext.autodoc', 'sphinx.ext.intersphinx'] 30 | 31 | # Add any paths that contain templates here, relative to this directory. 32 | templates_path = ['_templates'] 33 | 34 | # The suffix of source filenames. 35 | source_suffix = '.rst' 36 | 37 | # The encoding of source files. 38 | #source_encoding = 'utf-8-sig' 39 | 40 | # The master toctree document. 41 | master_doc = 'index' 42 | 43 | # General information about the project. 44 | project = u'python-geoip' 45 | copyright = u'2014, Armin Ronacher' 46 | 47 | # The version info for the project you're documenting, acts as replacement for 48 | # |version| and |release|, also used in various other places throughout the 49 | # built documents. 50 | # 51 | # The short X.Y version. 52 | version = '1.0' 53 | # The full version, including alpha/beta/rc tags. 54 | release = '1.0' 55 | 56 | # The language for content autogenerated by Sphinx. Refer to documentation 57 | # for a list of supported languages. 58 | #language = None 59 | 60 | # There are two options for replacing |today|: either, you set today to some 61 | # non-false value, then it is used: 62 | #today = '' 63 | # Else, today_fmt is used as the format for a strftime call. 64 | #today_fmt = '%B %d, %Y' 65 | 66 | # List of patterns, relative to source directory, that match files and 67 | # directories to ignore when looking for source files. 68 | exclude_patterns = ['_build'] 69 | 70 | # The reST default role (used for this markup: `text`) to use for all documents. 71 | #default_role = None 72 | 73 | # If true, '()' will be appended to :func: etc. cross-reference text. 74 | #add_function_parentheses = True 75 | 76 | # If true, the current module name will be prepended to all description 77 | # unit titles (such as .. function::). 78 | #add_module_names = True 79 | 80 | # If true, sectionauthor and moduleauthor directives will be shown in the 81 | # output. They are ignored by default. 82 | #show_authors = False 83 | 84 | # A list of ignored prefixes for module index sorting. 85 | #modindex_common_prefix = [] 86 | 87 | 88 | # -- Options for HTML output --------------------------------------------------- 89 | 90 | # The theme to use for HTML and HTML Help pages. Major themes that come with 91 | # Sphinx are currently 'default' and 'sphinxdoc'. 92 | html_theme = 'flask_small' 93 | 94 | # Theme options are theme-specific and customize the look and feel of a theme 95 | # further. For a list of options available for each theme, see the 96 | # documentation. 97 | html_theme_options = { 98 | 'index_logo': None, 99 | 'github_fork': 'mitsuhiko/python-geoip' 100 | } 101 | 102 | # Add any paths that contain custom themes here, relative to this directory. 103 | html_theme_path = ['_themes'] 104 | 105 | # The name for this set of Sphinx documents. If None, it defaults to 106 | # " v documentation". 107 | html_title = 'python-geoip' 108 | 109 | # A shorter title for the navigation bar. Default is the same as html_title. 110 | #html_short_title = None 111 | 112 | # The name of an image file (relative to this directory) to place at the top 113 | # of the sidebar. 114 | #html_logo = None 115 | 116 | # The name of an image file (within the static path) to use as favicon of the 117 | # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 118 | # pixels large. 119 | #html_favicon = None 120 | 121 | # Add any paths that contain custom static files (such as style sheets) here, 122 | # relative to this directory. They are copied after the builtin static files, 123 | # so a file named "default.css" will overwrite the builtin "default.css". 124 | html_static_path = ['_static'] 125 | 126 | # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, 127 | # using the given strftime format. 128 | #html_last_updated_fmt = '%b %d, %Y' 129 | 130 | # If true, SmartyPants will be used to convert quotes and dashes to 131 | # typographically correct entities. 132 | #html_use_smartypants = True 133 | 134 | # Custom sidebar templates, maps document names to template names. 135 | #html_sidebars = {} 136 | 137 | # Additional templates that should be rendered to pages, maps page names to 138 | # template names. 139 | #html_additional_pages = {} 140 | 141 | # If false, no module index is generated. 142 | #html_domain_indices = True 143 | 144 | # If false, no index is generated. 145 | #html_use_index = True 146 | 147 | # If true, the index is split into individual pages for each letter. 148 | #html_split_index = False 149 | 150 | # If true, links to the reST sources are added to the pages. 151 | #html_show_sourcelink = True 152 | 153 | # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. 154 | #html_show_sphinx = True 155 | 156 | # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. 157 | #html_show_copyright = True 158 | 159 | # If true, an OpenSearch description file will be output, and all pages will 160 | # contain a tag referring to it. The value of this option must be the 161 | # base URL from which the finished HTML is served. 162 | #html_use_opensearch = '' 163 | 164 | # If nonempty, this is the file name suffix for HTML files (e.g. ".xhtml"). 165 | #html_file_suffix = '' 166 | 167 | # Output file base name for HTML help builder. 168 | htmlhelp_basename = 'python-geoipdoc' 169 | 170 | 171 | # -- Options for LaTeX output -------------------------------------------------- 172 | 173 | # The paper size ('letter' or 'a4'). 174 | #latex_paper_size = 'letter' 175 | 176 | # The font size ('10pt', '11pt' or '12pt'). 177 | #latex_font_size = '10pt' 178 | 179 | # Grouping the document tree into LaTeX files. List of tuples 180 | # (source start file, target name, title, author, documentclass [howto/manual]). 181 | latex_documents = [ 182 | ('index', 'python-geoip.tex', u'python-geoip documentation', 183 | u'Armin Ronacher', 'manual'), 184 | ] 185 | 186 | # The name of an image file (relative to this directory) to place at the top of 187 | # the title page. 188 | #latex_logo = None 189 | 190 | # For "manual" documents, if this is true, then toplevel headings are parts, 191 | # not chapters. 192 | #latex_use_parts = False 193 | 194 | # Additional stuff for the LaTeX preamble. 195 | #latex_preamble = '' 196 | 197 | # Documents to append as an appendix to all manuals. 198 | #latex_appendices = [] 199 | 200 | # If false, no module index is generated. 201 | #latex_domain_indices = True 202 | 203 | 204 | # -- Options for manual page output -------------------------------------------- 205 | 206 | # One entry per manual page. List of tuples 207 | # (source start file, name, description, authors, manual section). 208 | man_pages = [ 209 | ('index', 'python-geoip', u'python-geoip documentation', 210 | [u'Armin Ronacher'], 1) 211 | ] 212 | 213 | intersphinx_mapping = { 214 | 'http://docs.python.org/dev': None 215 | } 216 | -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | python-geoip 2 | ============ 3 | 4 | .. module:: geoip 5 | 6 | python-geoip is a library that provides access to GeoIP databases. 7 | Currently it only supports accessing MaxMind databases. It's similar to 8 | other GeoIP libraries but comes under the very liberal BSD license and 9 | also provides an extra library that optionally ships a recent version of 10 | the Geolite2 database as provided by MaxMind. 11 | 12 | The ``python-geoip-geolite2`` package includes GeoLite2 data created by 13 | MaxMind, available from `maxmind.com `_ under 14 | the Creative Commons Attribution-ShareAlike 3.0 Unported License. 15 | 16 | Installation 17 | ------------ 18 | 19 | You can get the library directly from PyPI:: 20 | 21 | pip install python-geoip 22 | 23 | If you also want the free MaxMind Geolite2 database you can in addition:: 24 | 25 | pip install python-geoip-geolite2 26 | 27 | Usage 28 | ----- 29 | 30 | If you have installed the ``python-geoip-geolite2`` package you can start 31 | using the GeoIP database right away:: 32 | 33 | >>> from geoip import geolite2 34 | >>> match = geolite2.lookup('17.0.0.1') 35 | >>> match is not None 36 | True 37 | >>> match.country 38 | 'US' 39 | >>> match.continent 40 | 'NA' 41 | >>> match.timezone 42 | 'America/Los_Angeles' 43 | >>> match.subdivisions 44 | frozenset(['CA']) 45 | 46 | If you want to use your own MaxMind database (for instance because you 47 | paid for the commercial version) you can open the database yourself:: 48 | 49 | >>> from geoip import open_database 50 | >>> db = open_database('path/to/my.mmdb') 51 | 52 | API 53 | --- 54 | 55 | .. autofunction:: open_database 56 | 57 | .. autodata:: geolite2 58 | 59 | .. autoclass:: Database 60 | :members: 61 | 62 | .. autoclass:: IPInfo 63 | :members: 64 | 65 | .. autoclass:: DatabaseInfo() 66 | :members: 67 | -------------------------------------------------------------------------------- /docs/make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | REM Command file for Sphinx documentation 4 | 5 | if "%SPHINXBUILD%" == "" ( 6 | set SPHINXBUILD=sphinx-build 7 | ) 8 | set BUILDDIR=_build 9 | set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . 10 | if NOT "%PAPER%" == "" ( 11 | set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% 12 | ) 13 | 14 | if "%1" == "" goto help 15 | 16 | if "%1" == "help" ( 17 | :help 18 | echo.Please use `make ^` where ^ is one of 19 | echo. html to make standalone HTML files 20 | echo. dirhtml to make HTML files named index.html in directories 21 | echo. singlehtml to make a single large HTML file 22 | echo. pickle to make pickle files 23 | echo. json to make JSON files 24 | echo. htmlhelp to make HTML files and a HTML help project 25 | echo. qthelp to make HTML files and a qthelp project 26 | echo. devhelp to make HTML files and a Devhelp project 27 | echo. epub to make an epub 28 | echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter 29 | echo. text to make text files 30 | echo. man to make manual pages 31 | echo. changes to make an overview over all changed/added/deprecated items 32 | echo. linkcheck to check all external links for integrity 33 | echo. doctest to run all doctests embedded in the documentation if enabled 34 | goto end 35 | ) 36 | 37 | if "%1" == "clean" ( 38 | for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i 39 | del /q /s %BUILDDIR%\* 40 | goto end 41 | ) 42 | 43 | if "%1" == "html" ( 44 | %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html 45 | echo. 46 | echo.Build finished. The HTML pages are in %BUILDDIR%/html. 47 | goto end 48 | ) 49 | 50 | if "%1" == "dirhtml" ( 51 | %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml 52 | echo. 53 | echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. 54 | goto end 55 | ) 56 | 57 | if "%1" == "singlehtml" ( 58 | %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml 59 | echo. 60 | echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. 61 | goto end 62 | ) 63 | 64 | if "%1" == "pickle" ( 65 | %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle 66 | echo. 67 | echo.Build finished; now you can process the pickle files. 68 | goto end 69 | ) 70 | 71 | if "%1" == "json" ( 72 | %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json 73 | echo. 74 | echo.Build finished; now you can process the JSON files. 75 | goto end 76 | ) 77 | 78 | if "%1" == "htmlhelp" ( 79 | %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp 80 | echo. 81 | echo.Build finished; now you can run HTML Help Workshop with the ^ 82 | .hhp project file in %BUILDDIR%/htmlhelp. 83 | goto end 84 | ) 85 | 86 | if "%1" == "qthelp" ( 87 | %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp 88 | echo. 89 | echo.Build finished; now you can run "qcollectiongenerator" with the ^ 90 | .qhcp project file in %BUILDDIR%/qthelp, like this: 91 | echo.^> qcollectiongenerator %BUILDDIR%\qthelp\Classy.qhcp 92 | echo.To view the help file: 93 | echo.^> assistant -collectionFile %BUILDDIR%\qthelp\Classy.ghc 94 | goto end 95 | ) 96 | 97 | if "%1" == "devhelp" ( 98 | %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp 99 | echo. 100 | echo.Build finished. 101 | goto end 102 | ) 103 | 104 | if "%1" == "epub" ( 105 | %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub 106 | echo. 107 | echo.Build finished. The epub file is in %BUILDDIR%/epub. 108 | goto end 109 | ) 110 | 111 | if "%1" == "latex" ( 112 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 113 | echo. 114 | echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. 115 | goto end 116 | ) 117 | 118 | if "%1" == "text" ( 119 | %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text 120 | echo. 121 | echo.Build finished. The text files are in %BUILDDIR%/text. 122 | goto end 123 | ) 124 | 125 | if "%1" == "man" ( 126 | %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man 127 | echo. 128 | echo.Build finished. The manual pages are in %BUILDDIR%/man. 129 | goto end 130 | ) 131 | 132 | if "%1" == "changes" ( 133 | %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes 134 | echo. 135 | echo.The overview file is in %BUILDDIR%/changes. 136 | goto end 137 | ) 138 | 139 | if "%1" == "linkcheck" ( 140 | %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck 141 | echo. 142 | echo.Link check complete; look for any errors in the above output ^ 143 | or in %BUILDDIR%/linkcheck/output.txt. 144 | goto end 145 | ) 146 | 147 | if "%1" == "doctest" ( 148 | %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest 149 | echo. 150 | echo.Testing of doctests in the sources finished, look at the ^ 151 | results in %BUILDDIR%/doctest/output.txt. 152 | goto end 153 | ) 154 | 155 | :end 156 | -------------------------------------------------------------------------------- /geoip.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import mmap 3 | import socket 4 | import urllib 5 | 6 | from threading import Lock 7 | from datetime import datetime 8 | from struct import Struct 9 | 10 | 11 | MMDB_METADATA_START = '\xAB\xCD\xEFMaxMind.com' 12 | MMDB_METADATA_BLOCK_MAX_SIZE = 131072 13 | MMDB_DATA_SECTION_SEPARATOR = 16 14 | 15 | _int_unpack = Struct('>I').unpack 16 | _long_unpack = Struct('>Q').unpack 17 | _short_unpack = Struct('>H').unpack 18 | 19 | 20 | def _native_str(x): 21 | """Attempts to coerce a string into native if it's ASCII safe.""" 22 | try: 23 | return str(x) 24 | except UnicodeError: 25 | return x 26 | 27 | 28 | def pack_ip(ip): 29 | """Given an IP string, converts it into packed format for internal 30 | usage. 31 | """ 32 | for fmly in socket.AF_INET, socket.AF_INET6: 33 | try: 34 | return socket.inet_pton(fmly, ip) 35 | except socket.error: 36 | continue 37 | raise ValueError('Malformed IP address') 38 | 39 | 40 | class DatabaseInfo(object): 41 | """Provides information about the GeoIP database.""" 42 | 43 | def __init__(self, filename=None, date=None, 44 | internal_name=None, provider=None): 45 | #: If available the filename which backs the database. 46 | self.filename = filename 47 | #: Optionally the build date of the database as datetime object. 48 | self.date = date 49 | #: Optionally the internal name of the database. 50 | self.internal_name = internal_name 51 | #: Optionally the name of the database provider. 52 | self.provider = provider 53 | 54 | def __repr__(self): 55 | return '<%s filename=%r date=%r internal_name=%r provider=%r>' % ( 56 | self.__class__.__name__, 57 | self.filename, 58 | self.date, 59 | self.internal_name, 60 | self.provider, 61 | ) 62 | 63 | 64 | class IPInfo(object): 65 | """Provides information about the located IP as returned by 66 | :meth:`Database.lookup`. 67 | """ 68 | __slots__ = ('ip', '_data') 69 | 70 | def __init__(self, ip, data): 71 | #: The IP that was looked up. 72 | self.ip = ip 73 | self._data = data 74 | 75 | @property 76 | def country(self): 77 | """The country code as ISO code if available.""" 78 | if 'country' in self._data: 79 | return _native_str(self._data['country']['iso_code']) 80 | 81 | @property 82 | def continent(self): 83 | """The continent as ISO code if available.""" 84 | if 'continent' in self._data: 85 | return _native_str(self._data['continent']['code']) 86 | 87 | @property 88 | def subdivisions(self): 89 | """The subdivisions as a list of ISO codes as an immutable set.""" 90 | return frozenset(_native_str(x['iso_code']) for x in 91 | self._data.get('subdivisions') or () if 'iso_code' 92 | in x) 93 | 94 | @property 95 | def timezone(self): 96 | """The timezone if available as tzinfo name.""" 97 | if 'location' in self._data: 98 | return _native_str(self._data['location'].get('time_zone')) 99 | 100 | @property 101 | def location(self): 102 | """The location as ``(lat, long)`` tuple if available.""" 103 | if 'location' in self._data: 104 | lat = self._data['location'].get('latitude') 105 | long = self._data['location'].get('longitude') 106 | if lat is not None and long is not None: 107 | return lat, long 108 | 109 | def to_dict(self): 110 | """A dict representation of the available information. This 111 | is a dictionary with the same keys as the attributes of this 112 | object. 113 | """ 114 | return { 115 | 'ip': self.ip, 116 | 'country': self.country, 117 | 'continent': self.continent, 118 | 'subdivisions': self.subdivisions, 119 | 'timezone': self.timezone, 120 | 'location': self.location, 121 | } 122 | 123 | def get_info_dict(self): 124 | """Returns the internal info dictionary. For a maxmind database 125 | this is the metadata dictionary. 126 | """ 127 | return self._data 128 | 129 | def __hash__(self): 130 | return hash(self.addr) 131 | 132 | def __eq__(self, other): 133 | return type(self) is type(other) and self.addr == other.addr 134 | 135 | def __ne__(self, other): 136 | return not self.__eq__(other) 137 | 138 | def __repr__(self): 139 | return ('') % ( 141 | self.ip, 142 | self.country, 143 | self.continent, 144 | self.subdivisions, 145 | self.timezone, 146 | self.location, 147 | ) 148 | 149 | 150 | class Database(object): 151 | """Provides access to a GeoIP database. This is an abstract class 152 | that is implemented by different providers. The :func:`open_database` 153 | function can be used to open a MaxMind database. 154 | 155 | Example usage:: 156 | 157 | from geoip import open_database 158 | 159 | with open_database('data/GeoLite2-City.mmdb') as db: 160 | match = db.lookup_mine() 161 | print 'My IP info:', match 162 | """ 163 | 164 | def __init__(self): 165 | self.closed = False 166 | 167 | def __enter__(self): 168 | return self 169 | 170 | def __exit__(self, exc_type, exc_value, tb): 171 | self.close() 172 | 173 | def close(self): 174 | """Closes the database. The whole object can also be used as a 175 | context manager. Databases that are packaged up (such as the 176 | :data:`geolite2` database) do not need to be closed. 177 | """ 178 | self.closed = True 179 | 180 | def get_info(self): 181 | """Returns an info object about the database. This can be used to 182 | check for the build date of the database or what provides the GeoIP 183 | data. 184 | 185 | :rtype: :class:`DatabaseInfo` 186 | """ 187 | raise NotImplementedError('This database does not provide info') 188 | 189 | def get_metadata(self): 190 | """Return the metadata dictionary of the loaded database. This 191 | dictionary is specific to the database provider. 192 | """ 193 | raise NotImplementedError('This database does not provide metadata') 194 | 195 | def lookup(self, ip_addr): 196 | """Looks up the IP information in the database and returns a 197 | :class:`IPInfo`. If it does not exist, `None` is returned. What 198 | IP addresses are supported is specific to the GeoIP provider. 199 | 200 | :rtype: :class:`IPInfo` 201 | """ 202 | if self.closed: 203 | raise RuntimeError('Database is closed.') 204 | return self._lookup(ip_addr) 205 | 206 | def lookup_mine(self): 207 | """Looks up the computer's IP by asking a web service and then 208 | checks the database for a match. 209 | 210 | :rtype: :class:`IPInfo` 211 | """ 212 | ip = urllib.urlopen('http://icanhazip.com/').read().strip() 213 | return self.lookup(ip) 214 | 215 | 216 | class MaxMindDatabase(Database): 217 | """Provides access to a maxmind database.""" 218 | 219 | def __init__(self, filename, buf, md): 220 | Database.__init__(self) 221 | self.filename = filename 222 | self.is_ipv6 = md['ip_version'] == 6 223 | self.nodes = md['node_count'] 224 | self.record_size = md['record_size'] 225 | self.node_size = self.record_size / 4 226 | self.db_size = self.nodes * self.node_size 227 | 228 | self._buf = buf 229 | self._md = md 230 | self._reader = _MaxMindParser(buf, self.db_size) 231 | self._ipv4_start = None 232 | 233 | def close(self): 234 | Database.close(self) 235 | self._buf.close() 236 | 237 | def get_metadata(self): 238 | return self._md 239 | 240 | def get_info(self): 241 | return DatabaseInfo( 242 | filename=self.filename, 243 | date=datetime.utcfromtimestamp(self._md['build_epoch']), 244 | internal_name=_native_str(self._md['database_type']), 245 | provider='maxmind', 246 | ) 247 | 248 | def _lookup(self, ip_addr): 249 | packed_addr = pack_ip(ip_addr) 250 | bits = len(packed_addr) * 8 251 | 252 | node = self._find_start_node(bits) 253 | 254 | seen = set() 255 | for i in xrange(bits): 256 | if node >= self.nodes: 257 | break 258 | bit = (ord(packed_addr[i >> 3]) >> (7 - (i % 8))) & 1 259 | node = self._parse_node(node, bit) 260 | if node in seen: 261 | raise LookupError('Circle in tree detected') 262 | seen.add(node) 263 | 264 | if node > self.nodes: 265 | offset = node - self.nodes + self.db_size 266 | return IPInfo(ip_addr, self._reader.read(offset)[0]) 267 | 268 | def _find_start_node(self, bits): 269 | if bits == 128 or not self.is_ipv6: 270 | return 0 271 | 272 | if self._ipv4_start is not None: 273 | return self._ipv4_start 274 | 275 | # XXX: technically the next code is racy if used concurrently but 276 | # the worst thing that can happen is that the ipv4 start node is 277 | # calculated multiple times. 278 | node = 0 279 | for netmask in xrange(96): 280 | if node >= self.nodes: 281 | break 282 | node = self._parse_node(netmask, 0) 283 | 284 | self._ipv4_start = node 285 | return node 286 | 287 | def _parse_node(self, node, index): 288 | offset = node * self.node_size 289 | 290 | if self.record_size == 24: 291 | offset += index * 3 292 | bytes = '\x00' + self._buf[offset:offset + 3] 293 | elif self.record_size == 28: 294 | b = ord(self._buf[offset + 3:offset + 4]) 295 | if index: 296 | b &= 0x0F 297 | else: 298 | b = (0xF0 & b) >> 4 299 | offset += index * 4 300 | bytes = chr(b) + self._buf[offset:offset + 3] 301 | elif self.record_size == 32: 302 | offset += index * 4 303 | bytes = self._buf[offset:offset + 4] 304 | else: 305 | raise LookupError('Invalid record size') 306 | return _int_unpack(bytes)[0] 307 | 308 | def __repr__(self): 309 | return '<%s %r>' % ( 310 | self.__class__.__name__, 311 | self.filename, 312 | ) 313 | 314 | 315 | class PackagedDatabase(Database): 316 | """Provides access to a packaged database. Upon first usage the 317 | system will import the provided package and invoke the ``loader`` 318 | function to construct the actual database object. 319 | 320 | This is used for instance to implement the ``geolite2`` database 321 | that is provided. 322 | """ 323 | 324 | def __init__(self, name, package, pypi_name=None): 325 | Database.__init__(self) 326 | self.name = name 327 | self.package = package 328 | self.pypi_name = pypi_name 329 | self._lock = Lock() 330 | self._db = None 331 | 332 | def _load_database(self): 333 | try: 334 | mod = __import__(self.package, None, None, ['loader']) 335 | except ImportError: 336 | msg = 'Cannot use packaged database "%s" ' \ 337 | 'because package "%s" is not available.' % (self.name, 338 | self.package) 339 | if self.pypi_name is not None: 340 | msg += ' It\'s provided by PyPI package "%s"' % self.pypi_name 341 | raise RuntimeError(msg) 342 | return mod.loader(self, sys.modules[__name__]) 343 | 344 | def _get_actual_db(self): 345 | if self._db is not None: 346 | return self._db 347 | with self._lock: 348 | if self._db is not None: 349 | return self._db 350 | rv = self._load_database() 351 | self._db = rv 352 | return rv 353 | 354 | def close(self): 355 | pass 356 | 357 | def get_info(self): 358 | return self._get_actual_db().get_info() 359 | 360 | def get_metadata(self): 361 | return self._get_actual_db().get_metadata() 362 | 363 | def lookup(self, ip_addr): 364 | return self._get_actual_db().lookup(ip_addr) 365 | 366 | def __repr__(self): 367 | return '<%s %r>' % ( 368 | self.__class__.__name__, 369 | self.name, 370 | ) 371 | 372 | 373 | #: Provides access to the geolite2 cities database. In order to use this 374 | #: database the ``python-geoip-geolite2`` package needs to be installed. 375 | geolite2 = PackagedDatabase('geolite2', '_geoip_geolite2', 376 | pypi_name='python-geoip-geolite2') 377 | 378 | 379 | def _read_mmdb_metadata(buf): 380 | """Reads metadata from a given memory mapped buffer.""" 381 | offset = buf.rfind(MMDB_METADATA_START, 382 | buf.size() - MMDB_METADATA_BLOCK_MAX_SIZE) 383 | if offset < 0: 384 | raise ValueError('Could not find metadata') 385 | offset += len(MMDB_METADATA_START) 386 | return _MaxMindParser(buf, offset).read(offset)[0] 387 | 388 | 389 | def make_struct_parser(code): 390 | struct = Struct('>' + code) 391 | def unpack_func(self, size, offset): 392 | new_offset = offset + struct.size 393 | bytes = self._buf[offset:new_offset].rjust(struct.size, '\x00') 394 | value = struct.unpack(bytes)[0] 395 | return value, new_offset 396 | return unpack_func 397 | 398 | 399 | class _MaxMindParser(object): 400 | 401 | def __init__(self, buf, data_offset=0): 402 | self._buf = buf 403 | self._data_offset = data_offset 404 | 405 | def _parse_ptr(self, size, offset): 406 | ptr_size = ((size >> 3) & 0x3) + 1 407 | bytes = self._buf[offset:offset + ptr_size] 408 | if ptr_size != 4: 409 | bytes = chr(size & 0x7) + bytes 410 | 411 | ptr = ( 412 | _int_unpack(bytes.rjust(4, '\x00'))[0] + 413 | self._data_offset + 414 | MMDB_DATA_SECTION_SEPARATOR + 415 | (0, 2048, 526336, 0)[ptr_size - 1] 416 | ) 417 | 418 | return self.read(ptr)[0], offset + ptr_size 419 | 420 | def _parse_str(self, size, offset): 421 | bytes = self._buf[offset:offset + size] 422 | return bytes.decode('utf-8', 'replace'), offset + size 423 | 424 | _parse_double = make_struct_parser('d') 425 | 426 | def _parse_bytes(self, size, offset): 427 | return self._buf[offset:offset + size], offset + size 428 | 429 | def _parse_uint(self, size, offset): 430 | bytes = self._buf[offset:offset + size] 431 | return _long_unpack(bytes.rjust(8, '\x00'))[0], offset + size 432 | 433 | def _parse_dict(self, size, offset): 434 | container = {} 435 | for _ in xrange(size): 436 | key, offset = self.read(offset) 437 | value, offset = self.read(offset) 438 | container[key] = value 439 | return container, offset 440 | 441 | _parse_int32 = make_struct_parser('i') 442 | 443 | def _parse_list(self, size, offset): 444 | rv = [None] * size 445 | for idx in xrange(size): 446 | rv[idx], offset = self.read(offset) 447 | return rv, offset 448 | 449 | def _parse_error(self, size, offset): 450 | raise AssertionError('Read invalid type code') 451 | 452 | def _parse_bool(self, size, offset): 453 | return size != 0, offset 454 | 455 | _parse_float = make_struct_parser('f') 456 | 457 | _callbacks = ( 458 | _parse_error, # 0 459 | _parse_ptr, # 1 pointer 460 | _parse_str, # 2 utf-8 string 461 | _parse_double, # 3 double 462 | _parse_bytes, # 4 bytes 463 | _parse_uint, # 5 uint16 464 | _parse_uint, # 6 uint32 465 | _parse_dict, # 7 map 466 | _parse_int32, # 8 int32 467 | _parse_uint, # 9 uint64 468 | _parse_uint, # 10 uint128 469 | _parse_list, # 11 array 470 | _parse_error, # 12 471 | _parse_error, # 13 472 | _parse_bool, # 14 boolean 473 | _parse_float, # 15 float 474 | ) 475 | 476 | def read(self, offset): 477 | new_offset = offset + 1 478 | byte = ord(self._buf[offset:new_offset]) 479 | size = byte & 0x1f 480 | ty = byte >> 5 481 | 482 | if ty == 0: 483 | byte = ord(self._buf[new_offset:new_offset + 1]) 484 | ty = byte + 7 485 | new_offset += 1 486 | 487 | if ty != 1 and size >= 29: 488 | to_read = size - 28 489 | bytes = self._buf[new_offset:new_offset + to_read] 490 | new_offset += to_read 491 | if size == 29: 492 | size = 29 + ord(bytes) 493 | elif size == 30: 494 | size = 285 + _short_unpack(bytes)[0] 495 | elif size > 30: 496 | size = 65821 + _int_unpack(bytes.rjust(4, '\x00'))[0] 497 | 498 | return self._callbacks[ty](self, size, new_offset) 499 | 500 | 501 | def open_database(filename): 502 | """Open a given database. This currently only supports maxmind 503 | databases (mmdb). If the file cannot be opened an ``IOError`` is 504 | raised. 505 | """ 506 | with open(filename, 'rb') as f: 507 | buf = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) 508 | md = _read_mmdb_metadata(buf) 509 | return MaxMindDatabase(filename, buf, md) 510 | -------------------------------------------------------------------------------- /geolite2/MANIFEST.in: -------------------------------------------------------------------------------- 1 | include VERSION 2 | recursive-include _geoip_geolite2 * 3 | -------------------------------------------------------------------------------- /geolite2/VERSION: -------------------------------------------------------------------------------- 1 | 2014.0207 2 | -------------------------------------------------------------------------------- /geolite2/_geoip_geolite2/__init__.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | 4 | database_name = 'GeoLite2-City.mmdb' 5 | 6 | 7 | def loader(database, mod): 8 | filename = os.path.join(os.path.dirname(__file__), database_name) 9 | return mod.open_database(filename) 10 | -------------------------------------------------------------------------------- /geolite2/download.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | cd _geoip_geolite2 4 | rm *.mmdb 5 | wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz 6 | gunzip "GeoLite2-City.mmdb.gz" 7 | cd .. 8 | 9 | export PYTHONPATH=`pwd`:`pwd`/.. 10 | python -c "if 1: 11 | from geoip import geolite2; 12 | info = geolite2.get_info() 13 | print info.date.strftime('%Y.%m%d') 14 | " > VERSION 15 | -------------------------------------------------------------------------------- /geolite2/setup.py: -------------------------------------------------------------------------------- 1 | import os 2 | from setuptools import setup 3 | 4 | with open(os.path.join(os.path.dirname(__file__), 5 | 'VERSION')) as f: 6 | version = f.read().strip() 7 | 8 | 9 | setup( 10 | name='python-geoip-geolite2', 11 | author='Armin Ronacher', 12 | author_email='armin.ronacher@active-4.com', 13 | version=version, 14 | url='http://github.com/mitsuhiko/python-geoip', 15 | packages=['_geoip_geolite2'], 16 | description='Provides access to the geolite2 database. This product ' 17 | 'includes GeoLite2 data created by MaxMind, available from ' 18 | 'http://www.maxmind.com/', 19 | install_requires=['python-geoip'], 20 | include_package_data=True, 21 | zip_safe=False, 22 | classifiers=[ 23 | 'Programming Language :: Python', 24 | ], 25 | ) 26 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [upload_docs] 2 | upload-dir = docs/_build/html 3 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import os 2 | from setuptools import setup 3 | 4 | 5 | with open(os.path.join(os.path.dirname(__file__), 6 | 'README')) as f: 7 | readme = f.read().strip() 8 | 9 | 10 | setup( 11 | name='python-geoip', 12 | author='Armin Ronacher', 13 | author_email='armin.ronacher@active-4.com', 14 | version='1.2', 15 | url='http://github.com/mitsuhiko/python-geoip', 16 | long_description=readme, 17 | description='Provides GeoIP functionality for Python.', 18 | py_modules=['geoip'], 19 | zip_safe=False, 20 | classifiers=[ 21 | 'Programming Language :: Python', 22 | 'License :: OSI Approved :: BSD License', 23 | ], 24 | ) 25 | --------------------------------------------------------------------------------