Conduct a Range of ip Network Address Ping using Python

Intoduction

I would like to conduct a range of ip network address ping using python 3.5 script. It is easy. I will show you the python script and its screen dump result as below.

Python program script

# network ping program run for python3
# Import modules
import subprocess
import ipaddress

# Prompt the user to input a network address
net_addr = input(“Enter a network address in CIDR format(ex.192.168.1.0/24): “)

# Create the network
ip_net = ipaddress.ip_network(net_addr)

# Get all hosts on that network
all_hosts = list(ip_net.hosts())

# Configure subprocess to hide the console window
info = subprocess.STARTUPINFO()
info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = subprocess.SW_HIDE

# For each IP address in the subnet,
# run the ping command with subprocess.popen interface
for i in range(len(all_hosts)):
    output = subprocess.Popen([‘ping’, ‘-n’, ‘1’, ‘-w’, ‘500’, str(all_hosts[i])], stdout=subprocess.PIPE, startupinfo=info).communicate()[0]
    
    if “Destination host unreachable” in output.decode(‘utf-8’):
        print(str(all_hosts[i]), “is Offline”)
    elif “Request timed out” in output.decode(‘utf-8’):
        print(str(all_hosts[i]), “is Offline”)
    else:
        print(str(all_hosts[i]), “is Online”)

Screen dump result as below:

network-ping

Integrate Django with Apache

Introduction

As production run of a Django application, we can use tradition web server tools such as apache, IIS, etc. For example, I will show you to setup django running under apache as below.

Step 1 Install wsgi

Suppose you have already installed python, django, apache, then you need to install wsgi by running command:

sudo apt-get install libapache2-mod-wsgi

Step 2

Edit /etc/apache2/sites-available/000-default.conf as below:

<VirtualHost *:80>
        ServerName www.goldman168.no-ip.org
        #ServerAllas www.localhost
        ServerAdmin goldman.au168@gmail.com

        DocumentRoot /var/www/html/django-survey-master/
        WSGIScriptAlias / /var/www/html/django-survey-master/survey/wsgi.py

        Errorlog /var/www/logs/error.log
        CustomLog /var/www/logs/custom.log combined
</VirtualHost>

Step 3.

Edit /etc/apache2/apache.conf to add the following line to the end:

WSGIPythonPath /var/www/html/django-survey-master

Step 4.

Restart apache service by running command:  /etc/init.d/apache2 restart

Step 5.

Now, you do not need to run the django command (python manage.py runserver) to start the django application, you can browse the django application with link http://localhost/ via apache.

Setup a Survey Web Site with Python & Django

Introduction

This document shows how to setup a survey web site using python 2.7 and django 1.4. We do not need to use web server software such as IIS or Apache while python can start web server service itself. This survey web site can run under window and linux env provide that it has python. I will show its setup steps as below.

Step 1.

Download the program source code “django-survey-master.zip” from web site https://github.com/jessykate/djngo-survey and unzip it to a computer with python 2.7.

Step 2.

Under the django-survey-master directory, run the command $ pip install -r requirements.txt to install django.

Step 3.

Setup the survey database as command $ python manage.py syncdb , which you need to input a username and password.

Step 4.

Start the survey server as command $ python manage.py runserver ,as below screen dump:

survey1

Step 5.

Setup a survey as below screen dump with link http://127.0.0.1:8000/admin :

survey3

survey4

Step 6.

Browse the survey web site to fill-in a survey as below link http://127.0.0.1:8000 :

survey2

survey5

Bonus of a problem solving

Problem 1: The Survey web site is with link localhost:8000 and 127.0.0.1:8000 in its computer and that works fine. However, it cannot be accessed from other computer in the same network.

Solution 1: Start the Web Site Server with its own ip address, such as $ python manage.py runserver 192.168.5.105:8000; then other computers in the same network can access it with link http://192.168.5.105:8000.

Problem 2: If you have problem of running http://127.0.0.1:8000/admin, it comes out a error message “Admin Site: TemplateDoesNotExist at /admin/ …”

