Search code examples
pythonhttprequestpyramid

How to get the url of the requesting site with pyramid


I'm writing a small app with pyramid. One view serves files with a FileResponse. What I want to do is limiting those files to certain domains.

If this site http://www.example.com/index.html includes a file like:

<img src="http://myapp.com/servethisfile" />

I want to know the domain of the site which requests a file from my app. (example.com)

The limitation has to be done within the view as if will change during runtime.


Solution

  • The browser sends the referrer in the HTTP headers when doing a request. It's easily hackable though and I would not rely on this to protect something important.

    In Pyramid, you can access that header in request.referrer.

    http://docs.pylonsproject.org/projects/pyramid/en/latest/api/request.html#pyramid.request.Request.referer