Welcome to jaraco.net documentation!

dnsbl: DNS blocklist support

class jaraco.net.dnsbl.BlocklistHit(host, blocklist, result)

Bases: object

class jaraco.net.dnsbl.Service

Bases: str

Blocklist service. Represents a blocklist service suitable for referencing the reputation of potentially malicious or malfeasant hosts.

Initialize with the domain of the blocklist service or use the classmethod Service.get_services to get Services for a commonly-used set of domains.

classmethod get_services()
classmethod handle_command_line()
lookup(host)
classmethod lookup_all(host)
static reverse_ip(ip)
service_domains = ['dnsbl.jaraco.com', 'zen.spamhaus.org', 'ips.backscatterer.org', 'bl.spamcop.net', 'list.dsbl.org']
jaraco.net.icmp.monitor_cmd()
jaraco.net.icmp.monitor_hosts(hosts)
jaraco.net.icmp.ping(dest_addr, timeout=2)

Send an ICMP Echo request to a host and return how long it takes.

Raise socket.timeout if no response is received within timeout.

>>> ping('127.0.0.1')
datetime.timedelta(...)
>>> ping('10.10.10.254', timeout=.01)
Traceback (most recent call last):
...
TimeoutError: timed out
jaraco.net.icmp.save_result(host, delay)
jaraco.net.icmp.wait_for_host(host)

Continuously wait for a host until it becomes available. When it does, return the datetime when it occurred.

class jaraco.net.importer.HeadRequest(url, data=None, headers={}, origin_req_host=None, unverifiable=False, method=None)

Bases: Request

get_method()

Return a string indicating the HTTP request method.

class jaraco.net.importer.URLImporter

Bases: str

Simple Importer that imports from the network

find_module(fullname, path=None)
find_spec(fullname, path, target=None)
classmethod handles(path)
classmethod install()
classmethod remove()
class jaraco.net.importer.URLLoader

Bases: str

load_module(fullname)

inet.py

Tools for IP communication.

Objects:

PortScanner: scans a range of ports PortListener: listens on a port PortRangeListener: listens on a range of ports

class jaraco.net.inet.PortListener(port)

Bases: Thread

run()

Method representing the thread’s activity.

You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

class jaraco.net.inet.PortRangeListener

Bases: object

listen()
class jaraco.net.inet.PortScanner

Bases: object

add_range(*r)
set_range(*r)
class jaraco.net.inet.ScanThread(address)

Bases: Thread

all_testers: List[Thread] = []
report()
run()

Method representing the thread’s activity.

You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

static wait_for_testers_to_finish()
jaraco.net.inet.ping_host(host)
jaraco.net.inet.portscan(host, ports=range(1024), frequency=20)
jaraco.net.inet.portscan_hosts(hosts, *args, **kargs)
jaraco.net.ntp.handle_command_line()

Query the NTP server for the current time.

jaraco.net.ntp.query(server, force_ipv6=False)

Return current time from NTP server as a Unix timestamp

>>> import time
>>> res = query('pool.ntp.org')
>>> type(res)
<class 'int'>
>>> time.time() - 5 < res < time.time() + 5
True

Routines for handling RSS feeds

class jaraco.net.rss.CombinedFilter(iterable=(), /)

Bases: list

A list of callables that can be applied to a filter call.

jaraco.net.rss.download_enclosures()
jaraco.net.rss.get_feed_as_playlist(url, outfile=sys.stdout, filter_=None)
jaraco.net.rss.launch_feed_as_playlist()
jaraco.net.rss.launch_feed_enclosure()

RSS Feed Launcher

jaraco.net.rss.load_feed_enclosure(url, filter_=None, index=None)
jaraco.net.rss.parse_filter(filter_string)

scanner.py

TCP port scanning utility

jaraco.net.scanner.get_args()
jaraco.net.scanner.get_hosts(host_spec)

Get a list of hosts specified by subnet mask or using a specific range.

>>> list(get_hosts('192.168.0.0/30'))
['192.168.0.0', '192.168.0.1', '192.168.0.2', '192.168.0.3']
>>> list(get_hosts('192.168.0.1-4'))
['192.168.0.1', '192.168.0.2', '192.168.0.3', '192.168.0.4']

Eventually, I want to develop this to work >>> list(get_hosts(‘192.168.0.254-192.168.1.3’)) # doctest:+SKIP [‘192.168.0.254’, ‘192.168.0.255’, ‘192.168.1.1’, ‘192.168.1.2’]

If a pattern is not recognized, assume the input is a valid address. >>> list(get_hosts(‘192.168.0.1’)) [‘192.168.0.1’]

One may also specify named hosts >>> ‘2600:1f18:111:8a0a:61fa:e3e6:ce25:5a31’ in _gha(get_hosts(‘www.jaraco.com’)) True

jaraco.net.scanner.scan()
jaraco.net.scanner.setup_logger(output_level)
jaraco.net.site.make_index_cmd()

Generate an index file for each directory that itself has an index.htm file

class jaraco.net.stats.PingResult(text)

Bases: object

property date
property latency
success()
property type
class jaraco.net.stats.Reader(filename)

Bases: object

get_stats()
class jaraco.net.stats.Server

Bases: object

get_images()
classmethod handle_command_line()
index()
make_image(day, stats)
jaraco.net.stats.get_loss_stats(window)
jaraco.net.stats.get_windows(stats)

Construct rolling windows

jaraco.net.stats.main()
jaraco.net.stats.single_day(stats, day)

skip day-1 days then return only results from the next day

jaraco.net.stats.windows(seq, n=2)

Return a sliding window (of width n) over data from the iterable s -> (s0,s1,…s[n-1]), (s1,s2,…,sn), …

jaraco.net.tcp.get_connect_options()
jaraco.net.tcp.start_echo_server()
jaraco.net.tcp.test_connect()
class jaraco.net.udp.EchoServer

Bases: object

serve(args)
class jaraco.net.udp.Sender

Bases: object

send_message()
jaraco.net.wake.wake_on_lan(self, addr='00:00:00:00:00:00')

Wake up a computer on the network

>>> mgr = Manager()
>>> all(map(len, mgr.get_host_ip_addresses()))
True
>>> all(map(len, mgr.get_host_mac_addresses()))
True

HTTP routines

class jaraco.net.http.Query(query)

Bases: dict

HTTP Query takes as an argument an HTTP query request (from the url after ?) or a URL and maps all of the query pairs in itself as a dictionary. >>> Query(‘a=b&c=3&4=20’) == {‘a’:’b’, ‘c’:’3’, ‘4’:’20’} True >>> Query(’http://www.jaraco.com/?test=30’) == {‘test’:’30’} True >>> Query(’http://www.jaraco.com/’) == {} True >>> Query(‘’) == {} True

jaraco.net.http.get_content_disposition_filename(url)

Get the content disposition filename from a URL. Returns None if no such disposition can be found.

If url is already a response object, it will use its headers. Otherwise, urllib.request is used to retrieve the headers.

>>> url = 'http://www.voidspace.org.uk/cgi-bin'
>>> url += '/voidspace/downman.py?file=pythonutils-0.3.0.zip'
>>> get_content_disposition_filename(url) in (None, 'pythonutils-0.3.0.zip')
True
>>> url = 'http://www.example.com/invalid_url'
>>> get_content_disposition_filename(url) is None
True
>>> url = 'http://www.google.com/'
>>> get_content_disposition_filename(url) is None
True
jaraco.net.http.get_url(url, dest=None, replace_newer=False, touch_older=True)
jaraco.net.http.get_url_filename(url)
jaraco.net.http.headers()
jaraco.net.http.print_headers(url)
jaraco.net.http.wget()

urllib2 HTTP caching support inspired by http://code.activestate.com/recipes/491261/

class jaraco.net.http.caching.CacheHandler(store)

Bases: BaseHandler

Stores responses in a httplib2-style cache object.

default_open(request)

Open the url specified in the request. If it’s an HTTP GET, and the result is a valid cached value, return the cached version.

http_response(request, response)

Handle a HTTP response.

should_cache(response)
class jaraco.net.http.caching.CachedResponse(initial_bytes=b'')

Bases: BytesIO

A response object compatible with urllib.request.response objects but for cached responses.

property age

Return the age of this response, guaranteed >= 0

cached = True
exceeds_max_age(cache_control)
fresh()

Check the max-age and expires headers on this response. Return True if this response has not expired.

fresh_for(req_headers)

Check if this response is fresh in its own right and with respect to the request headers.

classmethod from_response(response)
geturl()
info()
classmethod load(payload)

Construct a CachedResponse from a serialized payload

reload(store)

Force a reload of this response

save()

Produce a serialized version of this response

update_headers(new_headers)
jaraco.net.http.caching.datetime_from_email(str)
jaraco.net.http.caching.get_endpoint_headers(headers)

Given a dictionary-like headers object, return the names of all headers in that set which represent end-to-end (and not intermediate or connection headers).

jaraco.net.http.caching.quick_test()

Quick test/example of CacheHandler

class jaraco.net.http.cookies.Shelf(filename)

Bases: MutableMapping

Similar to Python’s shelve.Shelf, implements a persistent dictionary using jsonpickle.

>>> fn = getfixture('tmp_path') / 'shelf.json'
>>> shelf = Shelf(fn)
>>> shelf['foo'] = 'bar'
>>> copy = Shelf(fn)
>>> copy['foo']
'bar'
>>> shelf['bar'] = 'baz'
>>> Shelf(fn)['bar']
'baz'
class jaraco.net.http.cookies.ShelvedCookieJar(shelf: Shelf, **kwargs)

Bases: CookieJar

Cookie jar backed by a shelf.

Automatically persists cookies to disk.

clear(domain=None, path=None, name=None)

Clear some cookies.

Invoking this method without arguments will clear all cookies. If given a single argument, only cookies belonging to that domain will be removed. If given two arguments, cookies belonging to the specified path within that domain are removed. If given three arguments, then the cookie with the specified name, path and domain is removed.

Raises KeyError if no matching cookie exists.

classmethod create(root: Path = pathlib.Path(), name='cookies.json', **kwargs)
get(name, default=None)

Set a cookie, without checking whether or not it should be set.

class jaraco.net.http.servers.AuthRequest(host, port, timeout, response_delay, outfile)

Bases: Simple

check_auth_response(conn)
serve()
serve_until_auth()
class jaraco.net.http.servers.Simple(host, port, timeout, response_delay, outfile)

Bases: object

static get_args()
get_content(conn, content='', length=0)
static get_content_length(request)
static get_headers(conn)
get_response()
serve()
serve_one()
classmethod start()

Indices and tables