All posts by godson

About godson

Hello, I am Godson at the helm of Lintel. It's nice meeting you !

How to make hello world program in wxPython

In this article we will look at creating a simple hello world program using wxPython. This program will create and display simple window with a big button on it. Up on clicking the button program will exit. Use the following code to create hello world program. You must already have wxPython library installed

Hello world program

import wx

class MainWindow(wx.Frame):
    def __init__(self,parent):
        wx.Frame.__init__(self,parent,title="Hello World")
        self.killButton = wx.Button(self,label="Kill Me")
        self.killButton.Bind(wx.EVT_BUTTON,self.kill)
        self.Show()

    def kill(self,event):
        self.Close()
        print("Bye Bye cruel world")
        
app = wx.App(False)
frame = MainWindow(None)
app.MainLoop()

Explanation

We will go line by line here and try to explain what’s going on in this program. Most of the lines of self explanatory. If you are just getting started in programming the following explanation will be helpful.

  1. Import wxpython library
  2. Inherit from wx.Frame class. This is useful style for most of the programs that you will build. You can create one base frame or window and put rest of GUI widgets on top of it like Text controls, buttons,images, tables etc.
  3. Instantiate the inherited frame the desired title. parent argument is usually None for main windows.
  4. Create a button with label “Kill Me”. The first argument is parent. In this case we use “self” which is the main window we have just created.
  5. Bind the button click event (EVT_BUTTON) of the killButton to kill method. Whenever, EVT_BUTTON event is fired aka the killButton is clicked, kill method will be called.
  6. This line will cause the window to get displayed on screen. It’s customary to call this method after being done with construction of GUI i.e. create main window, place widgets, bind event like we did here.
  7. Create wxPython application by call wx.App. Every wxPython program must have this application.
  8. Start the main loop. Which will hand over control to wxPython library. This post explains why main loop has to be called.

Output

This program will launch the following window. The button takes all the available space on the window since there are no other widgets. You need a few more lines of code to make the button look like what users are used to – small and horizontal. You can exit the program by clicking the button.

How to install wxpython

In this post we will go over the topic of easy way to install wxpython. The following command will install wxpython easily in python3.

pip install wxpython

Python 2

Older versions of wxpython can be installed by downloading binaries (for windows) from sourceforge . The binaries are available for both Windows and MacOS.

Linux

The pip install command should work on linux also. However, if you are stuck on old linux vesions. You can try installing wxpython using your distro’s package manager like apt-get or yum. You can search for exact version and package name available on your platform using the following commands for debian variants.

apt-cache search wxpython

┌──(kali㉿kali)-[~]
└─$ apt-cache search wxpython
gnumed-client - medical practice management - Client
psychopy - environment for creating psychology stimuli in Python
pyscanfcs - scientific tool for perpendicular line scanning FCS
python3-genx - differential evolution algorithm for fitting
python3-opengl - Python bindings to OpenGL (Python 3)
python3-pyface - traits-capable windowing framework
python3-squaremap - wxPython control to display hierarchic data as nested squares
python3-wxgtk-media4.0 - Python 3 interface to the wxWidgets Cross-platform C++ GUI toolkit (wx.media)
python3-wxgtk-webview4.0 - Python 3 interface to the wxWidgets Cross-platform C++ GUI toolkit (wx.html2)
python3-wxgtk4.0 - Python 3 interface to the wxWidgets Cross-platform C++ GUI toolkit
soundgrain - Graphical interface to control granular sound synthesis modules
wxglade - GUI designer written in Python with wxPython
wxpython-tools - Tools from the wxPython distribution

Successful installation

Use the following command to check if the installation is successful or not.

PS C:\Users\godson> python
Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx
>>> wx.version()
'4.1.1 msw (phoenix) wxWidgets 3.1.5'
>>>

As shown above it should print current version of wxpython installed if the installation is successful.

A more extensive tutorial is available here

How to find out CPU make and model in Linux

Use the following simple command to find out make and model of CPU/Processor – cat /proc/cpuinfo

┌──(kali㉿kali)-[~]
└─$ cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 94
model name : Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
stepping : 3
cpu MHz : 2591.996
cache size : 6144 KB
physical id : 0
siblings : 1
core id : 0
cpu cores : 1
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 22
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid tsc_known_freq pni pclmulqdq monitor ssse3 cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single pti fsgsbase avx2 invpcid rdseed clflushopt md_clear flush_l1d
bugs : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit srbds
bogomips : 5183.99
clflush size : 64
cache_alignment : 64
address sizes : 39 bits physical, 48 bits virtual
power management:

