PyMetasploit is a msfrpc library for Python and allowus us to automate the exploitation tasks with Python. It is meant to interact with the msfrpcd daemon that comes with the latest versions of Metasploit. Therefore, before you can begin to use this library, you'll need to initialize msfrpcd and optionally (highly recommended) PostgreSQL: https://github.com/allfro/pyMetasploit.
We can install the module from the source code with the setup.py script install:
$ git clone https://github.com/allfro/pyMetasploit.git
$ cd pyMetasploit
$ python setup.py install
Once we have installed it , we can import the module in our scripts and establish a connection with the MsfRpcClient class:
>>> from Metasploit.msfrpc import MsfRpcClient
>>> client = MsfRpcClient('password',user='msf')