Python on Life and Shell https://lifeandshell.com/tags/python/ Recent content in Python on Life and Shell Hugo -- gohugo.io en-us Mattias Hemmingssion mattias@lifeandshell.com Sat, 26 Nov 2016 22:05:28 +0000 Alexa and Jenkins (Docker) https://lifeandshell.com/posts/alexa-jenkins-docker/ Sat, 26 Nov 2016 22:05:28 +0000 https://lifeandshell.com/posts/alexa-jenkins-docker/ So I have an Alexa echo dot at home. Use it to control stuff but I wanted it to do more like release and deploy the stuff I build. This is how you can integrate Alexa voice service with Jenkins. First setup the server For receiving commands from Alexa and sending them to Jenkins we need a server and some code. First start with the server i use docker and a docker-compose to set it up. Autodeploy you docker images to AWS (git push = deploy) https://lifeandshell.com/posts/autodeploy-docker-images-aws-git-push-deploy/ Thu, 26 May 2016 12:56:19 +0000 https://lifeandshell.com/posts/autodeploy-docker-images-aws-git-push-deploy/ So I have a lot of small project and some large. To buil in quality into my code i need to run test in my code. And my code in a prod like env. I always uses docker so my dev env are verly like my prod. One key thing that i do is that when i push code to my master branch i do a release do server. This is so that i can verify that everything is working and i can run test on it. Python3 and rabbitmq https://lifeandshell.com/posts/python3-and-rabbitmq/ Tue, 21 Apr 2015 22:38:05 +0000 https://lifeandshell.com/posts/python3-and-rabbitmq/ Im using rabbitmq in some of my python apps. Here is a small guide to get pyton3 to send and recive data from rabbitmq   I uses the code from https://code.google.com/p/py-amqplib/ And read some guide from http://blogs.digitar.com/jjww/2009/01/rabbits-and-warrens/ from 2009 !!!!   Get the pip you need to connect sudo pip3 install amqp     My python code for sending and reciving   #!/usr/bin/env python from amqplib import client_0_8 as amqp import time conn = amqp. Setup SPI on Raspberry pi (mcp3008, Adafruit) https://lifeandshell.com/posts/setup-spi-on-raspberry-pi-mcp3008-adafruit/ Sun, 19 Apr 2015 20:25:33 +0000 https://lifeandshell.com/posts/setup-spi-on-raspberry-pi-mcp3008-adafruit/ Im building my own watering system and to that I will have some sensores.. They are connected to my pi over SFI and a mcp3008 from Adafruit.   The gear http://www.adafruit.com/products/1989 http://www.adafruit.com/products/856 http://www.kjell.com/sortiment/el/elektronik/elektroniklab/kopplingsplatta-lodfri-p87886 http://www.elecfreaks.com/store/octopus-soil-moisture-sensor-brick-p-422.html   Setup the cables Use this guide and se how the you should connect the mcp3008 and the sensore. http://www.raspberrypi-spy.co.uk/2013/10/analogue-sensors-on-the-raspberry-pi-using-an-mcp3008/   Get the Pi ready   1. First enable SFI on the board here http://www.raspberrypi-spy.co.uk/2014/08/enabling-the-spi-interface-on-the-raspberry-pi/ I uses the raspi-config and enabled the SFI OAuth2 Server on Python (with flask on Centos) https://lifeandshell.com/posts/oauth2-server-on-python-with-flask-on-centos/ Fri, 30 May 2014 20:04:05 +0000 https://lifeandshell.com/posts/oauth2-server-on-python-with-flask-on-centos/ So at work we have started to look at OAuth2 for our web apps. So on our creativ friday today i started looking at putting together an OAuth2 server using python and flask. I followed the guide from this page http://lepture.com/en/2013/create-oauth-server And after some work I got an working server and client running on my Centos server. The code only uses an sqlite db and are only testing the OAuth functions so for a working solutions there are some more work. Recover you python files from rm -rf * https://lifeandshell.com/posts/recover-you-python-files-from-rm-rf/ Thu, 03 Apr 2014 14:49:50 +0000 https://lifeandshell.com/posts/recover-you-python-files-from-rm-rf/ So after cleaning up my work i run rm -rf * in the woring folder. Deleting all my work!. After fighting holding back some tears I set down and start see if i could recover my lost work. First recover you file from the filesystem and my laptop is an ubuntu desktop sudo apt-get install extundelete Then its time to recover the files i run this command to get my lost folder back Build you first syco Module https://lifeandshell.com/posts/build-you-first-syco-module/ Tue, 18 Feb 2014 22:12:56 +0000 https://lifeandshell.com/posts/build-you-first-syco-module/ SO from the last post you can install syco but you also need to build and update your own plugins in syco. Here is a small guide how to build you first plugin. Here om building some syco commands for controlling apache and glassfish server. the commands are run from our syco-chuck release commands center so for adding them to syco i can controll the script from sudo and do some extra test before starting and stopping the service. Setup SYCO on you centos box https://lifeandshell.com/posts/setup-syco-on-you-centos-box/ Tue, 18 Feb 2014 15:27:04 +0000 https://lifeandshell.com/posts/setup-syco-on-you-centos-box/ So if you care about security and stability you must have syco installed on your server. Read more about syco on the github project https://github.com/systemconsole Im staring to use syco not only production but also on my “Own” server. So more of you should really start using it and here is i guide for you to start using syco 1. Installing and setting up centos yum install git   Gettings syco Installing Asylguiden on centos Server https://lifeandshell.com/posts/installing-asylguiden-on-centos-server/ Mon, 17 Feb 2014 21:21:20 +0000 https://lifeandshell.com/posts/installing-asylguiden-on-centos-server/ One of my own prodjects are Asylguiden. Its A python publish system build with django, Mysql and mongodb. You can find the code here on github https://github.com/mattiashem/asylguiden Asylguiden also works with wsgi for python and apache for displaying content here is my own how to for downloadning and setting up asylguiden on a production server. 1. Setting up server for hosing Centos yum install httpd mod_ssl git wget python-setuptools mod_wsgi   ejabber users from postfixadmin (python,mysql,md5crypt) https://lifeandshell.com/posts/ejabber-users-from-postfixadmin-pythonmysqlmd5crypt/ Fri, 10 Jan 2014 21:28:51 +0000 https://lifeandshell.com/posts/ejabber-users-from-postfixadmin-pythonmysqlmd5crypt/ So Im running my emails with postfix and have postfix admin to manager my users and domains. But now it should be nice to have i jabber server running and to have the same user and password for both email and jabber. Ejabber support custom auth plugins and with some python i now have a working plugin.   First install python packages yum install MySQL-python yum install python-passlib   Add this script to you ejabber folder Install and setup Haystack search for Django https://lifeandshell.com/posts/install-and-setup-haystack-search-for-django/ Sun, 05 Jan 2014 22:28:01 +0000 https://lifeandshell.com/posts/install-and-setup-haystack-search-for-django/ So Mysql is crap at doing full text search. So in one of my projects i use Haystack so i can do full text searches. I have a running Django project up and this is how I setup haystack for my project.   Install and config sudo pip install django-haystack   in settings.py under INSTALLED_APPS add haystack 'haystack',   And also in settings.py file add some haystack settings import os HAYSTACK_CONNECTIONS = { 'default': { 'ENGINE': 'haystack. Python ConfigParser using you own config files in python https://lifeandshell.com/posts/python-configparser-using-you-own-config-files-in-python/ Wed, 07 Aug 2013 11:26:42 +0000 https://lifeandshell.com/posts/python-configparser-using-you-own-config-files-in-python/ Storing settings in config files and then let python read the configfiles and to good stuff .   Read the file #Reading config file config = ConfigParser.ConfigParser() config.read('setting.cfg') print all items and values in an section for name, value in config.items("monitor"): print ' %s = %s' % (name, value) Print all items in configfile for section_name in parser.sections(): print 'Section:', section_name print ' Options:', parser.options(section_name) for name, value in parser.items(section_name): print ' %s = %s' % (name, value) My settings. How the HELL is oncall ? (the oncall reminder script) https://lifeandshell.com/posts/how-the-hell-is-oncall-the-oncall-reminder-script/ Mon, 05 Aug 2013 21:59:17 +0000 https://lifeandshell.com/posts/how-the-hell-is-oncall-the-oncall-reminder-script/ When you have oncall often sometimes is easy to forget hows oncall and when you are not. So for the last time wonder how is oncall and ask some python for some help,   The script #!/usr/bin/env python # # Mattias Hemmingsson # matte@elino.se # # Script for reminder friend when to bet # Uses and csv file and send email to remind when its time to bet. # # import csv import smtplib from datetime import datetime, timedelta, date #Get users and send email to users sender = 'noreply@elino. restrict sms in nagios / icinga https://lifeandshell.com/posts/limit-sms-flood-in-nagios-icing/ Mon, 05 Aug 2013 16:13:58 +0000 https://lifeandshell.com/posts/limit-sms-flood-in-nagios-icing/ Im using nagios as primary monitoring tool. And to get alerts we use an sms gateway. The problem is that sometimes when we work we bring down and server and we get so many sms from icinga that you trow away you phone. So for bringing the sms cost down and to have not so many sms to you phone i build a small email blocking script. This will take the address of the sms and only send one sms / email every 5 min (can be set to anything). Django sending email https://lifeandshell.com/posts/django-sending-email/ Tue, 04 Jun 2013 21:35:24 +0000 https://lifeandshell.com/posts/django-sending-email/ building and small webpage and in that page I want an small contact field. So my visitors (if any ) can contact me with an form input. So I made an small html template that has a very small form (No validation ) and then post the email and massages back to the view that send the email. small and simple and today work in front of the tv. django template my template extends my index as you see called contact. Testing OSSEC / Syslog auth https://lifeandshell.com/posts/testing-ossec-syslog-auth/ Mon, 03 Jun 2013 20:38:35 +0000 https://lifeandshell.com/posts/testing-ossec-syslog-auth/ Im runing and PCI DSS Level 1 system. And during our PCI Audit i have to provide evidence that our monitoring system (OSSEC) can log logins that fails. So or testing this and to provide evidence for our audit I made a small python script. the Scripts tries to login to th host specified in and text field and tries to run an command on them. (You can alter this to the correct username / password and then run commands on all server) DNS Verify new ns servers https://lifeandshell.com/posts/dns-new-ns-servers/ Sun, 02 Jun 2013 17:06:10 +0000 https://lifeandshell.com/posts/dns-new-ns-servers/ The dns tester scripot lets you check so that you dns name are correct checking first used names today and then verify the names with you new DNS server. You will need an file of you doman names first to run in the script.   #!/usr/bin/env python import socket import dns.resolver #v=”yes” g_dns=”88.80.170.189″ o_dns=”81.201.209.55″ def test_dns(name,typ,v): print “===================================================================” try: answers = dns.resolver.query(name,typ ) for rdata in answers: if v ==”yes”: print “Your DNS = ” + str(rdata) except dns.