Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts

    Python simple class and methods

    Hi Guys ,

    I have a class with 2 methods, and a test class I want to run in order to test this class.

    The class looks as follows:

    Code:
    import httplib;
    import urllib;
    
    class FormHelper:
        
        def __init__(self):
            return;
        
        '''
        This method can be used to submit a form as part of an availibility check.
        Pass it the host, path of the POST request and a set of params in the form 'spam':2,'param2':"lol" and it will
        return you the data response
        '''
        
        def submitForm(self,host,path,params ):
            params = urllib.urlencode({'spam': "1", 'eggs': 2, 'bacon': 0});
            headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"};
            conn = httplib.HTTPConnection(host+":80");
            conn.request("POST", path, params, headers);
            response = conn.getresponse();
            print response.status, response.reason;
            data = response.read();
            conn.close();
            return data;
        
        '''
        This method can be used to grab a page as part of any activity
        Pass it the host, path of the URL you want and it will return you the page's content
        '''
            
        def getPage(self,host,path):
            conn = httplib.HTTPConnection(host)
            conn.request("GET", path)
            r1 = conn.getresponse()
            print r1.status, r1.reason
            data1 = r1.read()
            conn.close()
            return data1;
    The test looks as follows:

    Code:
    from UtilityCode import FormHelper
    
    if __name__ == '__main__':
        formHelper = FormHelper();
        data = formHelper
        print data;
    Now what im trying to do in the test code is data = formHelper.getPage("firsparm","secondparm");
    but it wont let me - what am I doing wrong!?

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    you can't use stdio and stdout, try to use stderr:

    http://docs.python.org/release/2.6.6...ng-cgi-scripts

    best regards

  • #3
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    I am completely confused by this response?!

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by renegadeandy View Post
    I am completely confused by this response?!
    why? what is unclear?

    best regards

  • #5
    Regular Coder
    Join Date
    Mar 2006
    Location
    Brasília, Brazil
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by renegadeandy View Post
    Hi Guys ,
    Now what im trying to do in the test code is data = formHelper.getPage("firsparm","secondparm");
    but it wont let me - what am I doing wrong!?
    What do you mean by "it wont let me" ? Any error message or what?

  • #6
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    I fixed it - the import statements were wrong


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •