1
我想從我的網站上的XML頁面獲取顏色十六進制代碼,並在5-10秒內更新腳本。我可以很好地讀取十六進制代碼,並且我可以很好地更改XML文件中的值,但腳本需要一些時間來反映更新。Python請求像它有緩存
我希望腳本通過檢查來自我的網絡服務器的XML文件每5秒更新一次,然而在代碼實際看到更新之前大約需要整整1分鐘。我的python腳本以某種方式緩存XML文件?我的網絡服務器可能發送緩存版本嗎? (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>
是的,5秒不會太短。該程序只能在5分鐘內以演示的形式運行,因此不會有太多開銷。 –
你有沒有試過這個模塊? https://pypi.python.org/pypi/requests-cache – Veltro
假設你通過互聯網訪問xanderluciano.com,你的ISP有一個(不可見的)緩存嗎?你可以設置你的網站的緩存控制/過期返回的標題,以表明這些XML文件不應該被緩存? – barny