analitics

Pages

Thursday, April 20, 2017

The twilio python module and cloud communications platform .

Let's build apps that communicate with everyone in the world. Voice & Video, Messaging, and Authentication APIs for every application.
First, let's try to install it under Windows 10 operating system:
C:\>cd Python27
C:\Python27>cd Scripts
C:\Python27\Scripts>pip install twilio
Collecting twilio
  Downloading twilio-5.6.0.tar.gz (194kB)
    100% |################################| 194kB 588kB/s
Collecting httplib2>=0.7 (from twilio)
  Downloading httplib2-0.9.2.zip (210kB)
    100% |################################| 215kB 519kB/s
Requirement already satisfied: six in c:\python27\lib\site-packages (from twilio)
Requirement already satisfied: pytz in c:\python27\lib\site-packages (from twilio)
Installing collected packages: httplib2, twilio
  Running setup.py install for httplib2 ... done
  Running setup.py install for twilio ... done
Successfully installed httplib2-0.9.2 twilio-5.6.0
Try some example:
C:\Python27>python.exe
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import twilio
>>> from twilio import *
>>> dir(twilio)
['TwilioException', 'TwilioRestException', 'TwimlException', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__version__', '__version_info__', 'compat', 'exceptions', 'rest', 'sys', 'u', 'version']
>>> dir(twilio.rest)
['TwilioIpMessagingClient', 'TwilioLookupsClient', 'TwilioPricingClient', 'TwilioRestClient', 'TwilioTaskRouterClient', 'TwilioTrunkingClient', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '_hush_pyflakes', 'base', 'client', 'exceptions', 'ip_messaging', 'lookups', 'pricing', 'resources', 'set_twilio_proxy', 'task_router', 'trunking']
Under Fedora 25 you can use this command to install this API:
[root@localhost mythcat]# pip2.7 install twilio
Collecting twilio
  Downloading twilio-5.7.0.tar.gz (168kB)
    100% |████████████████████████████████| 174kB 1.8MB/s 
Requirement already satisfied: httplib2>=0.7 in /usr/lib/python2.7/site-packages (from twilio)
Requirement already satisfied: six in /usr/lib/python2.7/site-packages (from twilio)
Requirement already satisfied: pytz in /usr/lib/python2.7/site-packages (from twilio)
Installing collected packages: twilio
  Running setup.py install for twilio ... done
Successfully installed twilio-5.7.0
 
Make an account for Twilio here.
Now about phone Twilio numbers, then programmable phone Twilio numbers are a core part of Twilio’s platform, enabling you to receive SMS, MMS, and phone calls.
You can have some problems with SMS sending by country availability.
And one last example:
# /usr/bin/env python
# Download the twilio-python library from http://twilio.com/docs/libraries
from twilio.rest import Client

# Find these values at https://twilio.com/user/account
account_sid = "AC61b32be301f49f78f0ab3d69c4d335f6"
auth_token = "c8f37b65755900faa4fe7bbe1f948adb"
client = Client(account_sid, auth_token)

message = client.api.account.messages.create(to="+contry_allow_SMS",
                                             from_="++contry_allow_SMS",
                                             body="Hello python this is a twilio sms test")

Friday, April 14, 2017

Blender 3D - ellipsoid.

This is a simple way to use Blender 3D - version 2.78c with python scripting tool to make one ellipsoid.

The ellipsoid may be parameterized in several ways but I used the sin and cos functions:
x = sin(theta) * sin(phi)
y = cos(theta) * sin(phi)
z = cos(phi)

The steps I follow are:
  • make points of ellipsoid - CoordsPoints
  • define an ellipsoid vectors 
  • create a new mesh 
  • make rings for faces
  • make an ellipsoid
  • The verts_mesh and verts_mesh_face are used to make faces
  • put all into the Blender 3D scene

import bpy
import bmesh
from math import degrees, radians, sin, cos, tan
from mathutils import Vector


class CoordsPoints:
    @property
    def xyz(self):
        theta = self.theta
        phi = self.phi
        x = sin(theta) * sin(phi)
        y = cos(theta) * sin(phi)
        z = cos(phi)
        R = self.R
        return R * Vector((x,y,z))

    def __init__(self, R, theta, phi):
        self.R = R
        self.theta = theta
        self.phi = phi
        #self.xyz = self.point(theta, phi)

    def __repr__(self):
        return "Coords(%.4f, %.4f)" % (degrees(self.theta),
                                               degrees(self.phi))
# define the ellipsoid method.
def ellipsoid(a, b, c):
    def ellipsoid(v):
        x = a * (v.x)
        y = b * (v.y)
        z = c * (v.z)
        return Vector((x, y, z))
    return ellipsoid

# make the ellipsoid bmesh
bm = bmesh.new()

# TODO come up with a nicer way to do this.
rings = [[CoordsPoints(1, radians(theta), radians(phi)) 
                 for theta in range (0, 360, 2)]
                 for phi in range(0, 180, 2)]

h = ellipsoid(1.0, 1.0, 1.5)

verts_mesh = [bm.verts.new(h(p.xyz)) for p in rings[0]]
verts_mesh.append(verts_mesh[0])
for ring in range(1, len(rings)):

    verts_mesh_face = [bm.verts.new(h(p.xyz)) for p in rings[ring]]
    verts_mesh_face.append(verts_mesh_face[0])

    faces = [
        bm.faces.new((
            verts_mesh[i], verts_mesh_face[i],
            verts_mesh_face[i+1], verts_mesh[i+1]
        ))
        for i in range(len(verts_mesh) - 1)
    ]
    verts_mesh = verts_mesh_face

# create mesh link it to scene 
mesh = bpy.data.meshes.new("ellipsoid")
bm.to_mesh(mesh)
obj = bpy.data.objects.new("ellipsoid", mesh)
scene = bpy.context.scene
scene.objects.link(obj)
scene.objects.active = obj
obj.select = True
obj.location = scene.cursor_location

Wednesday, April 5, 2017

The scapy python module - part 001.

Today I will start with scapy python module.
This is a good python module to deal and interact with network packets.
[root@localhost mythcat]# pip install scapy
Collecting scapy
  Downloading scapy-2.3.3.tgz (1.4MB)
    100% |████████████████████████████████| 1.4MB 904kB/s 
Building wheels for collected packages: scapy
  Running setup.py bdist_wheel for scapy ... done
  Stored in directory: /root/.cache/pip/wheels/bd/cf/...
Installing collected packages: scapy
Successfully installed scapy-2.3.3
The first test is to test is the echo of Layer 3 ICMP.
Use the superuser shell to run this python script:
from scapy.all import *
dstip=raw_input("enter the ip address \n")
icmp=ICMP()
icmp.type=8
icmp.code=0
ip=IP()
ip.dst=dstip
p=sr1(ip/icmp,timeout=5, verbose=0)
if(p):
        print "Layer 3 is up"
else:
        print "Layer 3 status is down"
The next python script will about arp request:
from scapy.all import *
def arp_display(pkt):
    if pkt[ARP].op == 1: 
        return "Request: " + pkt[ARP].psrc + " is asking about " + pkt[ARP].pdst
    if pkt[ARP].op == 2: 
        return "*Response: " + pkt[ARP].hwsrc + " has address " + pkt[ARP].psrc
print sniff(prn=arp_display, filter="arp", store=0, count=10)
This will read the packages from source and destination and show me what ARP traffic my computer is seeing.

How to parse the OPML file.

For example, the Feedly (stylized as Feedly) is a news aggregator application for various web browsers and mobile devices can let you export and import the OPML file.

What is XML?
The Extensible Markup Language (XML) is a markup language much like HTML or SGML. This is recommended by the World Wide Web Consortium and available as an open standard.

Today I will show you how to parse the OPML file type with python 2.7 version and XML python module.
This is the source script:
from xml.etree import ElementTree
import sys

file_opml = sys.argv[1]
def extract_rss_urls_from_opml(filename):
    urls = []
    with open(filename, 'rt') as f:
        tree = ElementTree.parse(f)
    for node in tree.findall('.//outline'):
        url = node.attrib.get('xmlUrl')
        if url:
            urls.append(url)
    return urls
urls = extract_rss_urls_from_opml(file_opml)
print urls
The result is a list with all your RSS links.

Friday, March 24, 2017

Take weather data with pyowm from openweathermap .

This tutorial shows you how to download and install the pyowm python module.
One of the great things about using this python module let you to provide data from openweathermap website (need to have one account).
PyOWM runs on Python 2.7 and Python 3.2+, and integrates with Django 1.10+ models.
All documentation can be found here.

The install is simple with pip , python 2.7 and Fedora 25.
 
[root@localhost mythcat]# pip install pyowm
Collecting pyowm
  Downloading pyowm-2.6.1.tar.gz (3.6MB)
    100% |████████████████████████████████| 3.7MB 388kB/s 
Building wheels for collected packages: pyowm
  Running setup.py bdist_wheel for pyowm ... done
  Stored in directory: /root/.cache/pip/wheels/9a/91/17/bb120c765f08df77645cf70a16aa372d5a297f4ae2be749e81
Successfully built pyowm
Installing collected packages: pyowm
Successfully installed pyowm-2.6.1
The source code is very simple just connect with API key and print data.
#/usr/bin/env python
#" -*- coding: utf-8 -*-
import pyowm

print " Have a account to openweathermap.org and use with api key free or pro"
print " owm = pyowm.OWM(API_key='your-API-key', subscription_type='pro')"

owm = pyowm.OWM("327407589df060c7f825b63ec1d9a096")  
forecast = owm.daily_forecast("Falticeni,ro")
tomorrow = pyowm.timeutils.tomorrow()
forecast.will_be_sunny_at(tomorrow)  

observation = owm.weather_at_place('Falticeni,ro')
w = observation.get_weather()
print (w)                     
print " Weather details"
print " =============== "
                                    
print " Get cloud coverage"
print w.get_clouds() 
print " ----------------"                                     
print " Get rain volume"
print w.get_rain() 
print " ----------------"
print " Get snow volume"
print w.get_snow()                                       

print " Get wind degree and speed"
print w.get_wind() 
print " ----------------"                                      
print " Get humidity percentage"
print w.get_humidity()    
print " ----------------"                               
print " Get atmospheric pressure"
print w.get_pressure()                                   
print " ----------------"
print " Get temperature in Kelvin degs"
print w.get_temperature() 
print " ----------------"                              
print " Get temperature in Celsius degs"
print w.get_temperature(unit='celsius')
print " ----------------"                 
print " Get temperature in Fahrenheit degs"
print w.get_temperature('fahrenheit')                    
print " ----------------"
print " Get weather short status"
print w.get_status()                                     
print " ----------------"
print " Get detailed weather status"
print w.get_detailed_status()                           
print " ----------------"
print " Get OWM weather condition code"
print w.get_weather_code()                               
print " ----------------"
print " Get weather-related icon name"
print w.get_weather_icon_name()                          
print " ----------------"
print " Sunrise time (ISO 8601)"
print w.get_sunrise_time('iso')    
print " Sunrise time (GMT UNIXtime)"
print w.get_sunrise_time()                               
print " ----------------"
print " Sunset time (ISO 8601)"
print w.get_sunset_time('iso')  
print " Sunset time (GMT UNIXtime)"
print w.get_sunset_time()                          
print " ----------------"
print " Search current weather observations in the surroundings of"
print " Latitude and longitude coordinates for Fălticeni, Romania:"
observation_list = owm.weather_around_coords(47.46, 26.30)

Let's see and the result of running the python script for one random location:
 
[root@localhost mythcat]# python openweather.py 
 Have a account to openweathermap.org and use with api key free or pro
 owm = pyowm.OWM(API_key='your-API-key', subscription_type='pro')

 Weather details
 =============== 
 Get cloud coverage
20
 ----------------
 Get rain volume
{}
 ----------------
 Get snow volume
{}
 Get wind degree and speed
{u'speed': 5.7, u'deg': 340}
 ----------------
 Get humidity percentage
82
 ----------------
 Get atmospheric pressure
{'press': 1021, 'sea_level': None}
 ----------------
 Get temperature in Kelvin degs
{'temp_max': 287.15, 'temp_kf': None, 'temp': 287.15, 'temp_min': 287.15}
 ----------------
 Get temperature in Celsius degs
{'temp_max': 14.0, 'temp_kf': None, 'temp': 14.0, 'temp_min': 14.0}
 ----------------
 Get temperature in Fahrenheit degs
{'temp_max': 57.2, 'temp_kf': None, 'temp': 57.2, 'temp_min': 57.2}
 ----------------
 Get weather short status
Clouds
 ----------------
 Get detailed weather status
few clouds
 ----------------
 Get OWM weather condition code
801
 ----------------
 Get weather-related icon name
02d
 ----------------
 Sunrise time (ISO 8601)
2017-03-24 04:08:33+00
 Sunrise time (GMT UNIXtime)
1490328513
 ----------------
 Sunset time (ISO 8601)
2017-03-24 16:33:59+00
 Sunset time (GMT UNIXtime)
1490373239
 ----------------
 Search current weather observations in the surroundings of
 Latitude and longitude coordinates for Fălticeni, Romania:

Thursday, March 16, 2017

The tensorflow python module - part 003.

This short tutorial come to fix your work and help you with examples and tutorials.
You can look to the internet and you can search many examples and tutorials but you can get errors.
How to fix that:
The first step is to know what tensorflow version of you use.
[mythcat@localhost ~]$ python -c "import tensorflow; print(tensorflow.__version__)"
1.0.1
Then yo need to know what parts from tensorflow old version is deprecated from the last version.
One superannuated method is to fix old source code with this:
import tensorflow as tf
tf.scalar_summary = tf.summary.scalar
tf.merge_all_summaries = tf.summary.merge_all
tf.train.SummaryWriter = tf.summary.FileWriter
A better method is to fix your source code and read the documentation.