r/learnpython • u/Character-Alps2226 • 18d ago
How does one go about creating an adblocker using python?
I'm working on a project for uni, me and team chose to create an adblocker. As someone who's pretty new to python, where do we start? What modules do we use? Is there a list of blacklisted sites and adverts? I'd really appreciate the help.
4
u/baubleglue 18d ago
You normally start from getting domain knowledge. Read about adblocking methods, then you reach for a languages or libraries.
2
u/Atmosck 18d ago
Is the assignment to use python? Because ad blockers tend to be browser extensions, and therefore JavaScript.
I guess you could maybe use python to create some sort of network traffic monitor that blocks known ad IPs. That sounds kind of intense for a uni project if you're not already doing networking stuff like that.
1
u/Discordbothost 18d ago
for a uni project the dns/hosts-file path is the right scope. start tiny: load a blocklist into a set, resolve names against it, return 0.0.0.0 for hits. dnslib or even just editing /etc/hosts from a python script that refreshes from peter lowe's list is enough to demo. browser extensions really are js-only so don't fight that.
1
u/xervir-445 17d ago
Python doesn't make browser extensions, that is the domain of javascript. You can make a server with it, though.
What you can do is
find examples of DNS servers written in Python (here's a snippet I found)
make sure that code works as a dns forwarder (it sends off dns requests upstream to another server like 8.8.8.8 or 1.1.1.1)
before it forwards the request have it check dns requests against lists of ad domains, and if it gets a result don't foward to dns request and instead send back a 0.0.0.0 so the client can't find anything
take any computers or phones you want to block ads on and set their dns server to the address of your new python service
This is called a "dns hole" and it's the principle behind how a pihole works for network level adblocking.
0
u/TheRNGuy 18d ago
Can't, it should be made with js as a browser extension.
The only possible solution would be make your own browser that uses Python.
19
u/latkde 18d ago edited 18d ago
You cannot with realistic effort create a Python-based adblocker that runs in the browser: browser extensions must use JavaScript.
But you could implement a proxy, or a DNS server that provides some filtering. The DNS approach will be simplest, which will give you something similar to the existing Pi-hole tool (https://pi-hole.net/). However, that will require you to do rather low-level network programming.
Adblockers use a variety of lists as their data sources, which also differ in their format. You will likely find Peter Lowe's list to be most useful for your purposes: https://pgl.yoyo.org/adservers/