...
Just my blog

Blog about everything, mostly about tech stuff I made. Here is the list of stuff I'm using at my blog. Feel free to ask me about implementations.

Soft I recommend
Py lib I recommend

I'm using these libraries so you can ask me about them.

Django middleware for the rescue!

Now I can save visitors locally without external tools and also track a bad acting requests.

Finally, I can have a better working middleware which can catch HTTP status errors, make a redirect-response to the main page and also save a visitor.

Now I can also catch HTTP status codes in the database to see what a bad actor wants to achieve.

Thanks to: LINK

I can now save site visits without external analytics and save a bad example to later expose them in a fancy table.

Only I need to mask IP addresses before I show requests and paths to the public.

TBH I also need to add some load balancing.

    def __call__(self, request: HttpRequest) -> typing.Optional[HttpResponse]:
        """
        Check request for validity here and response with correct answers.
        Use bad codes when needed.
        Save visitor now with status code relation.
        :param request:
        :return:
        """
        try:
            response = self.get_response(request)
        except SuspiciousOperation as e:
            log.error(f"SuspiciousOperation:"
                      f"\nException:\n{e}\n")
            save_visit_task(request, status='SUS')
            return HttpResponseForbidden('CSRF verification failed.')
        except Exception as e:
            log.error(f"General …

Read...

I finally made the comments at this site.

Please use the comments form below any post.
Or leave feedback on the Feedback page if you want to ask a general question.

I use Disqus for comments, you might need to clear your cookies at this site if Disqus fails to load properly.

UPD: Also, I'm saving a lot of data for each HTTP request to get a list of the most common URLs and POST\GET arguments.
It's interesting to see some "smartass" XSS scripts targeting WordPress admin or plugins.

 

Soon I'll create a view at this site with the TOP list of such kinds of requests.

Interestingly I've updated this module to save just visits counters to be able to see visitors without logging into Google Analytics. I can't use that regularly, because my Pi-Hole cuts it. So, as soon as I manage to add a better sorting in this module, I'll show you this …

Read...

Raspberry 5 test-to-speech using Piper

How to make your Raspberry 5 talking with python lib Piper?

I wanted to make my Raspberry tell me about power outages and air alerts near my location.

The setup is pretty simple and described ar the repo:

I didn't want to waste my time making yet another Python venv and that's why I've chosen a simple binary mode.

Setup

Now about the setup:

  • Raspberry 5.
  • USB sound card with 3.5 jack.
  • USB-powered speaker with a 3.5 jack.

Sound

CMD to change the volume programmatically:

amixer set Master 80% >> /dev/null

 Sound devices and controls

You may have a problem with playing generated WAV files and making voice directly. So you need to choose devices for a different output.

To list devices run:

# List devices
aplay -L
# I only have USB card:
hw:CARD=Audio,DEV=0
     USB Audio, USB Audio
    Direct hardware device without any conversions
plughw:CARD=Audio,DEV=0
     USB Audio, USB Audio
    Hardware device with all software conversions
sysdefault:CARD=Audio
     USB Audio, …

Read...

Installing NUT - Network Ups Tool Notifications and emails

Installing NUT - Network Ups Tool Notifications and emails

Notifications and email

Just a simple work log of installing NUT and configuring email notifications from it.

 

Simple

 Go to file:

sudo vi /etc/nut/upsmon.conf
Add at the EOF:
# Email script for NOTIFYCMD
# Simple
NOTIFYCMD "/etc/nut/notifycmd.sh"

# Notification events
NOTIFYFLAG ONLINE     SYSLOG+WALL+EXEC
NOTIFYFLAG ONBATT     SYSLOG+WALL+EXEC
NOTIFYFLAG LOWBATT    SYSLOG+WALL+EXEC
NOTIFYFLAG FSD        SYSLOG+WALL+EXEC
NOTIFYFLAG COMMOK     SYSLOG+WALL+EXEC
NOTIFYFLAG COMMBAD    SYSLOG+WALL+EXEC
NOTIFYFLAG SHUTDOWN   SYSLOG+WALL+EXEC
NOTIFYFLAG REPLBATT   SYSLOG+WALL+EXEC
NOTIFYFLAG NOCOMM     SYSLOG+WALL+EXEC
NOTIFYFLAG NOPARENT   SYSLOG+WALL+EXEC

 

Simple notification script

 

sudo vi /etc/nut/notifycmd.sh

 

#!/bin/bash
EMAIL='to@YOYR_EMAIL.com'
echo -e "Subject: UPS ALERT Basic: $NOTIFYTYPE\n\nUPS: $UPSNAME\r\nAlert type: $NOTIFYTYPE\n\n\nThis is a basic notification!" | msmtp $EMAIL
Exec rights
 
# Change group to nut
sudo chown :nut /etc/nut/notifycmd.sh
# Add execution
sudo chmod 774 /etc/nut/notifycmd.sh
# Restart the NUT services
sudo systemctl restart nut-server.service
sudo systemctl restart nut-driver.service
sudo systemctl restart nut-monitor.service
 

Extra notification script by Scheduler

Read...

Installing NUT - Network Ups Tool

Install and use Network Ups Tool - an open source Uninterruptible Power Supply software

I'll describe a full path on how to install and use NUT.
My implementation is using Raspberry Pi 5 and a TP-Link router attached to 800W UPS.
RPi will be a brain unit which will send notifications to email and make a voice notification about power outages and battery levels.
 
My current surge and power loss protection scheme is the following:
  • UPS with connected devices:
    • LG TV
    • Soundbar
    • NUC as a media centre (with NUT monitor for Windows)
    • RPi5 as brain unit with NUT, Pi-Hole and DHCP.
    • TP-Link Router.

In case of a power-loss event, PRi will notify me via email, NUC will power off on its own, TV can be powered off manually.

Powered-off TV and soundbar are neglectable in total power usage on battery, so I have about 1,5 to 2 hours of running Raspberry 5 and TP-Link to keep my internet on.

Now to the setup. …

Read...

WSGI mod, Python, Django, Celery and a nice fella virtualenv

Old

Here I'll show how to use Python (3.8.0 in the current example, but legit for latest 3.+ versions, not for os.name == 'NT') and virtualenv to setup WGSI enabled website on Django (or any) with implemented Celery + Flower + RabbitMQ. This example is my own path used for automated test framework: Octopus (not available now, but have dev branch opened: Lobster)

 

Here only configurations and setup for Python 3.8.0, virtualenv, WSGI, Django, Celery (worker and beat services), Flower and that's it. Later I'll show Django + Celery configuration when they run together (sort of).

 

You may want to install Python at first: Read

 

When your modern and fancy python is installed, we'll make a virtual environment for our website. Never underestimate a power of virtualenv as I did, because since you start to use it, you will never understand how did you work without them …

Read...

Centos Python Installation Guide with no Pain

Old me installing Python

Centos 7 + Python 3(4,5,6,7,8) installation guide with no pain. As for me.

 

I can't even say how many times I have to update my python executables for web servers and how much pain I feel during this routine.

 

Usual Google\StackOverflow advice only describes one particular problem. But there're few of them, and they're different so you spent hours trying to collect them all and pick the right decision.

 

Then you just drop it and use yum for any latest python version available!

 

Here I'll show you and make a reminder for myself about how to install modern python in the best way, once, without pain.

 

I use Centos 7 typical install as for WebServer (minimal install could require to install tonnes of dev-libs and yum, be sure you prepared them before)

 

1. Preparations:

 

You need to install some compil libs.

 

yum …

Read...

Test design and implementation.

Old stuff, very old

 

Very beginning

 

Test design

 

Our test system has a pretty simple configuration, so I do not want to bother you with the detailed description of it. Simply - it's just a standalone execution of a test.py file with all requirements (test setup and tear down) in local internal test utility. Helper local utility has a usual amount of preparation functions, test data loading and some other configurations. In the very end of test loading, it just compares a node-set result of "real" scan (software and hardware nodes in a database after the scan) with predefined node-set (from test.py). Node set we usually expect at the end of the test should be added by the developer when product discovery pattern is ready or have a new feature or fix.

 

Now I'm trying to implement entirely new and somewhere different test design because current is not …

Read...

Test automation or: How I Learned to Stop Worrying and Love Async __init__

Old stuff

Morning everybody…

 

I recommend reading this part of the blog only in the morning with a cup of dark and strong coffee.

 

I'll show you some new cases, and howto's to help you automate routines in the developer's life. There will be an example of my fails and win on different scenarios. The poor code I'll include also.

 

Very beginning

 

Firstly - I'll try to make this thread in English. And maybe later when I have time - to make Ukrainian and Russian translations, I'll make them.

 

"Octopus" story has started from a small plugin for Sublime Text editor. Later it will be ported to Atom.

 

I've started developing "Octopus" on spring-summer 2016, and I've not finished it yet! New functions and features were implemented continuously. Product transforms into a framework.

 

Now all processes executed automatically and simultaneously for 24/7.  "Octopus" has …

Read...

Python - Django Celery - кейс длиною в вечность.

Вкратце: когда добавляешь пачку тасок на Celery worker'ы, случается так, что парочка из них отваливается или засыпает или еще чего. Долго не доходили руки поправить это недоразумение, поэтому я просто рестартовал все воркеры раз в пару дней или прямо перед началом проверки рутины, в общем очень сложно было поймать и воспроизвести тот случай, когда без каких-либо ошибок воркер просто молчал. И наконец дошоли руки запилить кривофикс, время покажет насколько он толковый, суть в следующем: В сразу после момента определения свободного воркера  - запускать app.control.heartbeat() и app.control.ping(timeout=10) и если ответ пришел (в виде [{'alpha@host': {'ok': 'pong'}}]) - значит воркер готов и можно передавать ему таску, пачку тасков, рутину и тп. Если ответ пришел отличным от этого - даем ошибку и ничего не запускаем далее. При плохом раскладе пользователю придется просто перезапустить задачу посредством нажатия кнопочки еще раз, а вот запланированная таска - умрет. Общий вид на код примерно такой: Набросавши …

Read...