Skip to content
lv_oz2 edited this page Aug 22, 2025 · 4 revisions

python_sri is a Python module to generate Subresource Integrity hashes on the fly. It supports Python 3.10+, including free threading, and has zero dependencies

Quickstart

Use the package manager pip to install python_sri.

pip install python_sri

Then import, setup and hash!

from python_sri import SRI

# Creating an instance, providing your site's domain name and some config
sri = SRI('https://example.com', static={'directory': 'static', 'url_path': '/static'})

@sri.html_uses_sri('/')
def index() -> str:
    return '''
		...
        <link rel="stylesheet" href="static/main.css" integrity></link>
		...
	'''
# -> ...
#    <link rel="stylesheet" href="static/main.css"></link>
#    ...

sri.hash_html('/', '<script src="/static/main.js" integrity></script>')
# -> <script src="/static/main.js"></script>

Usage

  • Using Flask? Flask specific documentation is here
  • Using Django? Django specific documentation is here
  • Using FastAPI? FastAPI specific documentation is here
  • Not using any of the above frameworks? Generic documentation is here

License

MIT

Clone this wiki locally