如何用Python编写代理服务器?
我想编写一个程序来更改 Web 浏览器发送的请求中的 HTTP 标头。 我相信这可以通过代理服务器来完成。 所以,我想写一个代理服务器。
我是学编程的。 我怎样才能在Python中做到这一点?
I want to write a program that changes the HTTP headers in my requests that are sent by my web-browser. I believe it can be done with a proxy server. So, I'd like to write a proxy server.
I study programming. How can I do this in Python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
下面的例子很好地说明了如何在没有框架开销的情况下做到这一点。
http://www.warriorhut.org/whatwg/websocket-proxy.py
Good example below on how to do this without the overhead of a framework.
http://www.warriorhut.org/whatwg/websocket-proxy.py
查看Twisted 框架,特别是扭曲网络。 它在 MIT 下都是免费提供的,因此您可以构建和/或修改它。
另请参阅 Python Twisted 示例。
Look at the Twisted framework, particularly Twisted Web. It's all freely available under MIT, so you can build off and/or modify it.
See also Python Twisted Examples.
虽然 Twisted 正如 @Matthew 推荐的那样非常棒,但更容易学习、理解和修改可能是 这个小例子——与 Twisted 可以提供的“生产质量”和可扩展性相去甚远,但是,您可以从它开始来更好地理解问题。
对于用 Python 编写的各种开源 HTTP 代理,我推荐此列表 -- that 参考拥有构建在线程、Twisted、asyncore 和其他技术之上的适合所有口味的代理!
While Twisted, as recommended by @Matthew, is awesome, easier to learn, understand and modify might be this tiny example -- far away from the "production quality" and scalability that Twisted can offer, but, you could start with it to understand the issues better.
For a wide variety of open-source HTTP proxies written in Python, I recommend this list -- that reference has proxies for all tastes built on top of threading, Twisted, asyncore, and other technologies yet!
查看 Tiny HTTP Proxy (1),当然还有相关文档 (2)。 它基本上是运行服务器并处理请求。
(1) http://www.oki-osk.jp/esc/python/代理/
(2) http://docs.python.org/library/basehttpserver.html
Have a look at Tiny HTTP Proxy (1) and of course the related docs (2). It's basically running a server and handling requests.
(1) http://www.oki-osk.jp/esc/python/proxy/
(2) http://docs.python.org/library/basehttpserver.html
WSGI 可能更容易理解。 因此,我将把paste.proxy 扔在那里,作为构建的基础或作为参考。
http://pythonpaste.org/modules/proxy.html
WSGI may be a little easier to get your head around. So I'll throw paste.proxy out there either as something to build on or as a reference.
http://pythonpaste.org/modules/proxy.html
http://twistedmatrix.com/
适合任何网络需求的优秀库。
http://twistedmatrix.com/
Great library for any networking needs.