analitics

Pages

Monday, May 22, 2017

Make one executable from a python script.

The official website of this tool tells us:
PyInstaller bundles a Python application and all its dependencies into a single package. The user can run the packaged app without installing a Python interpreter or any modules. PyInstaller supports Python 2.7 and Python 3.3+, and correctly bundles the major Python packages such as numpy, PyQt, Django, wxPython, and others.

PyInstaller is tested against Windows, Mac OS X, and Linux. However, it is not a cross-compiler: to make a Windows app you run PyInstaller in Windows; to make a Linux app you run it in Linux, etc. PyInstaller has been used successfully with AIX, Solaris, and FreeBSD, but is not tested against them.

The manual of this tool can see it here.
C:\Python27>cd Scripts

C:\Python27\Scripts>pip install pyinstaller
Collecting pyinstaller
  Downloading PyInstaller-3.2.1.tar.bz2 (2.4MB)
    100% |################################| 2.4MB 453kB/s
....
Collecting pypiwin32 (from pyinstaller)
  Downloading pypiwin32-219-cp27-none-win32.whl (6.7MB)
    100% |################################| 6.7MB 175kB/s
...
Successfully installed pyinstaller-3.2.1 pypiwin32-219
Also, this will install the PyWin32 python module.
Let's make one test python script and then to make it executable.
I used this python script to test it:
from tkinter import Tk, Label, Button

class MyFirstGUI:
    def __init__(self, master):
        self.master = master
        master.title("A simple GUI")

        self.label = Label(master, text="This is our first GUI!")
        self.label.pack()

        self.greet_button = Button(master, text="Greet", command=self.greet)
        self.greet_button.pack()

        self.close_button = Button(master, text="Close", command=master.quit)
        self.close_button.pack()

    def greet(self):
        print("Greetings!")

root = Tk()
my_gui = MyFirstGUI(root)
root.mainloop()
The output of the command of pyinstaller:
C:\Python27\Scripts>pyinstaller.exe   --onefile --windowed ..\tk_app.py
92 INFO: PyInstaller: 3.2.1
92 INFO: Python: 2.7.13
93 INFO: Platform: Windows-10-10.0.14393
93 INFO: wrote C:\Python27\Scripts\tk_app.spec
95 INFO: UPX is not available.
96 INFO: Extending PYTHONPATH with paths
['C:\\Python27', 'C:\\Python27\\Scripts']
96 INFO: checking Analysis
135 INFO: checking PYZ
151 INFO: checking PKG
151 INFO: Building because toc changed
151 INFO: Building PKG (CArchive) out00-PKG.pkg
213 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30729, 9247)
2120 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
2251 INFO: Bootloader c:\python27\lib\site-packages\PyInstaller\bootloader\Windows-32bit\runw.exe
2251 INFO: checking EXE
2251 INFO: Rebuilding out00-EXE.toc because tk_app.exe missing
2251 INFO: Building EXE from out00-EXE.toc
2267 INFO: Appending archive to EXE C:\Python27\Scripts\dist\tk_app.exe
2267 INFO: Building EXE from out00-EXE.toc completed successfully.
Then I run the executable output:
C:\Python27\Scripts>C:\Python27\Scripts\dist\tk_app.exe

C:\Python27\Scripts>
...and working well.

The output file come with this icon:

Also, you can make changes by using your icons or set the type of this file, according to VS_FIXEDFILEINFO structure.
You need to have the icon file and/or version.txt file for VS_FIXEDFILEINFO structure.
Let's see the version.txt file:
# UTF-8
#
# For more details about fixed file info 'ffi' see:
# http://msdn.microsoft.com/en-us/library/ms646997.aspx
VSVersionInfo(
  ffi=FixedFileInfo(
    # filevers and prodvers should be always a tuple with four items: (1, 2, 3, 4)
    # Set not needed items to zero 0.
    filevers=(2017, 1, 1, 1),
    prodvers=(1, 1, 1, 1),
    # Contains a bitmask that specifies the valid bits 'flags'
    mask=0x3f,
    # Contains a bitmask that specifies the Boolean attributes of the file.
    flags=0x0,
    # The operating system for which this file was designed.
    # 0x4 - NT and there is no need to change it.
    OS=0x4,
    # The general type of file.
    # 0x1 - the file is an application.
    fileType=0x1,
    # The function of the file.
    # 0x0 - the function is not defined for this fileType
    subtype=0x0,
    # Creation date and time stamp.
    date=(0, 0)
    ),
  kids=[
    StringFileInfo(
      [
      StringTable(
        u'040904b0',
        [StringStruct(u'CompanyName', u'python-catalin'),
        StringStruct(u'ProductName', u'test'),
        StringStruct(u'ProductVersion', u'1, 1, 1, 1'),
        StringStruct(u'InternalName', u'tk_app'),
        StringStruct(u'OriginalFilename', u'tk_app.exe'),
        StringStruct(u'FileVersion', u'2017, 1, 1, 1'),
        StringStruct(u'FileDescription', u'test tk'),
        StringStruct(u'LegalCopyright', u'Copyright 2017 free-tutorials.org.'),
        StringStruct(u'LegalTrademarks', u'tk_app is a registered trademark of catafest.'),])
      ]),
    VarFileInfo([VarStruct(u'Translation', [0x409, 1200])])
  ]
)
Now you can use this command for tk_app.py and version.txt files from the C:\Python27 folder:
 pyinstaller.exe --onefile --windowed --version-file=..\version.txt ..\tk_app.py