Solution 2: Force to re-download django with command:

pip install -r requirements.txt --ignore-installed --force-reinstall --upgrade --no-cache-dir

Temperature Sensor Setup on Raspberry Pi

Introduction

Today, I setup my raspberry pi as a temperature sensor. I mainly use a LM35 temperature sensor and an IC “ADC0804” as an analog to digital converter. I also setup a database to record down the temperature data and then display result on web browser with Chat format. The connection of the whole electronics circuit is as below diagram. temp0

Program script:

1) The coding the a python program to take temperature data is as the temp_url.py program below:

#!/usr/bin/env python
# author: Powen Ko    Program Name:  temp_url.py
import time, RPi.GPIO as GPIO
import urllib

def fetch_thing(url, params, method):
params = urllib.urlencode(params)
if method==’POST’:
f = urllib.urlopen(url, params)
else:
f = urllib.urlopen(url+’?’+params)
return (f.read(), f.code)

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.IN)
GPIO.setup(11, GPIO.IN)
GPIO.setup(12, GPIO.IN)
GPIO.setup(13, GPIO.IN)
GPIO.setup(15, GPIO.IN)
GPIO.setup(16, GPIO.IN)
GPIO.setup(18, GPIO.IN)
GPIO.setup(22, GPIO.IN)
while True:
     a0 = GPIO.input(7)
     a1 = GPIO.input(11)
     a2 = GPIO.input(12)
     a3 = GPIO.input(13)
     a4 = GPIO.input(15)
     a5 = GPIO.input(16)
     a6 = GPIO.input(18)
     a7 = GPIO.input(22)
     total=a0+(a1*2)+(a2*4)+(a3*8)+(a4*16)+(a5*32)+(a6*64)+(a7*128)
     temp=total*5*1000/256/10;
     print a7,a6,a5,a4,a3,a2,a1,a0,”[“,total,”]”,”[C=”,temp,”]”
     content, response_code = fetch_thing(
                    ‘http://127.0.0.1/settemp.php’,
                    {‘id’: 1, ‘temp’: temp},
                    ‘GET’
                    )
     time.sleep(5)

2) Then, I save the temperature data into the database via a web php program as below:

<?php
$con=mysqli_connect(“localhost”,”root”,”infotech”,”raspberryDB”);
if (mysqli_connect_errno()) {
echo “Failed to connect to MySQL: ” . mysqli_connect_error();
}

$now= date(‘Ymdhms’);
$id = $_GET[‘id’];
$temp = $_GET[‘temp’];
mysqli_query($con,”INSERT INTO temp (datatime,temp,userid)
VALUES ($now,$temp,$id)”);

mysqli_close($con);
echo “powenko.com get it”.”, date time=”.$now.”, temp=”.$temp.”, id=”.$id;
?>

Finally, I view data by using a php web browser program as below and also screen dump result as below:

<!doctype html>
<html>
        <head>
                <title>Bar Chart</title>
                <script src=”Chart.js-master/Chart.js”></script>
        </head>
        <body>
                <div style=”width: 50%”>
                        <canvas id=”canvas” height=”450″ width=”800″></canvas>
                </div>
<?php
$con=mysqli_connect(“localhost”,”root”,”infotech”,”raspberryDB”);
if (mysqli_connect_errno()) {
  echo “Failed to connect to MySQL: ” . mysqli_connect_error();
}

$result = mysqli_query($con,”SELECT * FROM temp”);

echo “<table border=’1′>
<tr>
<th>Date Time</th>
<th>Temperature</th>
<th>user ID </>

</tr>”;

while($row = mysqli_fetch_array($result))
{
  echo “<tr>”;
  echo “<td>” . $row[‘datatime’] . “</td>”;
  echo “<td>” . $row[‘temp’] . “</td>”;
  echo “<td>” . $row[‘userid’] . “</td>”;
  echo “</tr>”;
  $Lables=$Lables.'”‘. $row[‘datatime’].'”,’;
  $temps=$temps.'”‘. $row[‘temp’].'”,’;
}

echo “</table>”;
mysqli_close($con);
?>

        <script>
var barChartData = {
                labels : [<?php echo  $Lables;  ?>],
                datasets : [
                        {
                                fillColor : “rgba(20,20,20,0.5)”,
                                strokeColor : “rgba(220,220,220,0.8)”,
                                highlightFill: “rgba(220,220,220,0.75)”,
               highlightStroke: “rgba(220,220,220,1)”,
                                data : [<?php echo  $temps;  ?>
                                ]
                       }
                ]
        }
        window.onload = function(){
                var ctx = document.getElementById(“canvas”).getContext(“2d”);
                window.myBar = new Chart(ctx).Bar(barChartData, {
                        responsive : true
                });
        }
        </script>
        </body>
</html>

temp3

Speech with Raspberry Pi

Introduction

Raspberry Pi is an excellent automation control unit, and we can use it to build a voice recognition feature in order to make it as a voice automation control unit. Yeah, is it very interesting ! In the following, I will show you the script to build voice feature, including converting speech to text, converting text to speech, auto-reply a text question.

Script to convert Speech to Text

I take the benefit of using google speech recognition ver 2 feature and arecord feature of Raspberry Pi. To remind that you should apply your google api key for usage in this script, as below:

#!/bin/bash
echo “Recording…”
arecord -D plughw:1,0 -f cd -t wav -r 16000 –duration=4 test.wav
avconv -i test.wav -y -ar 16000 -ac 1 test.flac

echo “Processing…”
wget -q -U “Mozilla/5.0” –post-file test.flac –header “Content-Type: audio/x-flac; rate=16000” -O – “https://www.google.com/speech-api/v2/recognize?client=chromium&lang=en_US&key=AIzaSyB0RJilwaAhMpftgmgRhgEzd4lZnia1MwQ” |cut -d” -f8 >stt.txt
echo “You said: “
value=`cat stt.txt`
echo “$value”

The screen dump result to run speech2text.sh program is as below:

speech2text

Script to Auto-Reply a Query

It is a python program using Wolframalpha’s API add-on tools to process a question as below script, and you should apply a app ID from http://products.wolframalpha.com/api/:

import wolframalpha
import sys

# Get a free API key here http://products.wolframalpha.com/api/
# This is a fake ID, go and get your own, instructions on my blog.
app_id=”VWQU6P-YPRRG752XH”

client = wolframalpha.Client(app_id)

query = ‘ ‘.join(sys.argv[1:])
res = client.query(query)

if len(res.pods) > 0:
    texts = “”
    pod = res.pods[1]
    if pod.text:
        texts = pod.text
    else:
        texts = “I have no answer for that”
        # to skip ascii character in case of error
    texts = texts.encode(‘ascii’, ‘ignore’)
    print(texts)
else:
    print(“Sorry, I am not sure.”)

To run the python program as script –> python3 queryprocess.py “What is your name”, then we can get a reply result as “My name is Walfram|Alpha.”, as below screen dump.

speech-reply

Convert Text to Speech

We can use Espeak utility to convert text to speech under Raspberry Pi. Its installation is very simple as below:

$ sudo apt-get install espeak

After installation, you can run the espeak program to speech, for example,

$ espeak "Hello World"

Question and Answer Program with Speech and Voice Reply

I develop a script to combine the above three program into one, so that you can use it to speech a question and wait for a voice answer. Let’s see the script of main.sh program as below:

#!/bin/bash
echo “Recording… Press Ctrl+C to Stop.”
./speech2text.sh > /dev/null 2>&1
QUESTION=$(cat stt.txt)
echo “Me: “ $QUESTION
python3 queryprocess.py $QUESTION > ans1.txt
ANSWER=$(cut -c3- ans1.txt)
ANSWER1=$(echo “$ANSWER” | sed -e ‘s/\n/ /g’)
ANSWER2=${ANSWER1::-1}
echo “Robot: “ $ANSWER2
espeak “$ANSWER2” > /dev/null 2>&1

To run this program as script –> ./main.sh, then we can get a reply result as below screen dump, and with voice reply, too.

speech-main

Reference Document:

How to Capture Weather Forecast from web using Raspberry Pi’s Python Program

Introduction

We can use python to develop a program to capture any city’s weather forecast from openweathermap.org web site under our Raspberry Pi computer. I list our program and screen capture result in the following for your reference.

Program Source Code under Python ver 3 as below:

import urllib.request,json

city = input(“Enter City: “)

def getForecast(city) :
    #url = “http://api.openweathermap.org/data/2.5/forecast/daily?cnt=7&units=meteric&mode=json&q=”
    #url = “http://api.openweathermap.org/data/2.5/forecast/city?id=524901&APPID=42443ecbdcad3d01842205e3745895cd”
    #url = “http://api.openweathermap.org/data/2.5/forecast/daily?cnt=7&units=meteric&mode=json&q=LONDON&lang=zh_cn&APPID=42443ecbdcad3d01842205e3745895cd”
    url = “http://api.openweathermap.org/data/2.5/forecast/daily?cnt=7&units=meteric&mode=json&q=”
    url = url + city + “&lang=zh_cn&APPID=42443ecbdcad3d01842205e3745895cd”
    req = urllib.request.Request(url)
    response=urllib.request.urlopen(req)
    return json.loads(response.read().decode(“UTF-8”))

forecast = getForecast(city)

print(“Forecast for “, city, forecast[‘city’][‘country’])

day_num=1
for day in forecast[‘list’]:
    print(“Day : “, day_num)
    print(day[‘weather’][0][‘description’])
    print(“Cloud Cover : “, day[‘clouds’])
    print(“Temp Min : “, round(day[‘temp’][‘min’]-273.15, 1), “degrees C”)
    print(“Temp Max : “, round(day[‘temp’][‘max’]-273.15, 1), “degrees C”)
    print(“Humidity : “, day[‘humidity’], “%”)
    print(“Wind Speed : “, day[‘speed’], “m/s”)
    print()
    day_num = day_num+1

Screen Dump Result as below:

weather-pi

Further Development:

This function can be further developed to save weather forecast data to database, and then display in a chart format on screen. I may do it if I have time in future.

Network Communication Script for Raspberry Pi

Introduction

We can use python programs to start a server side and client side network communication service and allow them to transmit message  to each other under a Raspberry Pi. For example, we start two LXTerminal Sessions under a Raspberry Pi (or two difference Pi), and then run the following script.

In Server Side, run the following script:

import socket
comms_socket = socket.socket()
comms_socket.bind((‘localhost’, 50000))
comms_socket.listen(10)
connection, address = comms_socket.accept()
while True:
    print(connection.recv(4096).decode(“UTF-8”))
    send_data = input(“Reply: “)
    connection.send(bytes(send_data, “UTF-8”))

In Client Side, run the following script:

import socket
comms_socket = socket.socket()
comms_socket.connect((‘localhost’, 50000))
while True:
    send_data = input(“message: “)
    comms_socket.send(bytes(send_data, “UTF-8”))
    print(comms_socket.recv(4096).decode(“UTF-8”))

The testing result is shown as below screen dump for your reference:

network-communicate-pi

Most Easy Way to Start a Web Server Service Using Python

Introduction

Although it is easy to start-up IIS under Window O/S or Apache under Unix O/S, we still have another choice to start a web server service using Python programming tools. You only need to install python, and then start python and run the following script line by line:

import http.server, os
#define the server document directory with unix path;
os.chdir(“UsersadministratorDocuments”)
#if it is window env with directory as c:UsersadministratorDocuments
os.chdir(“/Users/goldmanau/Documents”)
httpd = http.server.HTTPServer((‘127.0.0.1’, 8000),
http.server.SimpleHTTPRequestHandler)
httpd.serve_forever()

This script can work under either python 2.7 or 3.x version under window or Unix environment. If you put the index.html to the server document path, then, you can start a web browser and input the link as http://127.0.0.1:8000/index.html in order to display your web page. It is the most easy, simple, fast way to start a web server service.

Installing pandas with Anaconda

Installing pandas and the rest of the NumPy and SciPy stack can be a little difficult for inexperienced users.

The simplest way to install not only pandas, but Python and the most popular packages that make up the SciPystack (IPython, NumPy, Matplotlib, …) is with Anaconda, a cross-platform (Linux, Mac OS X, Windows) Python distribution for data analytics and scientific computing.

After running a simple installer, the user will have access to pandas and the rest of the SciPy stack without needing to install anything else, and without needing to wait for any software to be compiled.

Installation instructions for Anaconda can be found here.

A full list of the packages available as part of the Anaconda distribution can be found here.

An additional advantage of installing with Anaconda is that you don’t require admin rights to install it, it will install in the user’s home directory, and this also makes it trivial to delete Anaconda at a later date (just delete that folder).

Windows Install

Download the Anaconda installer and double click it.

NOTE: If you encounter any issues during installation, please disable your anti-virus software.

TIP: The installer may also run in silent mode, without bringing up the graphical interface. To install Anaconda in this mode, type the following command into a command prompt, replacing the file name with the name of your downloaded install file:

Anaconda-2.4.0-Windows-x86_64.exe /S /D=C:Anaconda

The /D option specifies the install location. Quotes are not allowed here, even if there are spaces in the install location. For example, instead of /D="C:Program FilesAnaconda", use /D=C:ProgramFilesAnaconda.

Windows Uninstall

Click on “Add or remove Program” in the Control Panel, and select “Python 2.7 (Anaconda)”.

Updating from older Anaconda versions

You can easily update to the latest Anaconda version by updating conda, then Anaconda as follows:

conda update conda
conda update anaconda

Install Python’s Django on Windows

This document will guide you through installing Python and Django for basic usage on Windows. This is meant as a beginner’s guide for users working on Django projects and does not reflect how Django should be installed when developing patches for Django itself.
The steps in this guide have been tested with Windows 7 and 8. In other versions, the steps would be similar.

Install Python

Django is a Python web framework, thus requiring Python to be installed on your machine.

To install Python on your machine go to https://python.org/download/, and download a Windows MSI installer for Python. Once downloaded, run the MSI installer and follow the on-screen instructions.

After installation, open the command prompt and check the Python version by executing python --version. If you encounter a problem, make sure you have set the PATH variable correctly. You might need to adjust your PATHenvironment variable to include paths to the Python executable and additional scripts. For example, if your Python is installed in C:Python34, the following paths need to be added to PATH:

C:Python34;C:Python34Scripts;

Install Setuptools

To install Python packages on your computer, Setuptools is needed. Download the latest version of Setuptools for your Python version and follow the installation instructions given there.

Install PIP

PIP is a package manager for Python that uses the Python Package Index to install Python packages. PIP will later be used to install Django from PyPI. Python 3.4 and later include pip by default [1], so you may have pip already.

Install Django

Django can be installed easily using pip.

In the command prompt, execute the following command: pip install django. This will download and install Django.

After the installation has completed, you can verify your Django installation by executing django-admin --version in the command prompt.

Changed in Django 1.7:In Django 1.7, a .exe has been introduced, so just use django-admin in place of django-admin.py in the command prompt.

See Get your database running for information on database installation with Django.

Common pitfalls

  • If django-admin only displays the help text no matter what arguments it is given, there is probably a problem with the file association in Windows. Check if there is more than one environment variable set for running Python scripts inPATH. This usually occurs when there is more than one Python version installed.

  • If you are connecting to the internet behind a proxy, there might be problem in running the commands easy_installpip and pip install django. Set the environment variables for proxy configuration in the command prompt as follows:

    set http_proxy=http://username:password@proxyserver:proxyport
    set https_proxy=https://username:password@proxyserver:proxyport

    Git Installation

    • download from http://git-scm.com/download/win

    • Run UNIX command under Window Environment.