首页 > 解决方案 > 从远程 python 服务器上的 GET 请求返回结果的字段

问题描述

我在运行 python 代码的树莓派 4 上从邮递员向我的服务器执行“GET”请求时遇到问题......

我面临的错误是:

'int' object is not callable The view function did not return a valid response. The return type must be a string, tuple, Response instance, or WSGI callable, but it was a int.

这个代码的工作是计算来自传感器的值流,然后返回它们的平均值,但是每当我尝试它时,它总是给我这个错误.. #注意它在 Pycharm 上工作正常,但在请求中没有.

python脚本是:

def oxygin():
   result = "Result is: "
   print("\nSparkFun MAX3010x Photodetector - Example 5\n")
   sensor = qwiic_max3010x.QwiicMax3010x()

if sensor.begin() == False:
    print("The Qwiic MAX3010x device isn't connected to the system. Please check your connection", \
        file=sys.stderr)
    return
else:
    print("The Qwiic MAX3010x is connected.")

print("Place your index finger on the sensor with steady pressure.")

if sensor.setup() == False:
    print("Device setup failure. Please check your connection", \
        file=sys.stderr)
    return
else:
    print("Setup complete.")

sensor.setPulseAmplitudeRed(0x0A) # Turn Red LED to low to indicate sensor is running
sensor.setPulseAmplitudeGreen(0) # Turn off Green LED

RATE_SIZE = 4 # Increase this for more averaging. 4 is good.
rates = list(range(RATE_SIZE)) # list of heart rates
rateSpot = 0
lastBeat = 0 # Time at which the last beat occurred
beatsPerMinute = 0.00
OxiAvg = 0
samplesTaken = 0 # Counter for calculating the Hz or read rate
startTime = millis() # Used to calculate measurement rate

while True:

    irValue = sensor.getIR()
    samplesTaken += 1
    if sensor.checkForBeat(irValue) == True:

        # We sensed a beat!
        #print('')
        delta = ( millis() - lastBeat )
        lastBeat = millis() 

        beatsPerMinute = 60 / (delta / 1000.0)
        beatsPerMinute = round(beatsPerMinute,1)

        if beatsPerMinute < 255 and beatsPerMinute > 20:
            rateSpot += 1
            rateSpot %= RATE_SIZE # Wrap variable
            rates[rateSpot] = beatsPerMinute # Store this reading in the array

            # Take average of readings
            OxiAvg = 0

            for x in range(0, RATE_SIZE):
                OxiAvg += rates[x]
            OxiAvg /= RATE_SIZE
            OxiAvg = round(OxiAvg)

    Hz = round(float(samplesTaken) / ( ( millis() - startTime ) / 1000.0 ) , 2)
    if (samplesTaken > 1000) == 1:
        result = str(OxiAvg)
        #return str(OxiAvg)
        #print('SpO2=', (beatAvg+15) , '%\t')

return result 

标签: pythonservergetraspberry-pi

解决方案


您应该返回一个字符串对象,而不是一个 int 对象

替换以下行:

return result

和:

return str(result)

推荐阅读