10 python tricks that will be useful for your scripts

Hello! It’s been a while since we’ve written about Python, so today we bring you these 10 Python tricks that will be useful for your scripts.

10 trucos de python que te serán utiles para tus scripts

Remember that if you like our articles you can comment on them and we thank you for sharing them on social networks to help us reach more people.

Let’s get started!

1. Read and write GZip files

Sometimes we need to create GZip files from a larger file or we may need to transfer data. This is the easiest way to create GZip files. It is like creating a text file but importing the gzip module:

f = gzip.open(indexpath, 'rb')
import gzip
contenido = "Texto"
f = gzip.open(indexpath, 'rb')
data = f.read()
f.close()

To read it is exactly as easy, as well as writing it is the same way of working as when creating files with python:

import gzip
f = gzip.open(indexpath, 'rb')
data = f.read()
f.close()

2. Create tar.gz file

As in the previous example we will create a GZip file but from a directory instead of from a file. In this example we will create a tar.gz file in the home directory of the current user including in the file the .ssh directory which is also in the home directory of the current user:

from shutil import make_archive
import os
archive_name = os.path.expanduser(os.path.join('~', 'miarchivo'))
root_dir = os.path.expanduser(os.path.join('~', '.ssh'))
make_archive(archive_name, 'gztar', root_dir)

3. Hide the password when prompted by console

In this example we show how to hide the password when asking for it by console in one of our scripts. This is useful to prevent it from being displayed. It will behave exactly as the passwd or su or sudo commands do when asking for the password.

The code is as follows:

import getpass
usuario = raw_input("Introduce usuario: ")
password = getpass.getpass("Introduce password: ")
print(usuario, password)

The following would be displayed:

ocultar_password

4. Executing commands with pexpect

This module is the Python version of expect. Basically it is able to Execute a command and according to the output of the command enter a text in the console. This is valid when using commands that require a timeout for entering parameters such as passwd or ssh. This way you can complete the execution of the command correctly by entering the necessary parameters, in the example we will show how to execute SSH by entering the password of the target host with pexpect:

import pexpect
import time
cmd = "ssh root@192.168.1.165"
child = pexpect.spawn(cmd)
child.expect('password:', timeout=10)
child.sendline(PASSWORD)
time.sleep(5)
child.sendline("ping -c1 192.168.1.1\n")
time.sleep(5)
child.sendline("\n")
fout = file('/tmp/texto-192.168.1.165','w')
child.logfile = fout
time.sleep(5)
child.expect(pexpect.EOF)

What we would see in the execution would be as follows:

pexpect1

pexpect2

And the log file would be:

pexpect log

5. Encode and decode a base64 binary by chunks

These codes allow to encode and decode files in base64, it is useful to transfer large files or to transfer binary files or to save virus signatures, etc:

The way to encode it would be as follows, in which we are encoding a binary of the ping command stored in /tmp:

import codecs
import binascii

byte = "1"
in_file = open("/tmp/ping","rb")
out_file = open("/tmp/ping.b64","wb")
while byte != "":
    byte = in_file.read(128)
    encode = codecs.encode(binascii.b2a_base64(byte),"base64")
    out_file.write("__INICIO__" + encode[0:(len(encode)-2)] )
in_file.close()
out_file.close()

As you can see in the above code, it opens the source file and the destination file. It reads a block from the source file, encodes each block and saves it in the destination file.

In the next code block, it does just the opposite. That is, it opens the encoded file, decodes it and writes it to the destination file. All this block by block.

import codecs
import binascii

byte = "1"
in_file = open("/tmp/ping.b64","rb")
out_file = open("/tmp/ping.devuelta","wb")
count = 0 
while byte != "":
    byte = in_file.read(244)
    print byte
    if len(byte) != 244:
        byte = byte[0:len(byte)-1]
    if byte[0:10] == "__INICIO__": 
        byte = byte.replace("__INICIO__","")
        print "Entra1"
        out_file.write(binascii.a2b_base64(codecs.decode(byte+"=","base64")))
    else:
        bytes = byte.split("__INICIO__")
        for b in bytes:
            print b
            out_file.write(binascii.a2b_base64(codecs.decode(b+"==","base64")))
in_file.close()
out_file.close()

6. Comprobar web

This code block helps to see if a website has problems. In this case it is tested for only 10 minutes but by tweaking the code it can be tested infinitely:

import requests,time,sys

def getWeb(url):
    r = requests.get(url)
    return int(r.status_code)

def main():
    url = "http://google.es" #sys.argv[1]
    counter = 0
    status = getWeb(url)
    print ("The website returns status code: {0}".format(status))
    while counter<10 and status==500:
        status = getWeb(url)
        print ("The website returns status code: {0}".format(status))
        time.sleep(60)
        counter += 1
    if status==500:
        print ("The website is down")
    elif status==200:
        print ("The website is OK")
    else:
        print ("The website is not returning error code 500 but is not OK")

if __name__=="__main__":
    main()

 

7. Getting a voice from a text

Unfortunately I don’t have an alternative for linux yet, I only have this code available for windows.

This code plays the text that is passed as a parameter as a sound:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import win32com.client

speaker = win32com.client.Dispatch("SAPI.SpVoice")

while True:
    print("Introduzca el texto a reproducir:")
    s = raw_input()
    speaker.Speak(s)

 

8. Reimport module

Sometimes a module has to be reloaded at runtime, this is achieved with:

reload(modulo)

With this line we can make that if we have created a service, it reloads modules at runtime, to reload possible changes in modules or plugins without restarting the service.

9. Remove non-alphanumeric characters

This code replaces the non-alphanumeric characters by the character we want, this can be used for example as a filter for passwords:

import re
s = re.sub('[^0-9a-zA-Z]+', '*', s)

10. Remove empty items from a list

Sometimes when we replace elements of a list or we make a split of a text it can happen that we generate empty elements of the list in the form: [”]. This can be solved by eliminating these elements with the following code:

texto="Prueba con     espacios  multiples"
str_list=texto.split(" ")
#este es el mas rapido
str_list = filter(None, str_list) # fastest
#O podemos utilizar este:
str_list = filter(bool, str_list) # fastest
#Este es otra opcion:
str_list = filter(len, str_list) # a bit slower
#O este otro:
str_list = filter(lambda item: item, str_list) # slower than list 

Here we can see how it works:

filtro_vacio

That’s all for today. If you liked it or want more articles like this one leave your opinion in a comment and/or share in your social networks.
See you soon!

Leave a Reply