Let's see this info into the executable file:

If you want to change the icon then you need to add the --icon=tk_app.ico, where tk_app.ico is the new icon of the executable.



Updating all Python with pip on Windows OS.

Just use this python module named pip-review.
C:\Python27\Scripts>pip install pip-review
C:\Python27\Scripts>pip-review.exe --auto --verbose
Checking for updates of ...
...
pip-review.exe --auto --verbose
Everything up-to-date

The pycrypto python module - part 001.

This python module name pycrypto is a collection of Python Cryptography Toolkit.
This python module has been created by Andrew Kuchling and now maintained by Dwayne C. Litzenberger.
Let's install under Windows 10 OS using Command Prompt (Admin) shell.
C:\WINDOWS\system32>cd ..

C:\Windows>cd ..

C:\>cd Python27\Scripts

C:\Python27\Scripts>pip install pycrypto
Requirement already satisfied: pycrypto in c:\python27\lib\site-packages
Some info and help under python shell can be seen using this:
C:\Python27>python.exe
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import Crypto
>>> dir(Crypto)
['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 
'__revision__', '__version__', 'version_info']
>>> help(Crypto)
Help on package Crypto:

NAME
    Crypto - Python Cryptography Toolkit

FILE
    c:\python27\lib\site-packages\crypto\__init__.py

DESCRIPTION
    A collection of cryptographic modules implementing various algorithms
    and protocols.

    Subpackages:

    Crypto.Cipher
     Secret-key (AES, DES, ARC4) and public-key encryption (RSA PKCS#1) algorithms    Crypto.Hash
     Hashing algorithms (MD5, SHA, HMAC)
    Crypto.Protocol
     Cryptographic protocols (Chaffing, all-or-nothing transform, key derivation
     functions). This package does not contain any network protocols.
    Crypto.PublicKey
     Public-key encryption and signature algorithms (RSA, DSA)
    Crypto.Signature
     Public-key signature algorithms (RSA PKCS#1)
    Crypto.Util
     Various useful modules and functions (long-to-string conversion, random number
     generation, number theoretic functions)

PACKAGE CONTENTS
    Cipher (package)
    Hash (package)
    Protocol (package)
    PublicKey (package)
    Random (package)
    SelfTest (package)
    Signature (package)
    Util (package)
    pct_warnings

DATA
    __all__ = ['Cipher', 'Hash', 'Protocol', 'PublicKey', 'Util', 'Signatu...
    __revision__ = '$Id$'
    __version__ = '2.6.1'

VERSION
    2.6.1
Let's test some examples with this python module.
The first example comes with encrypting and decrypt message based one key.
The key also needs to be one encryption key and fix to key32.
The iv will not be specified by the user, it will be generated and then encrypted with RSA.
NEVER make the IV constant and unique, it must be unique for every message.
Let's see the example source code:
from Crypto.Cipher import AES
from Crypto import Random
def encrypt(key32,message):
    cipher=AES.new(key32,AES.MODE_CFB,iv)
    msg=cipher.encrypt(message)
    print(msg)
    return msg
def decrypt(key32,msg):
    dec=AES.new(key32,AES.MODE_CFB,iv)
    return dec.decrypt(msg).decode('ascii')
if __name__=='__main__':
    global iv
    iv=Random.new().read(AES.block_size)
    key='free-tutorials.org'
    key32 = "".join([ ' ' if i >= len(key) else key[i] for i in range(32) ])
    message='another website with free tutorials'
    enc =encrypt(key32, message)
    print enc
    print(decrypt(key32,enc))
The resulting output is this:
ᄚ Cᆪ゚2 ᄊÕ|ýXÍ ᄇNäÇ3ヨ゙Lマᆱuï: ù メNᄚm
ᄚ Cᆪ゚2 ᄊÕ|ýXÍ ᄇNäÇ3ヨ゙Lマᆱuï: ù メNᄚm
another website with free tutorials

Another more simplistic example:
from Crypto.Cipher import AES
from Crypto import Random
key = b'Sixteen byte key'
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CFB, iv)
msg = iv + cipher.encrypt(b'Attack at dawn')
See the output of variables:
>>> print key
Sixteen byte key
>>> print iv
ÔÄ▀DÒ ÕØ} m║dÕ╚\
>>> print cipher.encrypt(b'Attack at dawn')
åÌ£┴\u\ÍÈSÕ╦╔.
Using MD5 example:
>>> from Crypto.Hash import MD5
>>> MD5.new('free text').hexdigest()
'be9420c1596a781119c53a9933a8234f'
Using RSA key example:
>>> from Crypto.PublicKey import RSA
>>> from Crypto import Random
>>> rng = Random.new().read
>>> RSAkey = RSA.generate(1024, rng)
>>> public_key = RSAkey.publickey()
>>> print public_key
<_rsaobj e="" n="" x3650b98="">
>>> enc_data = public_key.encrypt('test data', 32)[0]
>>> print enc_data
H +îÕÊ ÙH:?ª2S½Fã0á! f¬ = ·+,Í0r³┐o·¼ÉlWy¿6ôên(£jê¿ ╦çª|*°q Ò4ì┌çÏD¦¿╝û╠╠MY¶ïzµ>©a}hRô ]í;
_[v¸¤u:2¦y¾/ ²4R╩HvéÌ'÷Ç)KT:P _<! D
>>> dec_data = RSAkey.decrypt(enc_data)
>>> print dec_data
test data 
Encrypted and decrypted output texts may look different depending on how encoded the used text editor or python language.