Search code examples
pythontwisted

Python Twisted How do I determine an absolute URL?


I need to generate an absolute URL to a resource while handling a web request in Twisted. The host, port, and protocol (HTTP/HTTPS) can all vary based on deployment, so it would be really convenient to look things up dynamically.

The URL I need to is a modification of the url of the resource currently being processed with all arguments stripped away, so just finding the full request URL would help a lot.

In other words, I know "/path/foo", but how do I find "http://example.com:8080"?


Solution

  • The Request passed in to lots of Twisted Web server APIs has a URLPath method that returns an instance of twisted.python.urlpath.URLPath. The string representation of a URLPath instance is a string like "http://example.com:8080/foo/bar".