考虑到女友的安全问题,就做了一个app实现定位和服务器实现转发的东西。刚学python,竟没想到用对象编程会更加方便,全程过程式开发,代码有点臃肿,就当学习下python吧.效果就是:在微信公众号中输入指定字符比如:”我要知道你的位置”,手机那端的位置就弹出来了.主要是讲一下思路:先是app实现定位,当微信发送消息时,消息从微信服务器转发到开发者服务器然后用socket发送指定消息来通知app,I need your location,app接收到消息之后再发送给开发服务器(app 开service实现后台全程运行),由于定位信息是经纬度,所以用了高德API,但是发现谷歌地球的定位是准的,可能和android内置的定位有关系吧,然后就转换了一下不同地图的经纬度,然后转成位置信息发送给微信服务器.

import socket
import threading
import os
import requests
from flask import Flask
from flask import request
from bs4 import BeautifulSoup
import json
global sock#实现通过微信控制手机app定位发送给服务器显示位置信息loca = "welcome"
app = Flask(__name__)#搭建web服务器通过socket发送消息给app索取定位信息,然后转发给微信服务器
@app.route("/wx_check",methods=["POST","GET"]) #这里用了一个Web框架   "/wx_check" 是你在微信中填的开发者服务器路径
def application():openID = request.args['openid'] # 微信发的,详见开发者文档soup = BeautifulSoup(request.data,"html.parser")content = soup.find("content") # content 是微信用户发的消息,可用来验证用户sock.send(b"getlocation") #  发送信息通知androidglobal locawhile True:  #手动阻塞if loca != "welcome":breakback = localoca = "welcome"return """<xml> <ToUserName>%s</ToUserName> <FromUserName>qqmsssssssss</FromUserName><CreateTime>12345678</CreateTime> <MsgType>text</MsgType> <Content>%s</Content> </xml>"""%(openID,back)
def start():app.run('0.0.0.0',80)
threading.Thread(target=start,args=()).start()# 与app进行socket连接 接受定位信息 另外用到经纬度兼容转换API 和经纬度转位置API
def tcplink(sock,addr):try:print('Accept new connection from %s:%s...' % addr)while True:sock.setblocking(True)data = sock.recv(1024)location = data.decode('utf-8')print("client:"+location)# 以下进行经纬度 地图信息的转换 loca为app所在地址接上面的 堵塞if location != "":global locaprint(location)lis = location.split(",")location = "%s,%s"%(lis[1],lis[0])print(location)xml = requests.get("http://api.gpsspg.com/convert/coord/?oid=xxxx&key=xxxxxxxxxxxxxxxxxx&from=0&to=3&latlng=%s&output=xml"%location)soup = BeautifulSoup(xml.text,"html.parser")print(soup.text)lat= soup.find("lat").stringlng= soup.find("lng").stringlocation = "%s,%s"%(lng,lat)print("after"+location)a = requests.get("http://restapi.amap.com/v3/geocode/regeo?key=xxxxxxxxxxxxxxxxx&location="+location)loca = a.textobj = json.loads(loca)loca = obj["regeocode"]["formatted_address"]else:print("socket is close,waiting new accept")sock.close()breakexcept Exception as e:location = "raise error"finally:passtry:s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)s.bind(('0.0.0.0',9999))s.listen(10)print('waiting to connect')while True:sock,addr = s.accept()   #等待app来连接t = threading.Thread(target=tcplink,args=(sock,addr))t.start()finally:print("ending")

以下是android端代码:

