Skip to content

Instantly share code, notes, and snippets.

@etigui
etigui / main.py
Created October 8, 2020 15:01
Pyhton - custom exception
class MyException(Exception):
def __init__(self, message):
super().__init__(message)
@etigui
etigui / main.py
Last active October 8, 2020 15:21
Python - logger configuration to log to file and print to stdout
import sys
import logging
from logging import handlers
from logging.handlers import RotatingFileHandler
LOG_PYTH = '/var/log/logging.log'
LOG_LEVEL = logging.DEBUG
LOG_BACKUP_COUNT = 10
LOG_MB = 1048576
LOG_MAX_BYTES = MB * 10
@etigui
etigui / slack.py
Last active June 15, 2023 21:18
Python - send file with Slack API using post request
# Send file with Slack API using post request
# Under "Add an OAuth scope" you need to add "files:write:user" permission
# https://api.slack.com/methods/files.upload#arg_title
# https://api.slack.com/types/file#file_types
def get_file_content(file_name):
content = ''
with open(file_name, 'rb') as f:
content = f.read()
@etigui
etigui / sort_dict.py
Last active October 14, 2020 12:08
Python - sort dict
mydict = {"carl": 40,"alan": 2,"bob": 1,"danny": 3}
# Sort a dict by key
for key in sorted(mydict.keys()):
print("%s: %s" % (key, mydict[key]))
# OUTPUT
alan: 2
bob: 1
carl: 40
@etigui
etigui / dico_list.py
Last active January 8, 2020 14:25
Appending to list in Python dictionary
from datetime import datetime
dico = dict()
dico.setdefault('42', []).append(datetime.now().strftime("%d-%m-%Y %H:%M:%S:%f"))
dico.setdefault('42', []).append(datetime.now().strftime("%d-%m-%Y %H:%M:%S:%f"))
dico.setdefault('777', []).append(datetime.now().strftime("%d-%m-%Y %H:%M:%S:%f"))
dico.setdefault('777', []).append(datetime.now().strftime("%d-%m-%Y %H:%M:%S:%f"))
print(dico)
@etigui
etigui / main.py
Created January 8, 2020 14:12
Parse Nginx "access.log" file to python object or json
def main():
ng = nginx.Nginx()
logs_parsed = ng.get_obj_parsed_logs('input.log')
for log in logs_parsed:
print(f'{log.date} {log.ip} {log.url} {log.bytes_sent} {log.referrer} {log.user_agent} {log.status} {log.method}')
ng.save_json_parsed_logs('input.log', 'output.josn')
if __name__ == "__main__":
@etigui
etigui / ws.py
Created November 12, 2019 17:35
Simple python web server (GET, POST, HEAD)
#!/usr/bin/env python
"""
Very simple HTTP server in python (Updated for Python 3.7)
Usage:
./dummy-web-server.py -h
./dummy-web-server.py -l localhost -p 8000
Send a GET request:
curl http://localhost:8000
Send a HEAD request:
curl -I http://localhost:8000
@etigui
etigui / repare.md
Last active October 30, 2019 16:47
Centos7: repare corupted/truncated/deleted file (from packages)

Centos7

If you got corupted/truncated/deleted file while updating/writing a bad commands, and as a result you cannot run service/app. You might be interested in this command to reinstall them:

rpm -Va --nodeps --nodigest --noscripts --nosignature --nofiledigest --nomode --nordev --nouser --nogroup --nomtime | grep '/lib64' | awk '/^S..../{system("rpm -qf "$2)}' | sort | uniq | xargs yum reinstall -y
  1. rpm
  • Verifying a package compare information about the installed files in the package with information about the files taken from the package metadata stored in the rpm database. Among other things, verifying compares the size, type, hash of each file.
  1. grep
  • Take only file located where you want to verify,
@etigui
etigui / commented_file.sh
Last active October 30, 2019 14:01
Bash: comment all python file lines
# One line (console)
while IFS= read -r line; do echo "# $line" >> output_commented.json; done < input.json
# Indented
while IFS= read -r line;
do
echo "# $line" >> output_commented.py
done < input.py
@etigui
etigui / list_dict_json.py
Created October 30, 2019 13:35
Python: add list to dict and save as json
name = dict()
name["data"] = list()
with open('file.json', 'a') as myfile:
for d in ["1", "2"]:
name["data"].append({"a": d[0], "b": d[1]})
json.dump(name, myfile, indent=2)