Forums

Expires header on static content

hi. Some speedtest I run on my site, reports me back that the static content does not have an Expires header.

I am using the "static files" feature of the Web page of the dashboard of pythonanywhere.

I have seeing some old post on the other forum. And nothing seems possible ? Or was ?

Is there a way to add this header now ? and other too, may be ?

Thanks

You can set custom header for static files with /api/v0/user/{username}/webapps/{domain_name}/static_headers/ endpoint via API

Ok. Excellent. I did not found it in the help, and I did fast read what the API was able to do. I did not scroll to the bottom /o\

Thank you

Here a quick and dirty script I made if anyone is interested:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
import json
import requests
import datetime
import locale

username = 'yourusername'
token = 'yourapitoken'
domain = 'yourdomain'

api_endpoint = f'https://eu.pythonanywhere.com/api/v0/user/{username}'
api_static_headers = f'{api_endpoint}/webapps/{domain}/static_headers/'
headers = {'Authorization': f'Token {token}'}

def create_new_header(url, header, value):
    data = {'url': url, 'name': header, 'value': value}
    response = requests.post(api_static_headers, data=data, headers=headers)
    if response.status_code < 300:
        print(json.loads(response.content))
    else:
        print(f'Got unexpected status code {response.status_code}:')
        print(response.content.decode('utf-8'))

def update_header(hid, url, header, value):
    data = {'url': url, 'name': header, 'value': value}
    response = requests.put(f'{api_static_headers}{hid}/', data=data, headers=headers)
    if response.status_code < 300:
        print(f'{response.status_code}: {json.loads(response.content)}')
    else:
        print(f'Got unexpected status code {response.status_code}:')
        print(response.content.decode('utf-8'))

def delete_header(hid):
    response = requests.delete(f'{api_static_headers}{hid}', headers=headers)
    if response.status_code == 204:
        print(f'Header {hid} deleted')
    else:
        print(f'Got unexpected status code {response.status_code}:')
        print(response.content.decode('utf-8'))

def show_headers():
    response = requests.get(api_static_headers, headers=headers)
    if response.status_code < 300:
        print(json.loads(response.content))
    else:
        print(f'Got unexpected status code {response.status_code}:')
        print(response.content.decode('utf-8'))

def reload():
    url = f'{api_endpoint}/webapps/{domain}/reload/'
    response = requests.post(url, headers=headers)
    if response.status_code < 300:
        print(json.loads(response.content))
    else:
        print(f'Got unexpected status code {response.status_code}:')
        print(response.content.decode('utf-8'))


locale.setlocale(locale.LC_TIME, 'C')
# renew expires header on static dir
now = datetime.datetime.utcnow()
expires_date = now + datetime.timedelta(days =7)
update_header(4, '/static', 'Expires', expires_date.strftime('%a, %d %b %Y %H:%M:%S GMT'))
update_header(6, '/static_root', 'Expires', expires_date.strftime('%a, %d %b %Y %H:%M:%S GMT'))

# force a reload of the app so that change take effect
reload()

#delete_header('9')
#delete_header('10')

#show_headers()

Thanks! That's a useful script.