优化flask

This commit is contained in:
2022-02-28 13:51:30 +08:00
parent e1d50c3b72
commit 752cc7e19d
33 changed files with 814 additions and 3099 deletions

View File

@ -4,12 +4,17 @@
import socket
import sys
import struct
import json
# 本机信息
import time
from util.simple_sqlite3_tool import SimpleSQLite3Tool
host_ip = socket.gethostbyname(socket.gethostname())
# 组播组IP和端口
mcast_group_ip = '239.255.255.252'
mcast_group_port = 5678
mcast_group_ip = '224.1.1.1'
mcast_group_port = 2234
def receiver():
@ -34,6 +39,23 @@ def receiver():
while True:
try:
data, address = sock.recvfrom(4096)
sql_tool = SimpleSQLite3Tool("../dms_client.db")
data2 = json.loads(data)
# 若组播数据不正确
if len(data2) < 3:
return
# 若数据已存在
res1 = sql_tool.query("select * from data_list where collection_code=? and file_name=?;", (data2.collectionCode,data2.filename))
if len(res1) != 0:
return
print(res1)
# 存入归档数据
current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
res2 = sql_tool.execute("insert into data_list "
"(file_path,file_name,collection_code,archive_status,created_time) values (?,?,?,?,?);",
(data2.file_path, data2.file_name, data2.collection_code, 0, current_time))
print(res2)
sql_tool.close()
except socket.error as e:
print(f"while receive message error occur:{e}")
else:

View File

@ -1,84 +0,0 @@
"""
Author : XinYi Song
Time : 2021/11/4 9:27
Desc:
"""
import datetime
import os
import time
def file_store_path(time_stamp):
"""
:param time_stamp: 时间戳类型时间
:return:
"""
now = int(round(time_stamp * 1000))
t = time.localtime(now / 1000)
return os.path.join('E:/data/upload', str(t[0]), str(t[1]), str(t[2]))
def file_store_path_year(data_str_time, upload_path, conllection_code):
"""
目录到年
:param upload_path:
:param data_str_time: 字符串类型
:return:
"""
t = time.strptime(data_str_time, '%Y-%m-%d %H:%M:%S')
return os.path.join(upload_path, conllection_code, str(t[0]))
def file_store_path_month(data_str_time, upload_path, conllection_code):
"""
目录到月
:param upload_path:
:param data_str_time:
:return:
"""
t = time.strptime(data_str_time, '%Y-%m-%d %H:%M:%S')
return os.path.join(upload_path, conllection_code, str(t[0]), str(t[1]))
def file_store_path_day(data_str_time, upload_path, conllection_code):
"""
目录到日
:param upload_path:
:param data_str_time: 字符串类型的时间
:return:
"""
t = time.strptime(data_str_time, '%Y-%m-%d %H:%M:%S')
return os.path.join(upload_path, conllection_code, str(t[0]), str(t[1]), str(t[2]))
if __name__ == '__main__':
# time_stamp1 = time.time()
# print(time_stamp1)
str_time = '2020-06-08 09:33:07'
t = time.strptime(str_time, '%Y-%m-%d %H:%M:%S')
# path = os.path.join('../upload', str(t[0]), str(t[1]), str(t[2]))
# if not os.path.exists(path):
# os.makedirs(path)
# print(path)
# time_stamp = float(time_stamp1)
# now = int(round(time_stamp * 1000))
# t = time.localtime(now / 1000)
# print(t)
# list1 = ['张三', '李四']
# token_s = dms_login()
# dms_list = dms_sensing_data(token_s)
# 数据库返回值
# list2 = ['张三', '李四']
# d = [y for y in list2 if y not in list1]
# if d is None or len(d) == 0:
# print("d为空")
# else:
# print(d)
# file_dir = 'C:/Users/HP/Desktop/数管/'
# dir_list = os.listdir(file_dir)
# print(dir_list)
# timestring = '2016-12-21 10:22:56'
# print(time.mktime(time.strptime(timestring, '%Y-%m-%d %H:%M:%S'))) # 1482286976.0
print(t)

File diff suppressed because it is too large Load Diff

