Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (2)

Powered by Vanilla. Made with Bootstrap.
Python OOP example 2
  • chroniccommand
    Posts: 1,389
    So after more tinkering around with OOP in python I created a simple script :P
    Check it out. Hopefully it helps you.


    #!/usr/bin/env python
    '''
    Some simple OOP examples in Python
    Examples:
    1) Socket's
    2) Command execution
    3) MD5 encrypter
    '''

    from socket import *
    import sys, hashlib
    import os
    class ConnectSocket(object): #Socket class
    def __init__(self, host, port, msg): #Initializer method
    self.host = host
    self.port = port
    self.msg = msg
    def ConnectIt(self): # Create a new method
    self.s = socket(AF_INET, SOCK_STREAM)
    self.s.connect((self.host, self.port))
    self.s.send(self.msg)
    print \"Message sent\"
    self.s.close()
    class ExecuteCmd(object):
    def __init__(self, cmd):
    self.cmd = cmd
    def ExecuteIt(self):
    self.executed = os.system(self.cmd)
    print self.executed
    class EncryptMd5(object):
    def __init__(self, text):
    self.plaintext = text
    def EncodeText(self):
    self.hashed = hashlib.md5(self.plaintext).hexdigest()
    print(str(self.hashed))
    def sockets():
    syshost = raw_input(\"Host: \")
    sysport = int(raw_input(\"Port: \"))
    sysmsg = raw_input(\"Message: \")
    socketinit = ConnectSocket(syshost, sysport, sysmsg)
    socketinit.ConnectIt()
    main()
    def execution():
    cmd = raw_input(\"Command: \")
    executeinit = ExecuteCmd(cmd)
    executeinit.ExecuteIt()
    main()
    def encryptit():
    message = raw_input(\"Message to encrypt: \")
    md5init = EncryptMd5(message)
    md5init.EncodeText()
    main()
    def main():
    print(\"*\" * 30)
    print(\"Simple OOP in Python examples\")
    print(\"*\" * 30)
    doit = raw_input(\"1 - Sockets\n2 - Execution\n3 - Encrypt\n4 - Exit\nChoice: \")
    if doit == '1':
    sockets()
    elif doit == '2':
    execution()
    elif doit == '3':
    encryptit()
    elif doit == '4':
    print(\"\nGoodbye\")
    sys.exit()
    else:
    print(\"Command not recognized\")
    main()

    main()