如何使用 Python 重新路由网络流量?
Freedom 和 反社会最近引起了不小的轰动。他们要么完全切断你与互联网的联系,要么只是屏蔽社交网站,以阻止你拖延,并在你真正想要完成一些工作时帮助你发挥自我控制力。
我查看了可用的选项以及一些 Google Chrome 扩展,发现它们都没有完全满足我的要求,因此我计划编写自己的小 Python 工具来实现这一点。我的第一个冲动是简单地修改 /etc/hosts
以将对某些服务器的请求重新路由回 localhost
。但是,这只能阻止整个域。我需要根据正则表达式或简单的字符串匹配来阻止地址,以阻止诸如 google.com/reader
(是的,特别是这个)之类的内容,但不是整个 google.com
域。
我可以使用哪个 Python 框架来监控我的网络流量并阻止列入黑名单的地址?实现我想要做的事情的基本设计是什么?我是否使用socket
库?我对 Python 非常熟悉,但对网络编程还很陌生。
Small apps like Freedom and Anti-social have created quite a stir lately. They cut you off from the internet either entirely or just block social networking sites in order to discourage procrastination and help you exert self control when you really want to get some work done.
I looked at the available options and also at some Google Chrome extensions and decided that none of them is exactly doing what I want, so I'm planning to write my own little Python tool to do that. My first impulse was to simply modify /etc/hosts
to re-route requests to certain servers back to localhost
. However, this can only block entire domains. I'd need to block addresses based on regular expressions or simple string matching to block something like google.com/reader
(yes, this one in particular), but not the entire google.com
domain.
Which Python framework can I use to monitor my network traffic and block blacklisted addresses and what is the basic design to achieve what I want to do? Do I use the socket
library? I'm very comfortable with Python, but I'm very new to network programming.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您对 python 很熟悉,所以我直接推荐 twisted。它比其他一些库稍微困难一些,但它经过了良好的测试,具有出色的性能和许多功能。您只需实现一个小型 HTTP 代理并对 URL 进行正则表达式过滤即可。
Since you're comfortable with python, I'd directly recommend twisted. It is slightly harder than some other libraries, but it is well-tested, has great performance and many features. You would just implement a small HTTP proxy and do your regexp filtering on the URLs.