Skip to content

Instantly share code, notes, and snippets.

@0xa
Created February 4, 2019 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xa/f2822449e2265bacb2e8dc4dd3e5f06f to your computer and use it in GitHub Desktop.
Save 0xa/f2822449e2265bacb2e8dc4dd3e5f06f to your computer and use it in GitHub Desktop.
remove errors from nginx cache
#!/usr/bin/env python3
# apt install python3-tqdm
import os
import sys
from glob import iglob
from tqdm import tqdm
root = '/var/cache/nginx/octodon'
HEADER_SIZE = 2048
def main():
print("Listing files... ", end="")
sys.stdout.flush()
files = iglob(os.path.join(root, '**'), recursive=True)
files = (f for f in files if not f.startswith(os.path.join(root, 'temp/')))
files = (f for f in files if os.path.isfile(f))
files = list(files)
print(len(files))
bar = tqdm(files)
for fp in bar:
try:
with open(fp, 'rb') as f:
header = f.read(HEADER_SIZE)[0x91:].split(b'\r\n\r\n', 1)[0]
headers = header.decode('utf-8').split('\n')
key = headers[0]
http = headers[1]
assert http.startswith('HTTP/1.'), http
code = int(http.split(' ')[1])
if code >= 400:
bar.write("Deleting HTTP {} {}".format(code, fp))
os.unlink(fp)
except FileNotFoundError:
pass
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment