Skip to content

Instantly share code, notes, and snippets.

@bockor
bockor / gist:fd1871f97adc9eae3064c96811b19951
Created February 13, 2023 14:33
python sorting ipaddresses
# try to sort ipv4 addr as strings
ss = ['114.122.102.2','114.122.11.1','118.123.12.13','122.14.113.3','192.144.1.5']
print([str(s) for s in sorted(ss)])
# this does not look righ to me ... let's invoke the ipaddress module
from ipaddress import IPv4Address
# and create the ipv4 addr instances
sso = [IPv4Address(s) for s in ss]
#print the sorted ipv4 addr
print([str(s) for s in sorted(sso)])
@bockor
bockor / gist:190656c3e75fc9716d3d93fd3907bb8b
Last active May 24, 2021 09:07
python difference two dates in years
import datetime
d1_date_str='29/08/1962'
d2_date_str= '06/06/1968'
d1_date_obj=datetime.datetime.strptime(d1_date_str, '%d/%m/%Y')
print(d1_date_obj.date())
d2_date_obj=datetime.datetime.strptime(d2_date_str, '%d/%m/%Y')
print(d2_date_obj.date())
#create datetime.timedelta object
diff = d2_date_obj - d1_date_obj
print(type(diff))
import zipfile
from datetime import datetime
# create zip file name
zip_file_name = 'myzip_{}.zip'.format(datetime.now().strftime("%d%m%Y"))
# create a ZipFile object and compress
zipObj = zipfile.ZipFile(zip_file_name, 'w', zipfile.ZIP_DEFLATED)
# Add multiple files to the zip
@bockor
bockor / gist:9a15788b9b1a27da06fc8463a71ad8b6
Last active March 3, 2021 13:08
uwsgi-as-a-systemd-service with python3 and bottle framework
*****************************************************
*** APPROACH 1: install uwsgi as a system package ***
*****************************************************
install packages
----------------
apt install uwsgi uwsgi-plugin-python3
Configure uwsgi daemon
----------------------
@bockor
bockor / gist:8264a1ae4bf54e2fbc00121dc8dd0e67
Last active March 3, 2021 12:14
infoblox notes // ipam utilization + tls/ssl unsecure messages surpression
import requests
import urllib3
import json
# Surpress SSL/TLS >Insecure Warnings
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
session = requests.Session()
session.auth = ('admin', 'infoblox')
@bockor
bockor / gist:d1a1547b9e37a92f45c2c5fc14b3a096
Created March 3, 2021 11:51
ipython // count number of files per file extension type in directory
from collections import Counter
file_list=!find /home/bruno/tbc/uwsgi/ -type f -name "*"
#print(file_list)
found_ext=[]
for f in file_list:
found_ext.append(f.split('/')[-1].split('.')[-1])
#print(found_ext)
print(Counter(found_ext))
@bockor
bockor / gist:ff2aef9f5be1ea98ac6fb927bb460d7d
Last active February 22, 2021 02:40
gunicorn-as-a-systemd-service with python3 and bottle framework
Gunicorn, or Green Unicorn, is a UNIX-compatible WSGI HTTP server that is commonly used to run Python applications.
Developed in 2010 by Benoit Chesneau, this open source project is similar to uWSGI, mod_wsgi, and CherryPy.
Gunicorn is often implemented with a reverse proxy server like NGINX, which typically handles requests for static resources and then passes on the requests to Gunicorn.
Gunicorn processes the dynamic portion of the request and returns a response to NGINX, which sends the response back to the client.
Gunicorn can be used to serve Python applications and is compatible with frameworks like Django, Flask and Bottle.
It’s easy to configure, lightweight, and only needs 4–12 worker processes to handle hundreds or thousands of requests per second.
# create python3 virtual environment
# ==================================
@bockor
bockor / gist:6d326773706a7d03d5462a817e072c26
Last active February 17, 2021 08:48
Infoblox notes // WAPI (Web API) Sample Code for NIOS 8.4.4 using python
'''
Ref: https://community.infoblox.com/t5/DNS-DHCP-IPAM/WAPI-Web-API-Sample-Code-for-NIOS/m-p/105/highlight/true#M12
Sample -- sample python application for WAPI
'''
import sys
import json
@bockor
bockor / gist:b062e29bd69527bf00ab0cf1c4c97139
Created February 14, 2021 17:54
docker // python in bottle
https://hub.docker.com/r/d3fk/python_in_bottle/
docker pull d3fk/python_in_bottle
docker run -itd --name nra_site_inventory --rm -p 8080:8080 -v $(pwd)/yEd-SiteInventory:/usr/src/myapp -w /usr/src/myapp d3fk/python_in_bottle:latest python site_inventory.py
# This prevents Python from writing out pyc files
docker run -itd --name nra_site_inventory --rm -e PYTHONDONTWRITEBYTECODE="1" -p 8080:8080 -v $(pwd)/yEd-SiteInventory:/usr/src/myapp -w /usr/src/myapp d3fk/python_in_bottle:latest python site_inventory.py
docker stop nra_site_inventory
@bockor
bockor / gist:0c0f4a1cc9a45ab9237c359967ce2fe5
Created January 31, 2021 09:12
Remove Bytecode (.pyc) Files in Linux
find . -type f -name "*.py[co]" -delete -or -type d -name "__pycache__" -delete