Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/nicotine-plus/nicotine-plus/llms.txt

Use this file to discover all available pages before exploring further.

The networkfilter module provides functionality for banning users, ignoring users, and filtering by IP address or country.

NetworkFilter Class

Manages user and IP-based filtering.

Attributes

ip_ban_requested
dict
Pending IP ban requests
ip_ignore_requested
dict
Pending IP ignore requests

Methods

ban_user()

def ban_user(self, username)
Bans a user by username.
username
str
Username to ban
from pynicotine.core import core

core.network_filter.ban_user("spammer123")

unban_user()

def unban_user(self, username)
Unbans a previously banned user.
username
str
Username to unban

is_user_banned()

def is_user_banned(self, username)
Checks if a user is banned.
username
str
Username to check
banned
bool
True if user is banned

ban_user_ip()

def ban_user_ip(self, username=None, ip_address=None)
Bans a user by IP address.
username
str
Username (IP will be looked up if not provided)
ip_address
str
IP address to ban directly
ip_address
str
The banned IP address

is_user_ip_banned()

def is_user_ip_banned(self, username=None, ip_address=None)
Checks if a user’s IP is banned, including wildcard matching.
username
str
Username to check
ip_address
str
IP address to check
banned
bool
True if IP is banned

ignore_user()

def ignore_user(self, username)
Ignores a user (hides their messages).
username
str
Username to ignore

unignore_user()

def unignore_user(self, username)
Unignores a previously ignored user.
username
str
Username to unignore

is_user_ignored()

def is_user_ignored(self, username)
Checks if a user is ignored.
username
str
Username to check
ignored
bool
True if user is ignored

IP Address Utilities

get_country_code()

def get_country_code(self, ip_address)
Determines the country code for an IP address.
ip_address
str
IP address to lookup
country_code
str
Two-letter country code (e.g., “US”, “GB”, “DE”)
from pynicotine.core import core

country = core.network_filter.get_country_code("8.8.8.8")
print(f"Country: {country}")  # Output: US

is_ip_address()

@staticmethod
def is_ip_address(ip_address, allow_zero=True, allow_wildcard=True)
Validates whether a string is a valid IPv4 address.
ip_address
str
String to validate
allow_zero
bool
default:true
Whether to accept “0.0.0.0”
allow_wildcard
bool
default:true
Whether to accept wildcards like “192.168..
valid
bool
True if valid IP address

Country Codes

The COUNTRIES dictionary maps ISO country codes to country names.
from pynicotine.networkfilter import NetworkFilter

country_name = NetworkFilter.COUNTRIES.get("US")
print(country_name)  # Output: United States

# List all countries
for code, name in NetworkFilter.COUNTRIES.items():
    print(f"{code}: {name}")

IP Wildcards

IP banning and ignoring supports wildcard patterns:
  • 192.168.1.* - Matches all IPs in 192.168.1.0/24 subnet
  • 10.*.*.* - Matches all IPs in 10.0.0.0/8 subnet
  • 172.16.*.5 - Matches specific IPs across subnets
from pynicotine.core import core

# Ban entire subnet
core.network_filter.ban_user_ip(ip_address="192.168.1.*")

# Check if specific IP is banned
is_banned = core.network_filter.is_user_ip_banned(
    ip_address="192.168.1.100"
)
print(is_banned)  # Output: True

Configuration Integration

Filtered users are persisted in the configuration:
from pynicotine.config import config

# Access ban lists
banlist = config.sections["server"]["banlist"]
ignorelist = config.sections["server"]["ignorelist"]
ipblocklist = config.sections["server"]["ipblocklist"]
ipignorelist = config.sections["server"]["ipignorelist"]
Changes to ban and ignore lists are automatically persisted to the configuration file.
from pynicotine.core import core

# Ban a spammer
core.network_filter.ban_user("spammer")

# Ignore an annoying user
core.network_filter.ignore_user("annoying")

# Ban IP range
core.network_filter.ban_user_ip(ip_address="10.0.0.*")

# Check user status
if core.network_filter.is_user_banned("spammer"):
    print("User is banned")

if core.network_filter.is_user_ip_banned(ip_address="10.0.0.5"):
    print("IP is banned")

# Later, unban if needed
core.network_filter.unban_user("spammer")
core.network_filter.unban_user_ip(ip_address="10.0.0.*")