簡體   English   中英

Python請求的行為就像它具有緩存一樣

[英]Python requests acting like it has a cache

我正在嘗試從網站上的XML頁面獲取彩色十六進制代碼,並在5到10秒內更新腳本。 我能夠很好地讀取十六進制代碼,並且能夠很好地更改XML文件中的值,但是腳本需要一些時間來反映更新。

我希望腳本通過從我的Web服務器檢查XML文件來每5秒更新一次,但是大約需要一整分鍾的時間,代碼才能真正看到更新。 我的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>

解決方案是我的Web服務器使用NGINX作為服務器端緩存控制器,我選擇在開發階段禁用此緩存控制,以使我可以立即看到結果。 很可能有一種更好的方式來推送數據,而不是不斷輪詢網絡服務器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM