Creating the log file and setting the permissions: $ sudo mkdir /var/log/netbox $ sudo touch /var/log/netbox/netbox.log $ sudo chown -R netbox.netbox /var/log/netbox $ Configuring NetBox (in /opt/netbox/netbox/netbox/configuration.py): You can set the level for the loggers as needed, in this setup the file handler accepts DEBUG and higher but the actual loggers (“django” and “netbox”) only […]
Tag: python
Basic Mocking with Pytest
Since I always seem to search for these examples over and over again, here it is, basic mocking with pytest. My example code is this (testmodule.py): It can be run: $ python3 testmodule.py In testfunc, sleeping $ Not surprisingly, running it takes about five seconds. The time.sleep() call simulates some long-running operation in the code. […]
Handling Retries in Python Requests
In my previous post we handled timeouts with requests. This post deals with making it easier to react to the errors using the built-in retry features. In this context let’s limit the retry-requiring cases in two categories: Cases that timed out (no response from the server) Cases that returned a transient error from the server […]
Handling Timeouts with Python Requests
The most usual way of making HTTP/HTTPS requests in Python applications is using the requests library. By default it doesn’t define any timeouts for the operations. It means that in specific circumstances a simple requests.get() call might not return at all. Depending on the nature of the application this is not desirable. This post demonstrates […]
Saving Gzip Files in Python
I have various Python applications that run periodically and deal with data in lists and dictionaries, and they save the processed and/or discovered data as JSON in text files for logging purposes or other later use. Typically the files are quite large and take up substantial amount of disk space. Since the applications run on […]
Selecting Pynetbox Version
If you have: And you need: Then you need to use: Because: NetBox 3.7.0 or newer VPNs pynetbox 7.3.3 or newer vpn app was added in pynetbox NetBox 3.5.0 or newer ASN ranges pynetbox 7.1.0 or newer asn_ranges.available_asns was added in pynetbox NetBox 3.3.0 or newer Cable terminations pynetbox 7.0.0 or newer Cable terminations were […]
Reading XML with Python
In an earlier post I wrote about reading CSV files with Python. This time let’s read some XML using just Python standard library, the ElementTree XML API. For the demonstration I’ll use this short XML output from Nmap: Originally the Nmap output was not indented at all but I copied it to VSCode and reformatted […]
Sending DHCP Discover with Python and Scapy
Simple example how to send a UDP packet with Scapy in a Python application: When using sendp() it is necessary to construct the full packet, starting from L2 Ethernet frame. The “/” operator is cleverly used when combining the layers of the packet.
Inter-Process Communication with Named Pipes between Python and PowerShell
I have a hunch that I may later need IPC between Python and PowerShell applications on Windows, so I did some research, and managed to make a small example of IPC using named pipes. Here the PowerShell app is the server, waiting for connections, and Python app is the client. simplepipeserver.ps1: simplepipeclient.py: The Python app […]
Comparing Version Numbers in Python
The distutils module is going to be deprecated in Python 3.10, and to be removed in Python 3.12. Hence, my current standard way of comparing version number strings is using pkg_resources module (part of setuptools): >>> from pkg_resources import parse_version as version >>> version(“1.0”) <Version(‘1.0’)> >>> version(“1.8.9”) < version(“2.0”) True >>> version(“2.0.9”) < version(“2.1”) True […]