There are other commands like

lscpu

dmidecode

But, cat /proc/cpuinfo works reliably on all machines.

How to use Free Clipboard Manager in Windows 10

I have been using clipboard managers for a long time. I came across a variety of clipboard managers both on Linux and Windows. Long ago when I was using KDE 3 Klipper was my favorite clipboard manager. After moving to windows I didn’t find any thing that matched my taste. A few free ones were sluggish and others came with restriction on usage unless you buy them. Number of times I have resisted the idea of writing my own clipboard history manager after failing to find some features I wanted in existing ones out there.

Windows 10 packs a neat little clipboard history manager for some one whose needs are simple. It won’t automatically process links or other text you put into clipboard. It’s pretty basic. But it will save your day with out having to install 3rd party clipboard managers

How to enable Windows Clipboard History

Goto Settings search for Clipboard . Turn on the toggle button under Clipboard History section. Look at the following screen shot

Once you have enabled it. You will be able to summon the clipboard history pop up by pressing Win + V key combination

Ideas are worthless

When building new products or services it’s better to share them with potential customers as early as possible. This will help close the feedback loop. Which is precious when a business is in early phase and haven’t burnt too much cash on the new product.

Start ups operating in stealth mode, shrouded in secrecy, developing the product in private, spend years in making it perfect. Then unveiling it to the public. Though that’s very tempting to entrepreneur. This path ruins careers and empties bank accounts.

I have been on the both sides of this game. There is a service which we have built and shared with potential customers very early on. It became successful. And there are a few products which were developed in stealth mode for a long time and they ultimately went no where.

Stealth mode hinders businesses from early learning opportunities and puts them at a huge disadvantage.

MVP (Minimum Viable Product) /Prototype – is early representation of what your offering will look like. It can rough around the edges. But, try to present it to the potential customers early on in the product’s life cycle. The feedback a business gets from early users is priceless and will help shape the whole thing into a better product that people actually want to use. Coming out of stealth mode and finding out that no one really wants to use the product is a very tough situation to spring back from. At this point one has to battle with lots of psychological effects like Sunk Cost Fallacy

Ideas are cheap or like they say dime a dozen. What really counts is the ability to transform that Idea into usable product/service. The journey from idea to taking the first payment from the customer is fraught with peril.

Unless you are working in an industry where there is heavy competition and competitors with deep pockets that can copy your idea over night, it’s a bad idea to develop the product in the dark.

What is milter?

Every one gets tons of email these days. This includes emails about super duper offers from amazon to princess and wealthy businessmen trying to offer their money to you from some African country that you have never heard of. In all these emails in your inbox there lies one or two valuable emails either from your friends, bank alerts, work related stuff. Spam is a problem that email service providers are battling for ages. There are a few opensource spam fighting tools available like SpamAssasin or SpamBayes.

What is milter ?

Simply put – milter is mail filtering technology. Its designed by sendmail project. Now available in other MTAs also. People historically used all kinds of solutions for filtering mails on servers using procmail or MTA specific methods. The current scene seems to be moving forward to sieve. But there is a huge difference between milter and sieve. Sieve comes in to picture when mail is already accepted by MTA and had been handed over to MDA. On the other hand milter springs into action in the mail receiving part of MTA. When a new connection is made by remote server to your MTA, your MTA will give you an opportunity to accept of reject the mail every step of the way from new connection, reception of each header, and reception of body.

milter stages
milter protocol various stages

The above picture depicts simplified version of milter protocol working. Full details of milter protocol can be found here https://github.com/avar/sendmail-pmilter/blob/master/doc/milter-protocol.txt  . Not only filtering; using milter, you can also modify message or change headers.

HOW DO I GET STARTED WITH CODING MILTER PROGRAM ?

If you want to get started in C you can use libmilter.  For Python you have couple of options:

  1. pymilter –  https://pythonhosted.org/milter/
  2. txmilter – https://github.com/flaviogrossi/txmilter

Postfix supports milter protocol. You can find every thing related to postfix’s milter support in here – http://www.postfix.org/MILTER_README.html

WHY NOT SIEVE WHY MILTER ?

I found sieve to be rather limited. It doesn’t offer too many options to implement complex logic. It was purposefully made like that. Also sieve starts at the end of mail reception process after mail is already accepted by MTA.

Coding milter program in your favorite programming language gives you full power and allows you to implement complex , creative stuff.

WATCHOUT!!!

When writing milter programs take proper care to return a reply to MTA quickly. Don’t do long running tasks in milter program when the MTA is waiting for reply. This will have crazy side effects like remote parties submitting same mail multiple time filling up your inbox.

google apps script

Google Apps Script

What is Google Apps Script?

Google Apps Script (GAS) is a JavaScript based language to script/automate/customize Google Apps (Gsuite products). Google Apps for work has recently changed it’s name to GSuite. The list products is ever increasing, right now there are around 12 Gsuite products. Google Apps Script can be used to script most of these product.

Some interesting stuff you can do with GAS are

  1. Send automated email when Google form is submitted
  2. Finding email with bigger attachment in your gmail account.
  3. Writing add-ons to google docs and sheets

And many more. Google Apps Script offers full blown API to deal all the GSuite products. Unlike traditional languages GAS gets written and executed on google servers. You don’t have to download any thing or go through the pains of setting up development environment.  In this tutorial we will see how to write your first Google Apps Script program.

hello world using google apps script

Visit script.google.com . The interface looks like as shown below.

google app script ide

Start putting together the following program.

function createAndSendDocument() {

var doc = DocumentApp.create('Hello world');
doc.getBody().appendParagraph('Hello from GAS - This doc was created with google app script');

var email = Session.getActiveUser().getEmail();
var subject = doc.getName();
var url = doc.getUrl();
var body = 'Link to your doc : ' + url;

MailApp.sendEmail(lemail, subject, body);

}

What this program does is

  1. Create google document called ‘Hello World’.
  2. Add some text to newly created document.
  3. Get the email addressed of currently logged in user (you).
  4. Extract URL/Link for the created document.
  5. Mail it to the email address.

Now, you can save the script using Save option from File item. Your code file gets saved to your google drive. Later on you can always go back to google drive and open this script to make changes.

That’s it now, all you need to do is run this script either by clicking on Run button or selecting Run from menubar and click on function name displayed there. Pretty cool isn’t it ? You can do lot more interesting stuff using Google Apps Script. Visit the reference link included earlier in this post to find out about all the API calls supported by Google Apps Script.

 

FCM – send push notifications using Python

What is FCM ?

FCM – Firebase Cloud Messaging is a cross-platform  ( Android, iOS and Chrome ) messaging solution that lets you reliably deliver messages at no cost. FCM is best suited if you want to send push notification to your app which you built to run on Android and iOS. The advantage you get is you don’t have to separately deal with GCM (Google Cloud Messaging deprecated now) and Apple’s APNS. You hand over your notification message to FCM and FCM takes care of communicating with apple’s APNS and Android messaging servers to reliably deliver those messages.

fcm-2

Using FCM we can send message to single device or multiple devices.  There are two different types of messages, notification and data. Notification messages include JSON keys that are understood and interpreted by phone’s operating system. If you want to include customized app specific JSON keys use data message. You can combine both notification and data JSON objects in single message. You can also send messages with different priority.

Note : – You need to set priority  to high  if you want phone to wake up and show notification on screen

Sending message with Python

We can use PyFCM to send messages via FCM. PyFCM is good for synchronous ( blocking ) python. We will discuss non-blocking option in next paragraph.

Install PyFCM using following command

pip install pyfcm

The following code will send a push notification to

from pyfcm import FCMNotification

push_service = FCMNotification(api_key="<api-key>")

# OR initialize with proxies

proxy_dict = {
          "http"  : "http://127.0.0.1",
          "https" : "http://127.0.0.1",
        }
push_service = FCMNotification(api_key="<api-key>", proxy_dict=proxy_dict)

# Your api-key can be gotten from:  https://console.firebase.google.com/project/<project-name>/settings/cloudmessaging

registration_id = "<device registration_id>"
message_title = "Uber update"
message_body = "Hi john, your customized news for today is ready"
result = push_service.notify_single_device(registration_id=registration_id, message_title=message_title, message_body=message_body)

print result
 
# Send to multiple devices by passing a list of ids.
registration_ids = ["<device registration_id 1>", "<device registration_id 2>", ...]
message_title = "Uber update"
message_body = "Hope you're having fun this weekend, don't forget to check today's news"
result = push_service.notify_multiple_devices(registration_ids=registration_ids, message_title=message_title, message_body=message_body)

