我正在构建一个SQLite3数据库,以存储任何给定天文图像中恒星的孔径通量测量值。我有一个文件(star_tabase.py(,其中包含一个Flask应用程序,该应用程序使用两个路由运行,用于处理从数据库中进行选择和插入数据库的操作。当传入的图像需要光度处理时,有一个单独的脚本(aperture_photometry.py(将调用这些路由。我的问题的关键在于将数据插入SQLite数据库的函数和将数据传递给Flask应用程序的光圈测光脚本之间的交互。以下是相关功能:
# Function in aperture_photometry.py that turns Star object data into a dictionary
# and passes it to the Flask app
from astropy.io import fits
import requests
import json
def measure_photometry(image, bandpass):
df = fits.getdata(image)
hf = fits.getheader(image, ext=1)
date_obs = hf['DATE-OBS']
ra, dec = get_center_coords(hf)
response = requests.get(f'http://127.0.0.1:5000/select/{ra}/{dec}/').content
star_json = json.loads(response)
if star_json is not None:
stars = json_to_stars(star_json)
get_raw_flux(df, df*0.01, hf, stars) # Error array will be changed
star_json = []
# Critical section
for star in stars:
star_json.append({"star_id":star.star_id, "source_id":star.source_id, "flux":star.flux, "flux_err":star.flux_err})
response = requests.put('http://127.0.0.1:5000/insert/', data={'stars':star_json, 'bandpass':bandpass, 'dateobs':date_obs})
print(response.content)
else:
print("ERROR: Could not get star objects from database.")
# Function in star_database.py that handles incoming flux measurements from the
# aperture photometry script, and then inserts data into the SQLite database
from flask import Flask, request
@app.route('/insert/', methods=['PUT'])
def insert_flux_rows():
conn = create_connection(database)
if conn is not None:
c = conn.cursor()
body = request.get_json(force=True)
print(body)
# More comes after this, but it is not relevant to the question
运行Flask应用程序并调用aperture_photometry.py后,PUT请求response.content行会打印一个400 Bad Request
错误,并显示消息Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)
。我认为这个问题要么是因为我试图在measure_photometry中将恒星对象数据传递到PUT请求时对其进行格式化,要么是因为做body = request.get_json(force=True)
有问题。还值得一提的是,insert_flux_rows中的语句print(body)
不会将任何内容打印到stdout。出于所有意图和目的,这两个脚本必须保持分离,即我不能将它们组合起来并删除请求依赖关系。
我真的很感谢你的帮助,因为我一整天都在努力解决这个问题。
根据这个问题的首要答案,measure_photometry
函数中的data
变量似乎无法正确转换为json。
您应该尝试对其进行测试(可能在其上运行json.dumps
(,看看是否提供了更详细的错误消息。还有jsonschema
软件包。