简体   繁体   English

Python请求的行为就像它具有缓存一样

[英]Python requests acting like it has a cache

I am trying to get a color hexcode from an XML page on my website and update a script within 5-10 seconds. 我正在尝试从网站上的XML页面获取彩色十六进制代码,并在5到10秒内更新脚本。 I am able to read the hexcode just fine, and I am able to change the value in the XML file just fine, but the script takes awhile to reflect the update. 我能够很好地读取十六进制代码,并且能够很好地更改XML文件中的值,但是脚本需要一些时间来反映更新。

I want the script to update every 5 seconds by checking the XML file from my webserver, however it takes about 1 full minute before the code actually sees the update. 我希望脚本通过从我的Web服务器检查XML文件来每5秒更新一次,但是大约需要一整分钟的时间,代码才能真正看到更新。 Is my python script somehow caching the XML file? 我的python脚本是否以某种方式缓存XML文件? Is my webserver possibly sending a cached version? 我的网络服务器是否可能发送缓存的版本? (Viewing the XML file in chrome refreshes instantly though.) (不过,在chrome中查看XML文件会立即刷新。)

Python code: 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 File: XML档案:

<?xml version="1.0" encoding="UTF-8"?>
<ledstrip>
    <color>2196f3</color>
    <flash>false</flash>
    <freq>15</freq>
</ledstrip>

The solution was that my webserver used NGINX as a server side cache controller, I opted to disable this cache control during the development stage, just so that I could see the results instantly. 解决方案是我的Web服务器使用NGINX作为服务器端缓存控制器,我选择在开发阶段禁用此缓存控制,以使我可以立即看到结果。 Most likely there is a better way of pushing data rather than continually polling the webserver for it. 很可能有一种更好的方式来推送数据,而不是不断轮询网络服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM