Fix UTF-8 Issue with Python Logger

When you work with Python Logger to STDOUT, sometimes you got error messages as the below:

The reason is STDOUT does not support UTF-8 encoder.

I try to google to solve the issue. And I got the solution. We have to create environment variable PYTHONIOENCODING=utf-8

So It works.

Send SMS from Raspberry Pi with USB 3G

In Digital Marketing Strategy, email and SMS are the important tools to improve your  customer services. And Raspberry Pi is a good platform to help us to implement sending email or SMS.

In this weekend, I try to send SMS from Raspberry Pi with USB 3G, and i was successful.  I used a Raspberry Pi 2 model B, and a USB 3G (I am in Vietnam, so I bought a Viettel USB 3G).

Send SMS with Raspberry Pi and USB 3G Viettel

Send SMS with Raspberry Pi and USB 3G Viettel

At the first step, plug USB 3G in Raspberry Pi, and restart Pi.
From the terminal, type lsusb command:

And using dmesg command, find ttyUSB:

If you see the output above, it means that USB 3G connection is correct.

Now, we need to install a software to help us connect to serial port ttyUSB. That software is picocom:

After that, try to connect ttyUSB1 with command:

If you see the output has “terminal ready” and you try to type “AT” and receive the response “OK”. It means that your connection is prefect. Now, you type “AT+CMFG=1” to change to “text mode”

And try to send a SMS message with command:

Finally, press Ctrl + a + x to exit picocom terminal.

If you send SMS successfully with picocom. And now, we write a Python script to send SMS message.  You can see the source code below. It is so simple.

To build a high performance SMS sender, we should apply Redis Pub/Sub to push all messages need to send to Queue.

Good luck!

Run a Flask application in Nginx uwsgi

As you know uWSGI is the most popular to deploy a Python Application.  And Nginx is a powerful web server to run website in production. So this post will show a simple way to run your application (uses Flask Framework)  with WSGI and Nginx


Peace. It does not mean to be in a place where there is no noise, trouble or hard work. It mean to be in the midst of those things and still be calm in your heart

Create wsgi.py in your root directory of the application:

(Please make note that you understand the meaning of the source code above. So you can modify it to match your situation) 

And a config file for WSGI – wsgi.ini:

Install uwsgi with PIP:

Test your uWSGI config with command below:

Make sure that WSGI works with your application properly. Now, you can config NGINX to run over socket file(your_app_name_here.sock). Please create nginx config file with content below

Enable your site in Nginx:

And restart Nginx to apply your changes:

Test your site now!!!

Create upstart script:

With content:


A simple examle for Python Kafka Avro

In the weekend, I try to use Python to write a producer and a consumer for Apache Kafka. I found Kafka-Python library that can help me do it easily. However, If you try to send Avro data from Producer to Consumer, it is not easy. You have to understand about them. We have enough specifications but there is no example source code. So this is a simple example to create a producer (producer.py) and a consumer (consumer.py) to stream Avro data via Kafka in Python.

The wise man never knows all, only fools know everything.

To run this source code, please make sure that you installed Kafka (https://sonnguyen.ws/install-apache-kafka-in-ubuntu-14-04/) and Python libraries (kafka-python, avro, io). And I am using Python 2.7

Create producer.py

Create consumer.py

Time for test:

I hope that this post will help you say “Hello” to Kafka, Python and Avro

Please see the details in GitHub: https://github.com/thanhson1085/python-kafka-avro

In the source code repository above, I also created consumer_bottledwater-pg.py to decode avro data that pushed from bottedwater-pg Kafka producer. This base on the question in Stackoverflow