如何使用 Python 重新路由网络流量?

发布于 2024-09-27 23:07:46 字数 674 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹微笑 2024-10-04 23:07:47

由于您对 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文