我正在尝试从我的网站上的XML页面获取一个颜色十六进制码,并在5-10秒内更新脚本。我能够很好地读取十六进制码,也能够很好地更改XML文件中的值,但是脚本需要一段时间才能反映出更新。
我想让脚本每5秒钟检查一次来自Web服务器的XML文件以进行更新,但实际上需要大约1分钟才能看到代码的更新。我的Python脚本是否缓存了XML文件?我的Web服务器可能会发送已缓存的版本吗?(在Chrome中查看XML文件可以立即刷新。)
Python代码:
我想让脚本每5秒钟检查一次来自Web服务器的XML文件以进行更新,但实际上需要大约1分钟才能看到代码的更新。我的Python脚本是否缓存了XML文件?我的Web服务器可能会发送已缓存的版本吗?(在Chrome中查看XML文件可以立即刷新。)
Python代码:
import time
import serial
import requests
from bs4 import BeautifulSoup
ser = serial.Serial('/dev/ttyACM0',9600)
print('Connected to Arduino!')
while (True):
print('Connecting to website...')
page = requests.get('http://xanderluciano.com/pi/color.xml', timeout=5)
soup = BeautifulSoup(page.text, 'html.parser')
print('scraped hexcode: ' + soup.color.string)
hex = soup.color.string
ser.write(hex.encode('utf-8'))
print(ser.readline())
time.sleep(5);
XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<ledstrip>
<color>2196f3</color>
<flash>false</flash>
<freq>15</freq>
</ledstrip>