Skip to content

Instantly share code, notes, and snippets.

@whym
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whym/3f8ea6fdf345fc457449 to your computer and use it in GitHub Desktop.
Save whym/3f8ea6fdf345fc457449 to your computer and use it in GitHub Desktop.
whoisa gateway
Whois gateway https://gist.github.com/whym/3f8ea6fdf345fc457449
url.rewrite-once = (
"/whois/([0-9A-Fa-f\:\.]*)/redirect/(\w*)/?$" => "/whois/gateway.py?ip=$1&provider=$2",
"/whois/([0-9A-Fa-f\:\.]*)/lookup/?$" => "/whois/gateway.py?ip=$1&provider=$2&lookup=true",
"/whois/([0-9A-Fa-f\:\.]*)/lookup/json/?$" => "/whois/gateway.py?ip=$1&provider=$2&lookup=true&format=json",
"/whois/([0-9A-Fa-f\:\.]*)/?$" => "/whois/gateway.py?ip=$1"
)
#! /usr/bin/env python
import sys
sys.path.insert(0, '/data/project/whois/local/lib/python2.7/site-packages')
import cgitb
import cgi
from ipwhois import IPWhois
import json
if __name__ == '__main__':
providers = {
"ARIN": lambda x: "http://whois.arin.net/rest/ip/" + x,
"RIPE": lambda x: "https://apps.db.ripe.net/search/query.html?searchtext=%s#resultsAnchor" % x,
"APNIC": lambda x: "http://wq.apnic.net/apnic-bin/whois.pl?searchtext=" + x,
"LACNIC": lambda x: "http://lacnic.net/cgi-bin/lacnic/whois?lg=EN&query=" + x
}
cgitb.enable(display=0, logdir="/data/project/whois/logs")
form = cgi.FieldStorage()
ip = form.getfirst("ip", "")
provider = form.getfirst("provider", "").upper()
fmt = form.getfirst("format", "html").lower()
doLookup = form.getfirst("lookup", "")
result = ''
if doLookup != '':
query = IPWhois(ip)
result = query.lookup()
if providers.has_key(provider):
print "Location: %s" % providers[provider](ip)
print ""
exit()
if fmt == 'json' and doLookup:
print "Content-type: text/plain"
print ""
print result
exit()
print "Content-type: text/html"
print ""
print '''
<style type="text/css">
li a { display: inline-block; padding: .2em 0; font: normal normal sans-serif; }
#result { border: 1px solid; }
</style>
<title>Whois Gateway</title>
<body>
<h1>Whois Gateway</h1>
'''
if doLookup:
print '<div id="result"><pre>%s</pre></div>' % json.dumps(result, indent=4)
print '''
<h2>External links</h2>
<ul>
'''
for (name,q) in sorted(providers.items()):
print '<li><a href="%s"><em>%s</em>@%s</a></li>' % (q(ip), ip, name)
print "</ul></body>"
print '''
<h2>Usage</h2>
<dl><dt>http://tools.wmflabs.org/whois/IPADDRESS/lookup</dt>
<dd>Whois result</dd>
<dt>http://tools.wmflabs.org/whois/IPADDRESS</dt>
<dd>List of links to regional databases</dd>
<dt>http://tools.wmflabs.org/whois/IPADDRESS/redirect/NAME</dt>
<dd>Redirect to a search result page provided by NAME.<dd>
</dl>
<p><strong>This tool is expereimental. The URL and functionalities are not stable.</strong></p>
'''

Copyright (c) 2014, Yusuke Matsubara All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  • Neither the name of the {organization} nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment