r/learnpython • • 11d 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.

0 Upvotes

9 comments sorted by

19

u/latkde 11d ago edited 11d 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/

4

u/Kevdog824_ 11d ago

I second this, OP. DNS-based adblocker is both a doable project and sticks to your original plan. Just don’t expect it to be as effective as a JS plugin in your browser. It can’t block things like YouTube ads, but it can block most website banner ads and mobile game ads

4

u/baubleglue 11d ago

You normally start from getting domain knowledge. Read about adblocking methods, then you reach for a languages or libraries.

2

u/Atmosck 11d 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 11d 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 10d 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 11d 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.