Pages

Thursday, March 23, 2017

How to install and build python application in linux?


How to install and build python application in linux?


This topic installing and creating simple server based on python


1. Install python



    yum install python



2. Install pip for python .
Epel repo required

    wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
rpm -ivh epel-release-latest-6.noarch.rpm

    yum install python-pip




3. install rpyc for server . ( to check pip)






    pip install rpyc


with proxy
    pip install rpyc –proxy=http://<IP:PORT>




4 . pyinstaller to create executables


This is for python 2.6.2 . This is default for RHEL 6 . For python 2.7 pip works for pyinstaller


    wget https://pypi.python.org/packages/c3/a3/d50648bc99a8d5d6c533fc5c1101cc18014d0782e7ddbe9744ad2d56ad49/PyInstaller-2.1.tar.gz


extract the file
    python setup.py install








5. Test the script


Create simple python file


#!/bin/python


print 'TEST'
as test.py



    ./pyinstaller.py --onefile /tmp/new.py




it will create , exe under dist folder




6. Creating simple server with logger in python


myserver.py
#!/bin/python
import rpyc,logging,logging.handlers
from rpyc.utils.server import ThreadedServer




LOG_FILENAME = '/tmp/monitorServer.log'
my_logger = logging.getLogger('MyLogger')
my_logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(message)s")
handler = logging.handlers.RotatingFileHandler(
LOG_FILENAME, maxBytes=1024, backupCount=5)
handler.setFormatter(formatter)


my_logger.addHandler(handler)








if __name__ == "__main__":
server = ThreadedServer(MyService, port = 18812)
my_logger.info(" Listening at 18812")
server.start()




This will create server running on 18812






7. Build server



    ./pyinstaller.py --onefile myserver.py




8. Adding as service to start when ever server starts


a. Create script under /etc/init.d


#!/bin/bash
# chkconfig: 345 99 10
# description: my Monitor server based on Python
#
case "$1" in
'start')
/opt/myserver 2>/tmp/mm_error.log &;;
'stop')
echo "put something to shutdown or kill the process here";;
esac
as myserver under /etc/init.d
Where /opt/myserver is the server exe
Other options are default




8. Adding chkconfig

    chkconfig --add myserver
  1. chkconfig --list myserver



After reboot check the port to verify server is running or not