11

What is the simplest way to create a HTTP proxy with Python? As far as I can understand, it should be possible to create the proxy relatively easily with a couple lines of code using the standard library HTTP server features and urlopen or Requests.

mrts
  • 16,697
  • 8
  • 89
  • 72
Jake
  • 907
  • 4
  • 12
  • 25
  • What kind of proxy - HTTP? SOCKS? And what's the use case? – AndiDog Aug 13 '10 at 19:45
  • 1
    Sorry for not being more precise. I would like it to be an HTTP proxy server. The use case is just to use as one would a normal proxy server. As simple as possible. – Jake Aug 13 '10 at 19:53
  • Well in this case you might rather want to use a normal proxy server (Squid, Privoxy, Polipo). If you want a custom proxy, use Apache/mod_proxy ;) – AndiDog Aug 13 '10 at 20:35
  • Thing is.. I want to write my own. Hmm and to me, even the simple ones like the one Brian linked looks a bit difficult to me. – Jake Aug 13 '10 at 20:57
  • @Jake: It's not clear to me how you could expect anything much simpler than the one I linked. It barely does anything. – Brian Aug 13 '10 at 23:21
  • I am probably not sufficient enough in python yet. I see your point in the code being relatively short, but it seems a bit complicated. I guess i will have to study a bit more hehehe. – Jake Aug 14 '10 at 08:40
  • Today I tried going through the proxy I linked to to look for chunks of it that were unnecessary, but didn't find much in it that could be eliminated excepting the `method_CONNECT` stuff, which relates to stuff you can probably ignore for the sake of simplifying your proxy. – Brian Oct 16 '10 at 00:05
  • See https://gist.github.com/mrts/2eefdf0f992f0f0e2269308fc6486011 for a simple Python 3 HTTP proxy. – mrts Apr 26 '22 at 12:52

2 Answers2

7

One incredibly simple one is python-proxy. I found it on the list of python proxies at xhaus, which was the top result when I googled "python proxy server" (sans quotes).

Brian
  • 25,523
  • 18
  • 82
  • 173
  • This answer is now out of date. The link to python-proxy goes to Google Code, which closed years ago. You may be able to get it if you have an old Google Code account. – BoarGules Jan 20 '18 at 07:34
  • @BoarGules: The link still lets me download the source, even if I open it in an incognito window. – Brian Jan 20 '18 at 19:05
  • But not me. Anonymous users not allowed. I think your incognito window isn't as incognito as you think. – BoarGules Jan 20 '18 at 19:07
  • 1
    The python-proxy code was exported to Github more than once, e.g. https://github.com/inject2006/python-proxy – Alex Willmer Aug 29 '18 at 00:24
  • 2
    I am author of proxy.py, lightweight http, https and websockets proxy server distributed as a single Python file with no external dependency. https://github.com/abhinavsingh/proxy.py – Abhinav Singh Dec 10 '18 at 10:55
4

Twisted lets you build simple ones, if you don't mind the complexity that is twisted.

nmichaels
  • 49,466
  • 12
  • 107
  • 135