< Back to Code Samples
# Client object portion of the QA XML-RPC Server
"""
    init
"""

from xmlrpclib import ServerProxy, Fault, ProtocolError
from SimpleXMLRPCServer import *
from threading import Thread
import sys
from pickle import dumps, loads

class QAclient(object):
    
    def __init__ (self, server_uri):
        super(QAclient, self).__init__()
        # Create the server proxy
        self.server_proxy = ServerProxy(server_uri)
            
    def get_survey(self):
        self.questions = loads(self.server_proxy.get_questions())
        self.Response = [0]*len(self.questions)
    
    def do_survey(self):
        #"surv-er"
        for question in self.questions:
            print(str(question.id) + ". " + question.text)
            validchoices = ""
            for choice in question.answer_list:
                print(("[" + choice.label + "]").ljust(10) + "\t" + choice.text)
                validchoices = validchoices + choice.label
            self.Response[question.id-1] = "null"
            correct = 0
            
            while correct == 0:
                self.Response[question.id-1] = raw_input("Your choice:")
                self.Response[question.id-1] = self.Response[question.id-1][:-1]
                for choice in question.answer_list:
                    if self.Response[question.id-1] == choice.label:
                        correct = 1
    
    def submit(self):
        print "That's it! Submitting results..."
        response = dumps(self.Response)
        self.server_proxy.respond(response)
                

if __name__ == "__main__":
    client = QAclient("http://127.0.0.1:8005")
    client.get_survey()
    client.do_survey()
    client.submit()