[更新]: 完全なコード
私は常にpyhtonのstaticmethodsと混同しますが、これ(最後の回答)によると、うまくいくはずです!
取得エラー:
AttributeError: クラス MyConnection に属性 'myuser' がありません
class MyConnection:
def __init__(self, hostname, port, user, password):
myhostname = hostname
myport = port
myuser = user
mypassword = password
isisessid = None
@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']})
uri = '/session/1/session'
connection = httplib.HTTPSConnection(MyConnection.myhostname, MyConnection.myport)
connection.connect()
try:
connection.request('POST', uri, body, headers)
response = connection.getresponse()
my_session.isisessid = MyConnection.extract_session_id(
response.getheaders())
except Exception, e:
print e
connection.close()
except httplib.BadStatusLine, e:
print e
connection.close()
return my_session