# 获取定位,其实就是获取经纬度
private Location getLastKnownLocation() {LocationManager mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE);List<String> providers = mLocationManager.getProviders(true);Location bestLocation = null;for (String provider : providers) {Location l = mLocationManager.getLastKnownLocation(provider);if (l == null) {continue;}if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {// Found best last known location: %s", l);bestLocation = l;}}return bestLocation;}String provider;public void GetLocation(){LocationManager mLocationManager;Location location = getLastKnownLocation();//        Log.d("TAG", provider.toString());Log.d("TAG", location.toString());if (location != null) {//获取当前位置,这里只用到了经纬度String string =location.getLongitude() + ","+ location.getLatitude();try {OutputStream outputStream = socket.getOutputStream();PrintWriter writer = new PrintWriter(outputStream);writer.write(string);writer.flush();
//              writer.close();
//              socket.shutdownOutput();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}@SuppressLint("ShowToast") public void Connect(){try {socket = new Socket();socket.connect(new InetSocketAddress("xxx.xxx.xxx.xxx",9999));while (true) {Log.d("TAG", socket.isConnected()+"");InputStream stream = socket.getInputStream();  byte[] b = new byte[11];stream.read(b);String sb = new String(b);if(sb.equalsIgnoreCase("getlocation")){GetLocation();}else{OutputStream outoStream = socket.getOutputStream();outoStream.write("error code".getBytes());socket.shutdownOutput();}}} catch (IOException e) {// TODO Auto-generated catch blockLog.d("TAG", "error");e.printStackTrace();}}

Python 通过微信控制实现app定位发送到个人服务器,再转发微信服务器接收位置信息。相关推荐

  1. Python 通过微信控制实现app定位发送到个人服务器再转发微信服务器接收位置信息

    import socket import threading import os import requests from flask import Flask from flask import r ...

  2. 微信控制树莓派运行python_Python+树莓派+人体红外感应模块 实现微信监控

    上一篇文章<Python+树莓派 实现微信拍照.摄相机器人>中简单的实现了由手机微信控制树莓派摄像头拍摄照片及录像,并将拍摄文件回传至微信的功能,程序比较简单, 今天要和大家分享的是在上一 ...

  3. 企业微信如何简单实现定时发送文件到群:企业微信群机器人操作(Java代码实现)

    前言 不知道小伙伴们的公司组织架构通勤用的啥软件,我公司用的企业微信.然后业务销售部那边需要每天统计销售数据报表然后发在群里(我是开发,我不配在群里).知道这个背景以后,产品给我们的需求是:直接统计数 ...

  4. 定位服务器的功能是提供用户位置信息和什么,LBS是什么意思 LBS的现有模式和功能介绍...

    LBS是什么意思?LBS有什么模式和功能?所谓的LBS的意思其实就是基于位置服务,所谓手机里的LBS软件就是很多lbs功能,比如我们常用的微博,大家都应该知道那个像小火柴一样的符号,点击后就会出现现在 ...

  5. 微信 发送图片 服务器上,公安提醒:微信发照片,千万别传原图”!5个步骤教你隐藏位置信息...

    原标题:公安提醒:微信发照片,千万别传"原图"!5个步骤教你隐藏位置信息 你与陌生人之间,可能只是一张照片的距离.有时候一张"原图"照片,分分钟就暴露了你的信息 ...

  6. 企业微信朋友圈这样定时发送!太省事了!

    ​我们在使用企业微信进行客户运营时,除了可以给客户或客户群发送消息,还可以使用企业微信朋友圈功能,通过发朋友圈的形式将产品或活动进行展示. 那么企业微信的朋友圈可以定时发送吗?今天和企业微信服务商小编 ...

  7. 单反相机照片用python 脚本添加gps位置信息

    现在的单反相机基本都不带gps,如果要给照片添加gps信息,需要一直开着官方的app,非常耗电,出去拍照,可能忘记打开app,或者手机电量不够都可能导致照片没有位置信息. 个人整理照片的时候,喜欢在手 ...

  8. 微信小程序获取用户位置信息并显示到地图上

    1.配置地理位置用途说明 在app.json中的大括号内输入 "permission": {"scope.userLocation": {"desc& ...

  9. 获取手机或电脑GPS位置信息(定位平台)

    获取GPS定位平台 原理: javascript访问手机GPS位置信息 链接: http://14.18.103.46:8000/location/index (电信某云无法直接开启80端口,坑 ̄へ ̄ ...

最新文章

  1. 第十六届全国大学智能车竞赛创意组比赛国赛获奖名单
  2. MySQL连接问题【如何解决MySQL连接超时关闭】
  3. Linux-网络配置
  4. java正则表达式非贪婪_正则表达式中的贪婪与非贪婪匹配模式
  5. python 武沛齐_武沛齐 - 主页
  6. 1.14 关于梯度检验实现的注记
  7. Jmeter系列之简介与环境安装
  8. 360浏览器升级_360安全卫士下载|360安全卫士 12.0 最新版
  9. html页面ajax提交数据,ajax请求提交form表单
  10. Vue图片上传删除预览操作
  11. remote Incorrect username or password ( access token ):用户名或密码不正确(访问令牌)
  12. [Swift]LeetCode311. 稀疏矩阵相乘 $ Sparse Matrix Multiplication
  13. Visio高级使用技巧
  14. 暗月渗透实战靶场-项目七(下)
  15. 10个值得推荐的免费设计模板网站
  16. 如何站在巨人的肩膀上学习
  17. 实习期间的一些思考整理(5)2018.4.17~4.18
  18. java登录界面圆形头像,Android使用CircleImageView实现圆形头像的方法
  19. 51单片机教程 :(一) 开发环境的搭建
  20. 2022国自然中标至少1篇1区代表作?没中接下来怎么办?

热门文章

  1. ag-Grid 超丰富的表格插件(1)——简易使用
  2. 李宏毅机器学习之Deep Learning简介
  3. 跟我一起数据挖掘(16)——R中的线性回归
  4. 千锋Unity学习笔记
  5. MOGRT替换视频,图像,照片及LOGO?如何替换PR动态图形模板中的图片视频素材
  6. 计算机考研分数403,总分403分过来人分享成功考研经验_跨考网
  7. 整理程序员常用英语单词1695个
  8. 用 Pandas 读写网页中的 HTML 表格数据
  9. 【专题5: 硬件设计】 之 【9.案例一:门控开关,制作BOM表和成本核算】
  10. 网络编程培训之六 使用原始套接字实现Ping