View File

@ -1,81 +0,0 @@
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)

View File

@ -1,86 +0,0 @@
"""
Author : XinYi Song
Time : 2021/11/3 14:29
Desc:
"""
from util.file_store_path import file_store_path_day, file_store_path_year, file_store_path_month
"""
实现文件断点续传
"""
import sys
import os
from hashlib import md5
FILE_DIR = os.path.dirname(__file__)
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
home = os.path.join(BASE_DIR, "E:/data/upload")
# 定义一个函数,计算进度条
def bar(num=1, sum=100):
rate = float(num) / float(sum)
rate_num = int(rate * 100)
temp = '\r%d %%' % rate_num
sys.stdout.write(temp)
def md5_file(name):
m = md5()
a_file = open(name, 'rb') #需要使用二进制格式读取文件内容
m.update(a_file.read())
a_file.close()
return m.hexdigest()
def upload_client(local_path, depth, dateTime, conllection_code):
global file_path
while True:
file_byte_size = os.stat(local_path).st_size # 获取文件的大小
file_name = os.path.basename(local_path) # 设置文件名
md5 = md5_file(local_path)
has_sent = 0
file_obj = open(local_path, 'rb') # 对文件进行读操作
file_obj.seek(has_sent) # 调整指针
if depth == 'year':
file_path = file_store_path_year(dateTime, home, conllection_code)
if not os.path.exists(file_path):
os.makedirs(file_path)
if depth == 'month':
file_path = file_store_path_month(dateTime, home, conllection_code)
if not os.path.exists(file_path):
os.makedirs(file_path)
if depth == 'day':
file_path = file_store_path_day(dateTime, home, conllection_code)
if not os.path.exists(file_path):
os.makedirs(file_path)
path = os.path.join(file_path, file_name)
has_received = 0
# 首先判断该路径下是否已存在文件
if os.path.exists(path):
f = open(path, 'wb')
else:
f = open(path, 'wb')
while has_sent < file_byte_size:
# 读出数据
data = file_obj.read(1024)
try:
# 写入数据
f.write(data)
has_received += len(data)
if not data:
raise Exception
except Exception:
flag = False
break
has_sent += len(data)
bar(has_sent, file_byte_size) # 进度条
print("文件上传成功!")
file_obj.close()
f.close()
file_dict = {'fileName': file_name, 'md5': md5, 'file_size': file_byte_size, 'file_path': file_path, 'type': 'ok'}
return file_dict

View File

