| 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 |
from base64 import decode |
| 26 |
from email.utils import getaddresses |
| 27 |
from time import sleep |
| 28 |
from traceback import print_tb |
| 29 |
from urllib.request import urlopen |
| 30 |
import requests |
| 31 |
import json |
| 32 |
import re |
| 33 |
|
| 34 |
import dbus |
| 35 |
import dbus.service |
| 36 |
from dbus.mainloop.glib import DBusGMainLoop |
| 37 |
from gi.repository import GLib |
| 38 |
|
| 39 |
import logging |
| 40 |
|
| 41 |
logging.basicConfig(filename='/var/log/kysdk-location.log', |
| 42 |
level=logging.DEBUG, |
| 43 |
format='%(asctime)s.%(msecs)03d - %(name)s - %(levelname)s - %(message)s', |
| 44 |
datefmt='%Y-%m-%d %H:%M:%S') |
| 45 |
loggre = logging.getLogger() |
| 46 |
|
| 47 |
class AddressBus(dbus.service.Object): |
| 48 |
|
| 49 |
def __init__(self, conn=None, object_path=None): |
| 50 |
dbus.service.Object.__init__(self, conn, object_path) |
| 51 |
self._last_input = None |
| 52 |
|
| 53 |
def getaddr(self): |
| 54 |
text = str(urlopen("http://txt.go.sohu.com/ip/soip").read()) |
| 55 |
ip = re.findall(r'\d+.\d+.\d+.\d+',text) |
| 56 |
return ip[0] |
| 57 |
|
| 58 |
@dbus.service.method('com.kysdk.location', out_signature='s') |
| 59 |
def getAddress(self): |
| 60 |
url = "http://api.map.baidu.com/location/ip" |
| 61 |
myParams = {"ak":"4DPAeozisfSCUgSLuC14Ux4iXjeEYNGa","ip":self.getaddr(),"coor":"bd09ll"} |
| 62 |
res = requests.get(url=url,params=myParams) |
| 63 |
if res.status_code != 200: |
| 64 |
return "Error Code" + res.status_code |
| 65 |
decoded_u = res.text.encode('utf-8').decode('unicode_escape') |
| 66 |
data = json.loads(decoded_u) |
| 67 |
if data["status"] == 0 : |
| 68 |
return decoded_u |
| 69 |
else: |
| 70 |
return data["message"] |
| 71 |
|
| 72 |
if __name__ == '__main__' : |
| 73 |
try: |
| 74 |
loggre.info("location start") |
| 75 |
|
| 76 |
Bus主循环 |
| 77 |
DBusGMainLoop(set_as_default=True) |
| 78 |
|
| 79 |
system_bus = dbus.SystemBus() |
| 80 |
name = dbus.service.BusName("com.kysdk.location",system_bus) |
| 81 |
object_path = "/com/kysdk/location" |
| 82 |
obj = AddressBus(system_bus, object_path) |
| 83 |
loggre.info("location started") |
| 84 |
|
| 85 |
s主循环 |
| 86 |
mainloop = GLib.MainLoop() |
| 87 |
mainloop.run() |
| 88 |
except Exception as e: |
| 89 |
|
| 90 |
loggre.info(e) |