I found pathlib standard library in Python just recently. It is a full set of classes, methods, properties and operators that make it very easy to work with files, directories and paths in operating system-independent way. You all do remember that not all filesystems use / (slash) as the path separator…? I had a specific […]
Tag: python
Reading CSV Files With Python
It’s not rare that as a technical worker you get some data in CSV format (comma-separated values), and the data has to be read and interpreted some way. MS Excel is the default tool for some to handle those cases, but I’m one of those that never got used to using Excel in external data […]
Using Pynetbox
Pynetbox is a Python API client library for NetBox. Here are some basic examples of using pynetbox. In this page: Getting started Creating and changing something Retrieving objects Deleting objects Handling object status Using tags Referring to other objects Here we assume that you already have NetBox running (I have NetBox 2.8.6 in this demo), […]
Subcommands with Argument Parsing in Python
An example how subcommands can be used with argparse module: Sample output: markku@btest:~/temp$ python3 argtest.py usage: argtest.py [-h] [-c CONFIG_FILE] {update-device,add-vlan} … positional arguments: {update-device,add-vlan} update-device update device data add-vlan add a VLAN optional arguments: -h, –help show this help message and exit -c CONFIG_FILE, –config-file CONFIG_FILE specify the configuration file to read (default: argtest.cfg) […]
Get Linux Distribution Name and Version with Python
Since Debian 10 Buster there is no minor version number in lsb_release output or /etc/os-release anymore. Bug has been filed, but apparently there is no will to get the full version number back. The dist() and linux_distribution() functions in the platform module in Python have been deprecated in 3.5 and removed in 3.8.0: Also, the […]
Even Better Webhook Listener
I made some improvements to the NetBox webhook listener presented in my earlier post. Here is the new one: Example from the log file: Notable changes: Added the message digest verification (X-Hook-Signature header sent by NetBox) Added content length checking as an example of all the worries you need to take care of when publishing […]
Webhook Listener for NetBox
Note: There is an improved version in the next post. Following my earlier post about implementing a REST API, here is a simple listener for NetBox webhooks: webhook-listener.py: With the other configurations described in the earlier post (and after configured NetBox to send the webhook POST requests to http://my.server.local/webhook/), these are the outputs: webhook-listener.log: Automatically […]
My First REST API with Python and Flask-RESTPlus
With few or no explanations, on Debian 10: The actual API app, in ~/resttest/resttest.py: ~/resttest/wsgi.py: /etc/systemd/system/resttest.service: /etc/nginx/sites-available/resttest: Final setup steps: Testing results: Also note that the Swagger UI is automatically accessible in http://my.server.name/: Some future considerations: TLS encryption (= reconfiguring NGINX) Authentication for the API Disabling/customizing the Swagger UI Links: Flask Flask-RESTPlus NGINX The post […]
Version Numbering in Python Package
A revisit for this topic is available in: Revisited: Version Numbering in Python Package After written the short Python package post I realized that the version number (actually string but anyway) of the package is now in two different places: in the package code (__init__.py) and in the package install metadata (setup.py). Obviously that’s not […]
Creating a Python Package, the Cruel Way
This is how you can create a package of Python code so that you can use the module in various projects. Let’s assume this is the code your module has: STUFF_VERSION = “1.1-dev” def get_stuff(): return “This is stuff. Use it wisely.” Of course the module can be much more complicated with various functions and […]