繁体   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