Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > Python

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-20-2011, 01:01 AM   PM User | #1
renegadeandy
Regular Coder

 
Join Date: Feb 2008
Location: Edinburgh - Scotland
Posts: 107
Thanks: 0
Thanked 12 Times in 12 Posts
renegadeandy is an unknown quantity at this point
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!?
renegadeandy is offline   Reply With Quote
Old 02-20-2011, 04:06 AM   PM User | #2
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
you can't use stdio and stdout, try to use stderr:

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

best regards
oesxyl is offline   Reply With Quote
Old 02-20-2011, 04:12 AM   PM User | #3
renegadeandy
Regular Coder

 
Join Date: Feb 2008
Location: Edinburgh - Scotland
Posts: 107
Thanks: 0
Thanked 12 Times in 12 Posts
renegadeandy is an unknown quantity at this point
I am completely confused by this response?!
renegadeandy is offline   Reply With Quote
Old 02-20-2011, 04:13 AM   PM User | #4
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by renegadeandy View Post
I am completely confused by this response?!
why? what is unclear?

best regards
oesxyl is offline   Reply With Quote
Old 02-20-2011, 12:09 PM   PM User | #5
Kakao
Regular Coder

 
Join Date: Mar 2006
Location: Brasília, Brazil
Posts: 153
Thanks: 0
Thanked 0 Times in 0 Posts
Kakao is on a distinguished road
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?
Kakao is offline   Reply With Quote
Old 02-21-2011, 01:05 AM   PM User | #6
renegadeandy
Regular Coder

 
Join Date: Feb 2008
Location: Edinburgh - Scotland
Posts: 107
Thanks: 0
Thanked 12 Times in 12 Posts
renegadeandy is an unknown quantity at this point
I fixed it - the import statements were wrong
renegadeandy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:48 PM.


Advertisement
Log in to turn off these ads.