print result

So, the PyFCM API is the pretty straight forward to use.

Sending FCM push notification using Twisted

PyFCM discussed in above paragraph is good enough if you want to send messages in blocking fashion. If you have to send high number of concurrent messages then using Twisted is a good option.

Twisted Matrix
Twisted Matrix

Network operations performed using twisted library don’t block. Thus it’s a good choice when network concurrency is required by program. We can use txFCM library to send FCM messages using twisted

Install txFCM using following command

pip install txfcm

Following code send FCM message using txFCM

from txfcm import TXFCMNotification
from twisted.internet import reactor

push_service = TXFCMNotification(api_key="<api-key>")

# Your api-key can be gotten from:  https://console.firebase.google.com/project/<project-name>/settings/cloudmessaging
# Send to multiple devices by passing a list of ids.
registration_ids = ["<device registration_id 1>", "<device registration_id 2>", ...]
message_title = "Uber update"
message_body = "Hope you're having fun this weekend, don't forget to check today's news"
df = push_service.notify_multiple_devices(registration_ids=registration_ids, message_title=message_title, message_body=message_body)

def got_result(result):
    print result

df.addBoth(got_result)
reactor.run()

txFCM is built on top of PyFCM so all the API call that are available in PyFCM are also available in txFCM.

Twisted Matrix

How to write port-forwarding program using Twisted

Recently I was faced with an issue where a long running process is listening on loop back IP (127.0.0.1) on port 8080 on one of our servers and client programs on other machines are trying to access it on server’s local IP 10.91.20.66.  We ended up at this situation when we have updated server configuration and restarted the server program and forgot to change IP binding info in config file from loop back to local IP. Server got busy with it’s work, with lots of customer’s connections already, by the time we have discovered that some services of  server are not accessible to client programs on other machines. So, the dummy’s guide to fixing it by changing config and restarting the server program is not an option as we can’t risk to disconnect existing customers. So, hot patching is the only option until we can restart the program at next scheduled down time.

I could have fixed this in couple of ways either by adding few lines to iptables configuration or by writing simple socket program in python. The task is to forward data coming in on local IP port 8080 to loop back IP (127.0.0.1) port 8080 and send replies back to source address. Forwarding one socket data to other socket is pretty trivial using Python’s socket library and Twisted made it even more trivial, so I went with the following solution using Twisted.

__author__ = 'godson'

from twisted.protocols.portforward import ProxyFactory
from twisted.application import internet,service

src_ip = "10.91.20.66"
src_port = 8080
dst_ip = "127.0.0.1"
dst_port = 8080

application = service.Application("Proxy")
server = ProxyFactory(dst_ip, dst_port)
ps = internet.TCPServer(src_port,server,50,src_ip)

ps.setServiceParent(application)

That’s it. Now, all I needed to do is to run this program by the following command

twistd -y portforwarder.py

This simple program is made possible by the heavy lifting done by twisted library. Interested folks can look under hood at twisted’s portforward.py module.

FreeSWITCH logo g729a

How to Fix G729a CODEC NEGOTIATION ERROR in FreeSWITCH

Are you facing issue of failing calls that are having G729a codec with 488 response? This article tells you how to fix that issue. When using G729 codec in FreeSWITCH if it receives following SDP in INVITE packet, that call is going to fail with 488 Incompatible Destination printing the error message mod_sofia.c:2226 CODEC NEGOTIATION ERROR.  SDP:

v=0
o=Sippy 3205873754679187826 0 IN IP4 192.168.22.7
s=-
t=0 0
m=audio 53792 RTP/AVP 18 101
c=IN IP4 199.158.22.7
a=rtpmap:18 G729a/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-15
a=ptime:20
a=sendrecv

FreeSWITCH is not the offending party here, but the device that is sending G729 as G729a. The correct encoding name assigned to this codec by IANA is G729 not G729a.

To fix interop quirks like these FreeSWITCH has implemented some options that can be enabled. They are prefixed with NDLB means No Device Left Behind. The particular option that helps us with the current issue is

NDLB-allow-bad-iananame

If you set this option to true in sofia profile as shown below, FreeSWITCH will be more forgiving to devices that are using non standard IANA codec names in SDP.

<param name="NDLB-allow-bad-iananame" value="true"/>

Note:- The situation described in this article has commercial G729 module loaded in FreeSWITCH