添加UDP工具类
This commit is contained in:
46
util/UDP_Receive.py
Normal file
46
util/UDP_Receive.py
Normal file
@ -0,0 +1,46 @@
|
||||
#!/usr/bin/python3
|
||||
# coding= utf-8
|
||||
|
||||
import socket
|
||||
import sys
|
||||
import struct
|
||||
|
||||
# 本机信息
|
||||
host_ip = socket.gethostbyname(socket.gethostname())
|
||||
# 组播组IP和端口
|
||||
mcast_group_ip = '239.255.255.252'
|
||||
mcast_group_port = 5678
|
||||
|
||||
|
||||
def receiver():
|
||||
# 建立接收 udp socket
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||
# linux能绑定网卡这里绑定组播IP地址不会服错,windows没法绑定网卡只能绑定本网卡IP地址
|
||||
if "linux" in sys.platform:
|
||||
# 绑定到的网卡名,如果自己的不是eth0则修改
|
||||
nic_name = 0
|
||||
# 监听的组播地址
|
||||
sock.setsockopt(socket.SOL_SOCKET, 25, nic_name)
|
||||
sock.bind((mcast_group_ip, mcast_group_port))
|
||||
else:
|
||||
sock.bind((host_ip, mcast_group_port))
|
||||
# 加入组播组
|
||||
mq_request = struct.pack("=4sl", socket.inet_aton(mcast_group_ip), socket.INADDR_ANY)
|
||||
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mq_request)
|
||||
# 设置非阻塞
|
||||
sock.setblocking(True)
|
||||
while True:
|
||||
try:
|
||||
data, address = sock.recvfrom(4096)
|
||||
except socket.error as e:
|
||||
print(f"while receive message error occur:{e}")
|
||||
else:
|
||||
print("Receive Data!")
|
||||
print("FROM: ", address)
|
||||
print("DATA: ", data.decode('utf-8'))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
receiver()
|
32
util/UDP_Sender.py
Normal file
32
util/UDP_Sender.py
Normal file
@ -0,0 +1,32 @@
|
||||
#!/usr/bin/python3
|
||||
# coding= utf-8
|
||||
|
||||
import time
|
||||
import struct
|
||||
import socket
|
||||
|
||||
# 本机信息
|
||||
host_ip = socket.gethostname()
|
||||
host_port = 6501
|
||||
# 组播组IP和端口
|
||||
mcast_group_ip = '239.255.255.252'
|
||||
mcast_group_port = 5678
|
||||
|
||||
|
||||
def sender():
|
||||
send_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
||||
send_sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||
send_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
send_sock.bind((host_ip, host_port))
|
||||
# 设置存活时长
|
||||
ttl_bin = struct.pack('@i', 255)
|
||||
send_sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl_bin)
|
||||
while True:
|
||||
data = '12345 english 汉字#测试'
|
||||
send_sock.sendto(str(data).encode('utf-8'), (mcast_group_ip, mcast_group_port))
|
||||
print(f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}: send finish.')
|
||||
time.sleep(10)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sender()
|
81
util/find_GPS_image.py
Normal file
81
util/find_GPS_image.py
Normal file
@ -0,0 +1,81 @@
|
||||
import exifread
|
||||
import re
|
||||
import json
|
||||
import requests
|
||||
|
||||
def latitude_and_longitude_convert_to_decimal_system(*arg):
|
||||
"""
|
||||
经纬度转为小数, param arg:
|
||||
:return: 十进制小数
|
||||
"""
|
||||
return float(arg[0]) + ((float(arg[1]) + (float(arg[2].split('/')[0]) / float(arg[2].split('/')[-1]) / 60)) / 60)
|
||||
|
||||
def find_GPS_image(pic_path):
|
||||
GPS = {}
|
||||
date = ''
|
||||
with open(pic_path, 'rb') as f:
|
||||
tags = exifread.process_file(f)
|
||||
for tag, value in tags.items():
|
||||
if re.match('GPS GPSLatitudeRef', tag):
|
||||
GPS['GPSLatitudeRef'] = str(value)
|
||||
elif re.match('GPS GPSLongitudeRef', tag):
|
||||
GPS['GPSLongitudeRef'] = str(value)
|
||||
elif re.match('GPS GPSAltitudeRef', tag):
|
||||
GPS['GPSAltitudeRef'] = str(value)
|
||||
elif re.match('GPS GPSLatitude', tag):
|
||||
try:
|
||||
match_result = re.match('\[(\w*),(\w*),(\w.*)/(\w.*)\]', str(value)).groups()
|
||||
GPS['GPSLatitude'] = int(match_result[0]), int(match_result[1]), int(match_result[2])
|
||||
except:
|
||||
deg, min, sec = [x.replace(' ', '') for x in str(value)[1:-1].split(',')]
|
||||
GPS['GPSLatitude'] = latitude_and_longitude_convert_to_decimal_system(deg, min, sec)
|
||||
elif re.match('GPS GPSLongitude', tag):
|
||||
try:
|
||||
match_result = re.match('\[(\w*),(\w*),(\w.*)/(\w.*)\]', str(value)).groups()
|
||||
GPS['GPSLongitude'] = int(match_result[0]), int(match_result[1]), int(match_result[2])
|
||||
except:
|
||||
deg, min, sec = [x.replace(' ', '') for x in str(value)[1:-1].split(',')]
|
||||
GPS['GPSLongitude'] = latitude_and_longitude_convert_to_decimal_system(deg, min, sec)
|
||||
elif re.match('GPS GPSAltitude', tag):
|
||||
GPS['GPSAltitude'] = str(value)
|
||||
elif re.match('.*Date.*', tag):
|
||||
date = str(value)
|
||||
return {'GPS_information': GPS, 'date_information': date}
|
||||
|
||||
def find_address_from_GPS(GPS):
|
||||
"""
|
||||
使用Geocoding API把经纬度坐标转换为结构化地址。
|
||||
:param GPS:
|
||||
:return:
|
||||
"""
|
||||
secret_key = 'zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf'
|
||||
if not GPS['GPS_information']:
|
||||
return '该照片无GPS信息'
|
||||
lat, lng = GPS['GPS_information']['GPSLatitude'], GPS['GPS_information']['GPSLongitude']
|
||||
baidu_map_api = "http://api.map.baidu.com/geocoder/v2/?ak={0}&callback=renderReverse&location={1},{2}s&output=json&pois=0".format(
|
||||
secret_key, lat, lng)
|
||||
response = requests.get(baidu_map_api)
|
||||
content = response.text.replace("renderReverse&&renderReverse(", "")[:-1]
|
||||
baidu_map_address = json.loads(content)
|
||||
formatted_address = baidu_map_address["result"]["formatted_address"]
|
||||
province = baidu_map_address["result"]["addressComponent"]["province"]
|
||||
city = baidu_map_address["result"]["addressComponent"]["city"]
|
||||
district = baidu_map_address["result"]["addressComponent"]["district"]
|
||||
return formatted_address,province,city,district
|
||||
|
||||
pic_path = 'D:\pythonjob\pic-time&location\DJI_0001.jpg'
|
||||
|
||||
GPS_info = find_GPS_image(pic_path)
|
||||
address = find_address_from_GPS(GPS=GPS_info)
|
||||
#print(GPS_info)
|
||||
#print(address)
|
||||
|
||||
x = list(GPS_info.values())
|
||||
#print(x)
|
||||
time = x[1]
|
||||
gps_dict_formate = x[0]
|
||||
y = list(gps_dict_formate.values())
|
||||
information = '拍照时间:'+time+',拍照地点:'+str(address[0])+'(经度:'+str(y[2])+' '+str(y[3])+',纬度:'+str(y[0])+' '+str(y[1])+',高度:'+str(y[5])+'米)'
|
||||
|
||||
print(pic_path)
|
||||
print(information)
|
91
util/simple_sqlite3_tool.py
Normal file
91
util/simple_sqlite3_tool.py
Normal file
@ -0,0 +1,91 @@
|
||||
# coding: utf-8
|
||||
# Author:boxker
|
||||
# Mail:icjb@foxmail.com
|
||||
|
||||
import sqlite3
|
||||
import os
|
||||
|
||||
|
||||
class SimpleSQLite3Tool:
|
||||
"""
|
||||
simpleToolSql for sqlite3
|
||||
简单数据库工具类
|
||||
编写这个类主要是为了封装sqlite,继承此类复用方法
|
||||
"""
|
||||
|
||||
def __init__(self, filename="stsql"):
|
||||
"""
|
||||
初始化数据库,默认文件名 stsql.db
|
||||
filename:文件名
|
||||
"""
|
||||
self.filename = filename
|
||||
self.db = sqlite3.connect(self.filename)
|
||||
self.c = self.db.cursor()
|
||||
|
||||
def close(self):
|
||||
"""
|
||||
关闭数据库
|
||||
"""
|
||||
self.c.close()
|
||||
self.db.close()
|
||||
|
||||
def execute(self, sql, param=None):
|
||||
"""
|
||||
执行数据库的增、删、改
|
||||
sql:sql语句
|
||||
param:数据,可以是list或tuple,亦可是None
|
||||
retutn:成功返回True
|
||||
"""
|
||||
try:
|
||||
if param is None:
|
||||
self.c.execute(sql)
|
||||
else:
|
||||
if type(param) is list:
|
||||
self.c.executemany(sql, param)
|
||||
else:
|
||||
self.c.execute(sql, param)
|
||||
count = self.db.total_changes
|
||||
self.db.commit()
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return False, e
|
||||
if count > 0:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def query(self, sql, param=None):
|
||||
"""
|
||||
查询语句
|
||||
sql:sql语句
|
||||
param:参数,可为None
|
||||
retutn:成功返回True
|
||||
"""
|
||||
if param is None:
|
||||
self.c.execute(sql)
|
||||
else:
|
||||
self.c.execute(sql, param)
|
||||
return self.c.fetchall()
|
||||
|
||||
# def set(self,table,field=" * ",where="",isWhere=False):
|
||||
# self.table = table
|
||||
# self.filed = field
|
||||
# if where != "" :
|
||||
# self.where = where
|
||||
# self.isWhere = True
|
||||
# return True
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 数据库文件位置
|
||||
sql = SimpleSQLite3Tool("../dms_client.db")
|
||||
# f = sql.execute("create table test (id int not null,name text not null,age int);")
|
||||
# print("ok")
|
||||
# sql.execute("insert into test (id,name,age) values (?,?,?);", [(1, 'abc', 15), (2, 'bca', 16)])
|
||||
# res = sql.query("select * from test;")
|
||||
# print(res)
|
||||
# sql.execute("insert into test (id,name) values (?,?);", (3, 'bac'))
|
||||
# res = sql.query("select * from test where id=?;", (3,))
|
||||
res = sql.query("select * from data_collection_info;")
|
||||
print(res)
|
||||
sql.close()
|
Reference in New Issue
Block a user