FaHSocket For Python 3.2.2 on Android.

This is the place to post any code that you want to share with the community. Only completed scripts should be posted here.

FaHSocket For Python 3.2.2 on Android.

Postby codyf86 » Thu Jul 25, 2013 9:15 pm

Hi all,

I wanted to share the project I've been working on, FaHSocket. - https://github.com/codyf86/FaHSocket-Py3-Android

You can get python 3.2.2 for Android here. - https://code.google.com/p/python-for-android/

This is a class API for python 3.2.2, specifically for Android but works with windows, etc just fine. It is a socket class I wrote to communicate with the Folding@Home remote interface. i.e.,

Image
Image

You can actually use this class for anything socket related and you probably wouldn't even have to change it.

It will also log to log.txt if log_ON is true. Also, the recv() function has an is_PyON bool i.e., recv(self, size, is_PyON). If this is set to True then you can set that recv call equal to a variable and use getparameter() to pull data from the PyON information if you send the slot-info command or any command that returns PyON data.

Also I am posting my test script which includes the whole API; I use it to test the Class API when I make changes to it. This is my first Python project and I'm pretty much self taught, so feedback is welcome. :)


Code: Select all
import socket
import sys

log_ON = True
sh_NLINE = False

class fahsocket:

##################################################
    def __init__(self):
       
        try:
            self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        except IOError as msg:

            print ('Init Failed: ' + str(msg))
            sys.exit()

        else:
            self.sock.settimeout(16)


##################################################
    def close(self):

        fahsocket.send(self, 'quit\n')
        self.buffer = fahsocket.recv(self, 16, True)
        print ('Received fahsocket.close(); Cleaning Up Socket!')

        try:
            if self.buffer == fahsocket.recv(self, 16, True):

                print ('Buffer Empty!')

            else:
                print ('ERROR: Buffer not empty...Flushing Buffer!')
                fahsocket.recv(self, 8192, False)

        except IOError as msg:

            print ('Socket Not Responding: ' + str(msg) + '!')
            sys.exit()

        finally:
            print ('Sending sock.shutdown()!')
            self.sock.shutdown(2)

            print ('Sending sock.close()!')
            self.sock.close()


##################################################
    def connect(self, host, port):

        self.output = 'Connecting: ' + repr(host) + ':' + repr(port)
        print ('\n' + self.output + '\n')
        fahsocket.dolog(self, self.output)

        try:
            self.sock.connect((host, port))

        except IOError as msg:

            print ('Failed To Connect: ' + str(msg) + '!')
            sys.exit()

        else:
            fahsocket.recv(self, 7, False)
            fahsocket.recv(self, 54, False)


##################################################
    def send(self, command):

        self.output = 'Sending: ' + repr(command.splitlines(sh_NLINE)[0])
        print ('\n' + self.output + '\n')

        try:
            self.sock.send(command.encode('utf-8'))

        except IOError as msg:

            print ('Send Failed: ' + str(msg) + '!')
            sys.exit()

        else:
            fahsocket.dolog(self, self.output)


##################################################
    def recv(self, size, is_PyON):

        try:
            self.buffer = self.sock.recv(size).splitlines(sh_NLINE)

        except IOError as msg:

            print ('Receive Failed: ' + str(msg) + '!')
            sys.exit()

        else:
            if is_PyON is True:

                self.pyon_data = []

            for i, v in enumerate(self.buffer):

                v = v.decode('utf-8')
                print (v)
                fahsocket.dolog(self, v)

                if is_PyON is True:

                    list.append(self.pyon_data, v)

            if is_PyON is True:

                return self.pyon_data


##################################################
    def dolog(self, output):

        if log_ON is True:

            self.log = open('log.txt', 'a')
            self.log.write(output + '\n')
            self.log.close()


##################################################
    def getparameter(self, pyon_data, parameter):

        for i, v in enumerate(pyon_data):

            if parameter in v:

                fahsocket.dolog(self, v)

                return v


##################################################

s = fahsocket()

s.connect('YourIPHERE', 36330)

#!
s.send('auth YourPASSHERE\n')
s.recv(8192, False)
#!

#!
s.send('slot-info\n')
s.recv(14, False)
slot1_pyon = s.recv(216, True)
slot2_pyon = s.recv(202, True)
s.recv(14, False)
#!

#!
slot1_id = s.getparameter(slot1_pyon, 'id')
print ('\n' + slot1_id)
#!

#!
slot2_id= s.getparameter(slot2_pyon, 'id')
print ('\n' + slot2_id)
#!

s.close()
codyf86
 
Posts: 3
Joined: Mon Jul 08, 2013 7:25 pm

Return to Completed Scripts

Who is online

Users browsing this forum: No registered users and 0 guests