@ -1,91 +0,0 @@
"""
Author : XinYi Song
Time : 2021/11/4 16:59
Desc:
"""
import rasterio
import requests
from util.xml_util import xml_to_dict, dict_to_xml
def gf4_pmi_001(file_name, xml_name):
"""
:param file_name: 扫描文件传过来的遥感数据源文件的路径
:param xmlPath: 解析出的xml文件存储的路径
:param ThumbnailPath: 解析出的缩略图文件存储的路径
:return:
"""
file_path = 'E:/sensing/GF4_PMI_001/'
with rasterio.open(file_path+file_name, 'r') as ds:
# 存放xml缩略图的文件夹由根文件夹+数据集代码命名的文件夹组成
print('该栅格数据的基本数据集信息:')
CollectionCode = 'GF4_PMI_001' # 数据集代码
DataFormat = ds.driver # DataFormat 数据格式
NumberBands = ds.count # NumberBands 波段数目
ImageWidth = ds.width # ImageWidth 影像宽度
ImageHeight = ds.height # ImageHeight 影像高度
GeographicScope = ds.bounds # GeographicScope 地理范围
ReflectionParameter = ds.transform # ReflectionParameter 反射变换参数(六参数模型)
ProjectionDefinition = ds.crs # ProjectionDefinition 投影定义
# print(CRS.from_epsg(4326))
# 获取第一个波段数据跟GDAL一样索引从1开始
# 直接获得numpy.ndarray类型的二维数组表示如果read()函数不加参数,则得到所有波段(第一个维度是波段)
band1 = ds.read(1)
FirstBindMax = band1.max() # FirstBindMax 第一波段的最大值
FirstBindMin = band1.min() # FirstBindMin 第一波段的最小值
FirstBindAverage = band1.mean() # FirstBindAverage 第一波段的平均值
# 根据地理坐标得到行列号
x, y = (ds.bounds.left + 300, ds.bounds.top - 300) # 距离左上角东300米南300米的投影坐标
row, col = ds.index(x, y) # 对应的行列号
print(f'(投影坐标{x}, {y})对应的行列号是({row}, {col})')
ProjectedCoordinates = x, y # ProjectedCoordinates 投影坐标
RowNumber = row, col # RowNumber 对应的行列号
# 根据行列号得到地理坐标
x, y = ds.xy(row, col) # 中心点的坐标
print(f'行列号({row}, {col})对应的中心投影坐标是({x}, {y})') #
CenterProjectionCoordinates = x, y # CenterProjectionCoordinates 中心投影坐标
# 'C:/Users/HP/Desktop/Number tube/GF4_PMI_E119.8_N35.3_20210908_L1A0000417337/GF4_PMS_E119.8_N35.3_20210908_L1A0000417337.xml'
# 传入xml文件路径解析xml文件
# xml_name 存储后的xml文件的路径+文件名
xml_dict = xml_to_dict(file_path+xml_name)
StartTime = xml_dict['ProductMetaData']['StartTime'] # 开始采集时间
EndTime = xml_dict['ProductMetaData']['EndTime'] # 结束采集时间
CloudPercent = xml_dict['ProductMetaData']['CloudPercent'] # 云覆盖量百分比
TopLeftLatitude = xml_dict['ProductMetaData']['TopLeftLatitude'] # 左上纬度
TopLeftLongitude = xml_dict['ProductMetaData']['TopLeftLongitude'] # 左上经度
TopRightLatitude = xml_dict['ProductMetaData']['TopRightLatitude'] # 右上纬度
TopRightLongitude = xml_dict['ProductMetaData']['TopRightLongitude'] # 右上经度
BottomRightLatitude = xml_dict['ProductMetaData']['BottomRightLatitude'] # 右下纬度
BottomRightLongitude = xml_dict['ProductMetaData']['BottomRightLongitude'] # 右下经度
BottomLeftLatitude = xml_dict['ProductMetaData']['BottomLeftLatitude'] # 左下纬度
BottomLeftLongitude = xml_dict['ProductMetaData']['BottomLeftLongitude'] # 左下经度
boundaryGeomStr = f'POLYGON(({TopLeftLatitude} {TopLeftLongitude},' \
f'{TopRightLatitude} {TopRightLongitude},' \
f'{BottomRightLatitude} {BottomRightLongitude},' \
f'{BottomLeftLatitude} {BottomLeftLongitude},' \
f'{TopLeftLatitude} {TopLeftLongitude}))'
# ThumbnailPath 存储后的缩略图的路径+文件名 ThumbnailName 缩略图文件名称
# xmlPath 存储后的xml文件路径+文件名 xmlFileName xml文件名称
sensing_dict = {'StartTime': StartTime, 'EndTime': EndTime, 'CloudPercent': CloudPercent,
'boundaryGeomStr': boundaryGeomStr, 'DataFormat': DataFormat, 'NumberBands': NumberBands,
'ImageWidth': ImageWidth, 'ImageHeight': ImageHeight, 'GeographicScope': GeographicScope,
#'ReflectionParameter': ReflectionParameter, 'ProjectionDefinition': ProjectionDefinition,
#'FirstBindMax': FirstBindMax, 'FirstBindMin': FirstBindMin,
'FirstBindAverage': FirstBindAverage,
'ProjectedCoordinates': ProjectedCoordinates, 'RowNumber': RowNumber,
#'CenterProjectionCoordinates': CenterProjectionCoordinates,
'CollectionCode': CollectionCode,
"ThumbnailPath": file_path+"GF4_IRS_E119.8_N35.3_20210908_L1A0000417337_thumb.jpg",
"ThumbnailName": "GF4_IRS_E119.8_N35.3_20210908_L1A0000417337_thumb.jpg",
"xmlPath": "", "xmlFileName": "",
'DirectoryDepth': 'day'}
return sensing_dict
if __name__ == '__main__':
file_path = 'C:/Users/HP/Desktop/Number tube/GF4_PMI_E119.8_N35.3_20210908_L1A0000417337/GF4_PMS_E119.8_N35.3_20210908_L1A0000417337.tiff'
xml_path = 'C:/Users/HP/Desktop/Number tube/GF4_PMI_E119.8_N35.3_20210908_L1A0000417337/GF4_PMS_E119.8_N35.3_20210908_L1A0000417337.xml'
gf4_pmi_001(file_path, xml_path)

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,5 @@
# coding: utf-8
# Authorboxker
# Mailicjb@foxmail.com
# Authortajochen
import sqlite3
import os
@ -13,79 +12,84 @@ class SimpleSQLite3Tool:
编写这个类主要是为了封装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 __init__(self, filename="stsql"):
"""
初始化数据库,默认文件名 stsql.db
filename文件名
"""
self.filename = filename
self.db = sqlite3.connect(self.filename)
self.c = self.db.cursor()
def execute(self, sql, param=None):
"""
执行数据库的增、删、改
sqlsql语句
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):
"""
查询语句
sqlsql语句
param参数,可为None
retutn成功返回True
"""
def close(self):
"""
关闭数据库
"""
self.c.close()
self.db.close()
def execute(self, sql, param=None):
"""
执行数据库的增、删、改
sqlsql语句
param数据可以是list或tuple亦可是None
return成功返回True
"""
try:
if param is None:
self.c.execute(sql)
else:
self.c.execute(sql, param)
return self.c.fetchall()
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 set(self,table,field=" * ",where="",isWhere=False):
# self.table = table
# self.filed = field
# if where != "" :
# self.where = where
# self.isWhere = True
# return True
def query(self, sql, param=None):
"""
查询语句
sqlsql语句
param参数,可为None
return:成功返回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,))
sql = SimpleSQLite3Tool("../test.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()

119
util/upload_client.py Normal file
View File

@ -0,0 +1,119 @@
"""
Author : XinYi Song
Time : 2021/12/1 15:19
Desc:
"""
import socket
import sys
import re
import os
from hashlib import md5
import time
FILE_DIR = os.path.dirname(__file__)
# BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# home = os.path.join(BASE_DIR, "/unstructured_data/remote_sensing_data")
home = "/unstructured_data/remote_sensing_data"
# 定义一个函数,计算进度条
def bar(num=1, sum=100):
rate = float(num) / float(sum)
rate_num = int(rate * 100)
temp = '\r%d %%' % (rate_num)
sys.stdout.write(temp)
def file_store_path(time_stamp):
"""
:param time_stamp: 时间戳类型时间
:return:
"""
now = int(round(time_stamp * 1000))
t = time.localtime(now / 1000)
return os.path.join('E:/data/upload', str(t[0]), str(t[1]), str(t[2]))
def file_store_path_year(data_str_time, upload_path, conllection_code):
"""
目录到年
:param upload_path:
:param data_str_time: 字符串类型
:return:
"""
t = time.strptime(data_str_time, '%Y-%m-%d %H:%M:%S')
return os.path.join(upload_path, conllection_code, str(t[0]))
def md5_file(name):
m = md5()
a_file = open(name, 'rb') # 需要使用二进制格式读取文件内容
m.update(a_file.read())
a_file.close()
return m.hexdigest()
def file_store_path_month(data_str_time, upload_path, conllection_code):
"""
目录到月
:param upload_path:
:param data_str_time:
:return:
"""
t = time.strptime(data_str_time, '%Y-%m-%d %H:%M:%S')
return os.path.join(upload_path, conllection_code, str(t[0]), str(t[1]))
def file_store_path_day(data_str_time, upload_path, conllection_code):
"""
目录到日
:param upload_path:
:param data_str_time: 字符串类型的时间
:return:
"""
t = time.strptime(data_str_time, '%Y-%m-%d %H:%M:%S')
return os.path.join(upload_path, conllection_code, str(t[0]), str(t[1]), str(t[2]))
def upload_file_client(file_path, depth, dateTime, collectionCode):
ck = socket.socket()
ck.connect(('192.168.2.9', 9002))
print(str(ck.recv(1024), encoding='utf-8'))
# inp = input('请输入内容格式post|文件路径 目标路径): \n >>> ').strip() # 输入内容格式:命令|文件路径 目标路径
# func, file_path = inp.split("|", 1) # 将输入的内容拆分为两部分,方法名和路径
# local_path, target_path = re.split("\s*", file_path, 1) #再将路径部分,通过正则表达式。以空格拆分为:文件路径和上传的目标路径
file_byte_size = os.stat(file_path).st_size # 获取文件的大小
file_name = os.path.basename(file_path) # 设置文件名
md5 = md5_file(file_path)
if depth == 'year':
file_paths = file_store_path_year(dateTime, home, collectionCode)
if depth == 'month':
file_paths = file_store_path_month(dateTime, home, collectionCode)
if depth == 'day':
file_paths = file_store_path_day(dateTime, home, collectionCode)
post_info = "post|%s|%s|%s|%s|%s" % (file_name, file_byte_size, collectionCode, dateTime, depth) # 将发送的内容进行拼接
ck.sendall(bytes(post_info, encoding='utf-8')) # 向服务器端发送内容
result_exist = str(ck.recv(1024), encoding='utf-8')
has_sent = 0
if result_exist == "2003":
ck.sendall(bytes("2004", encoding='utf-8'))
result_continue_pos = str(ck.recv(1024), encoding='utf-8') # 已经传输了多少的文件内容
print(result_continue_pos)
has_sent = int(result_continue_pos)
file_obj = open(file_path, 'rb') # 对文件进行读操作
file_obj.seek(has_sent) # 调整指针
while has_sent < file_byte_size:
data = file_obj.read(1024)
ck.sendall(data)
has_sent += len(data)
bar(has_sent, file_byte_size) # 进度条
file_obj.close()
print("文件上传成功!")
uc = {'file_size': file_byte_size, 'fileName': file_name, 'md5': md5, 'file_path': file_paths + '/' + file_name,
'type': 'ok'}
return uc

View File

@ -1,78 +0,0 @@
"""
Author : XinYi Song
Time : 2021/10/9 9:43
Desc:
"""
import os, sys, subprocess, tempfile, time
# 创建临时文件夹,返回临时文件夹路径
TempFile = tempfile.mkdtemp(suffix='_test', prefix='python_')
# 文件名
FileNum = int(time.time() * 1000)
# python编译器位置
EXEC = sys.executable
# 获取python版本
def get_version():
v = sys.version_info
version = "python %s.%s" % (v.major, v.minor)
return version
# 获得py文件名
def get_pyname():
global FileNum
return 'test_%d' % FileNum
# 接收代码写入文件
def write_file(pyname, code):
fpath = os.path.join(TempFile, '%s.py' % pyname)
with open(fpath, 'w', encoding='utf-8') as f:
f.write(code)
print('file path: %s' % fpath)
return fpath
# 编码
def decode(s):
try:
return s.decode('utf-8')
except UnicodeDecodeError:
return s.decode('gbk')
# 主执行函数
def main(code):
r = dict()
r["version"] = get_version()
pyname = get_pyname()
fpath = write_file(pyname, code)
try:
# subprocess.check_output 是 父进程等待子进程完成,返回子进程向标准输出的输出结果
# stderr是标准输出的类型
outdata = decode(subprocess.check_output([EXEC, fpath], stderr=subprocess.STDOUT, timeout=5))
except subprocess.CalledProcessError as e:
# e.output是错误信息标准输出
# 错误返回的数据
r["code"] = 'Error'
r["output"] = decode(e.output)
return r
else:
# 成功返回的数据
r['output'] = outdata
return r
finally:
# 删除文件(其实不用删除临时文件会自动删除)
try:
os.remove(fpath)
except Exception as e:
exit(1)
# if __name__ == '__main__':
# code = "print(11);print(22)"
# print(main(code))