【发布时间】:2019-06-06 19:47:51
【问题描述】:
我尝试检查我是否已经在 GitHub 上拥有存储库的主分支的当前版本。我希望这可以与ETag 一起使用,这样如果我向我的请求提供最后保存的ETag 并且ETag 与远程相同,那么我不需要再次下载它。
我用这段代码来检查这个:
import requests
etag_old = '"8ec0bb526b1b281a450669d79ca9ed0c7ff6b4f2"'
headers = {"If-None-Match": etag_old}
# Also doesn't work: https://codeload.github.com/Endogen/OpenCryptoBot/zip/master
response = requests.get("https://github.com/Endogen/OpenCryptoBot/archive/master.zip", headers=headers)
if response.status_code == 200:
print(f"Status-Code: {response.status_code}")
print(f"New ETag: {response.headers.get('ETag')}")
print(f"History: {response.history}")
print(f"URL: {response.url}")
else:
# If we get status code 304, it's working
print(f"Status-Code: {response.status_code}")
奇怪的是,这不起作用。 response.url 中的 URL 也不起作用。它与 URL 有关,因为它有效:
import requests
etag_old = '"29b127a376b492572f7e332ba5dd38ea89d4d37c"'
headers = {"If-None-Match": etag_old}
response = requests.get("https://raw.githubusercontent.com/endogen/Telegram-Kraken-Bot/master/telegram_kraken_bot.py", headers=headers)
if response.status_code == 200:
print(f"Status-Code: {response.status_code}")
print(f"New ETag: {response.headers.get('ETag')}")
print(f"History: {response.history}")
print(f"URL: {response.url}")
else:
# If we get status code 304, it's working
print(f"Status-Code: {response.status_code}")
我的问题是:如何可靠地检查我是否已经拥有当前的 master 分支版本?
我正在使用 Python 3.7
【问题讨论】:
-
你为什么不使用 git 并克隆 repo?
-
@TheIncorrigible1因为在我使用的环境中没有git。我要做的是:1)从主人那里获取内容2)提取zip 3)用下载的内容替换本地内容。当然,如果回购中有新内容,我只想这样做
标签: python python-3.x github python-requests