If your purpose is really to make connect
a @staticmethod
, then initialize myhostname
,myport
, myuser
, and mypassword
at the class level, like in:
class MyConnection:
myhostname= hostnameValue
myport= portValue
myuser= userValue
mypassword= passwordValue
@staticmethod
def connect():
my_session = MyConnection()
headers = {'content-type': 'application/json'}
headers['Authorization'] = 'Basic ' + string.strip( base64.encodestring( MyConnection.myuser + ':' + MyConnection.mypassword ) )
body = json.dumps({'username': MyConnection.myuser, 'password': MyConnection.mypassword,'services': ['platform', 'namespace']})
my_session.connection = httplib.HTTPSConnection(MyConnection.myhostname, MyConnection.myport)
my_session.connection.connect()
MyConnection.connect()
Alternatively, you can leave them in None
and give them a value before calling connect()
.
If you want to make connect
an instance method, then you are pretty much there. You just need to remove decorator @staticmethod
, and do a few other changes:
class MyConnection:
def __init__(self, hostname, port, user, password):
self.myhostname = hostname
self.myport = port
self.myuser = user
self.mypassword = password
def connect():
headers = {'content-type': 'application/json'}
headers['Authorization'] = 'Basic ' + string.strip( base64.encodestring(self.myuser + ':' + self.mypassword) )
body = json.dumps({'username': self.myuser, 'password': self.mypassword, 'services': ['platform', 'namespace']})
connection = httplib.HTTPSConnection(self.myhostname, self.myport)
connection.connect()
my_session= MyConnection(hostnameValue,portValue,userValue,passwordValue)
my_session.connect()