...

Python simple class and methods

renegadeandy
02-20-2011, 01:01 AM
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:


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:


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!?

oesxyl
02-20-2011, 04:06 AM
you can't use stdio and stdout, try to use stderr:

http://docs.python.org/release/2.6.6/library/cgi.html#debugging-cgi-scripts

best regards

renegadeandy
02-20-2011, 04:12 AM
I am completely confused by this response?!

oesxyl
02-20-2011, 04:13 AM
I am completely confused by this response?!
why? what is unclear?

best regards

Kakao
02-20-2011, 12:09 PM
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?

renegadeandy
02-21-2011, 01:05 AM
I fixed it - the import statements were wrong :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum