~unity-team/unity-scopes-shell/overview

« back to all changes in this revision

Viewing changes to tests/geoip.ubuntu.com.py

  • Committer: Michal Hruby
  • Date: 2014-07-31 11:08:50 UTC
  • mfrom: (108.2.4 unity-scopes-shell)
  • Revision ID: michal.mhr@gmail.com-20140731110850-41qkf5s8x8ugm3bj
Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: utf-8 -*-
 
3
#
 
4
# Copyright (C) 2014 Canonical Ltd
 
5
#
 
6
# This program is free software: you can redistribute it and/or modify
 
7
# it under the terms of the GNU Lesser General Public License version 3 as
 
8
# published by the Free Software Foundation.
 
9
#
 
10
# This program is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU Lesser General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU Lesser General Public License
 
16
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
#
 
18
# Authored by: Pete Woods <pete.woods@canonical.com>
 
19
 
 
20
import tornado.httpserver
 
21
import tornado.ioloop
 
22
import tornado.netutil
 
23
import tornado.web
 
24
import sys
 
25
 
 
26
RESPONSE = '''<Response>
 
27
<Ip>1.2.3.4</Ip>
 
28
<Status>OK</Status>
 
29
<CountryCode>GB</CountryCode>
 
30
<CountryCode3>GBR</CountryCode3>
 
31
<CountryName>United Kingdom</CountryName>
 
32
<RegionCode>H2</RegionCode>
 
33
<RegionName>Lancashire</RegionName>
 
34
<City>Accrington</City>
 
35
<ZipPostalCode>BB5</ZipPostalCode>
 
36
<Latitude>55.7654</Latitude>
 
37
<Longitude>-2.7467</Longitude>
 
38
<AreaCode>0</AreaCode>
 
39
<TimeZone>Europe/London</TimeZone>
 
40
</Response>
 
41
'''
 
42
 
 
43
class Lookup(tornado.web.RequestHandler):
 
44
    def get(self):
 
45
        sys.stderr.write('GeoIP location requested\n')
 
46
        sys.stderr.flush()
 
47
 
 
48
        self.write(RESPONSE)
 
49
        self.finish()
 
50
        
 
51
def new_app():
 
52
    application = tornado.web.Application([
 
53
        (r"/lookup", Lookup),
 
54
    ], gzip=True)
 
55
    sockets = tornado.netutil.bind_sockets(0, '127.0.0.1')
 
56
    server = tornado.httpserver.HTTPServer(application)
 
57
    server.add_sockets(sockets)
 
58
 
 
59
    sys.stdout.write('%d\n' % sockets[0].getsockname()[1])
 
60
    sys.stdout.flush()
 
61
 
 
62
    return application
 
63
 
 
64
if __name__ == "__main__":
 
65
    application = new_app()
 
66
    tornado.ioloop.IOLoop.instance().start()