How to make very simple http proxy using werkzeug or other python requests framework? How to make very simple http proxy using werkzeug or other python requests framework? flask flask

How to make very simple http proxy using werkzeug or other python requests framework?


Twisted has an implementation of a reverse proxy that you could modify to suit your needs. You can look at the examples here. If you look at the source code of twisted.web.proxy, you can see that the 'Host:' header is set in ReverseProxyRequest.process, so you could subclass it and set your own header.


Unless you need to tailor the proxied request based on parameters that only your web application can know (for example, you need to authenticate the proxied request with your webapp's custom authentication system), you should use your web server's proxy capabilities.

Example with Apache:

Listen 0.0.0.0:9090ProxyRequests off<VirtualHost myhost:9090>    ProxyPass / http://localhost:8080/    ProxyPassReverse / http://localhost:8080/    ProxyPassReverseCookieDomain localhost myhost</VirtualHost>

If you have to proxy things in a Flask or Werkzeug application, you can use httplib, creating requests based on the incoming request data and returning the response, either raw or modified (eg for link rewriting). It's doable, I have one such proxy in use where there was no good alternative. If you do that I recommend against using regular expressions to rewrite HTML links. I used PyQuery instead, it's far easier to get it right.