如何解决Cherrypy中同源策略导致的跨域问题?
摘要:# 添加:cherrypy.response.headers["Access-Control-Allow-Origin"] = "*"
# 添加:
cherrypy.response.headers["Access-Control-Allow-Origin"] = "*"
cherrypy.response.headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS"
cherrypy.response.headers["Access-Control-Allow-Headers"] = "Content-Type"
cherrypy.response.headers["Access-Control-Allow-Credentials"] = "true"
例如:
import cherrypy
import os
import subprocess
import change_format
class PreAnnoAsyncHandle:
@cherrypy.expose
@cherrypy.tools.json_out()
def index(self, scene_dir, frame, frame_num):
''' cherrypy - 解决同源策略跨域问题 '''
# ------------------------------------------------------------------------------
cherrypy.response.headers["Access-Control-Allow-Origin"] = "*"
cherrypy.response.headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS"
cherrypy.response.headers["Access-Control-Allow-Headers"] = "Content-Type"
cherrypy.response.headers["Access-Control-Allow-Credentials"] = "true"
# ------------------------------------------------------------------------------
frame_num = int(frame_num)
try:
# 同步数据:将标注后的.label文件同步到本地
res_state = subprocess.call(f'sh async_minio_data.sh {scene_dir}', shell=True)
print(res_state)
# label文件格式转换:将同步后的.label文件转换成本地的.pcd文件
# frame: 当前frame
# frame_num: 从当前帧开始标注frame_num+1帧
root_dir = "/data/sse-images/"
pcd_dir = root_dir + scene_dir + '/lidar'
label_points_dir = root_dir + scene_dir + '/label_points'
pcd_labels_dir = "/data/sse-internal/" + scene_dir + '/lidar/'
pcd_list = []
file_names = os.listdir(pcd_dir)
for file_name in file_names:
file_name = str(file_name)
if file_name.endswith('.pcd'):
pcd_list.append(file_name)
pcd_list = sorted(pcd_list)
# pcd_name_list:存放pcd名称(不包含.pcd后缀)
pcd_name_list = []
for pcd in pcd_list:
pcd = str(pcd)
pcd_name = pcd.split('.')[0]
pcd_name_list.append(pcd_name)
print('pcd_name_list\n', pcd_name_list)
# 最大索引值
pcd_name_list_length = len(pcd_name_list)
# 当前frame索引
current_frame_index = pcd_name_list.index(frame)
# 要转换的具体frame名
to_pre_anno_frames = []
# 如果是最后一帧,会超出索引范围,因此需要进行长度判断
if current_frame_index + frame_num + 1 > pcd_name_list_length:
max_idx = pcd_name_list_length
else:
max_idx = current_frame_index + frame_num + 1
for idx in range(current_frame_index, max_idx):
name = pcd_name_list[idx]
to_pre_anno_frames.append(name)
print(to_pre_anno_frames)
# 要转换的具体frame的.label文件路径,以及转换后的.pcd文件路径
format_switch_path_list = []
for item in to_pre_anno_frames:
label_path = label_points_dir + '/' + item + '.label'
pcd_path = pcd_dir + '/' + item + '.pcd'
print(label_path, pcd_path)
frame_info = {
'label_path': label_path,
'pcd_path': pcd_path
}
format_switch_path_list.append(frame_info)
print(format_switch_path_list)
# 开始调用change_format.py程序进行格式转换,将label文件中的内容转换至.pcd文件
# change_format.start_switch()
for info_dict in format_switch_path_list:
label_path = info_dict['label_path']
pcd_path = info_dict['pcd_path']
print(label_path, pcd_path)
# 将模型标注后的.labels文件 转化成本地的.pcd文件
change_format.start_switch(label_path, pcd_path)
# 删除原来的.pcd.labels文件
for frm in to_pre_anno_frames:
lab_pth = pcd_labels_dir + frm + '.pcd.labels'
if os.path.exists(lab_pth):
res = subprocess.call(f'rm -rf {lab_pth}', shell=True)
print(res)
return 1
except Exception as error:
print(error)
print("失败")
return 0
if __name__ == '__main__':
cherrypy.quickstart(PreAnnoAsyncHandle())
