aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlice Ferrazzi <alicef@gentoo.org>2017-06-17 03:56:12 +0000
committerAlice Ferrazzi <alicef@gentoo.org>2017-06-17 03:56:12 +0000
commit0a7a6e1a1dad7b5f2948c416eee8c5f14de74fde (patch)
treede3669b7a7f37986b5e78c04a51e12d97b2fbe81 /elivepatch_client/client/restful.py
parentadd restful client communication and url option to argparse (diff)
downloadelivepatch-0a7a6e1a1dad7b5f2948c416eee8c5f14de74fde.tar.gz
elivepatch-0a7a6e1a1dad7b5f2948c416eee8c5f14de74fde.tar.bz2
elivepatch-0a7a6e1a1dad7b5f2948c416eee8c5f14de74fde.zip
added restful client module
Diffstat (limited to 'elivepatch_client/client/restful.py')
-rw-r--r--elivepatch_client/client/restful.py41
1 files changed, 36 insertions, 5 deletions
diff --git a/elivepatch_client/client/restful.py b/elivepatch_client/client/restful.py
index b5cf5e6..0e5fade 100644
--- a/elivepatch_client/client/restful.py
+++ b/elivepatch_client/client/restful.py
@@ -1,16 +1,47 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
-import json
-import urllib2
+import json, base64
+try:
+ import urllib
+except:
+ import urllib.request as urllib
import requests
class ManaGer(object):
- def __init__(self,server_url):
+ def __init__(self, server_url):
self.server_url = server_url
+ self.version()
def version(self):
- version = json.load(urllib2.urlopen(self.server_url))
- print(version)
+
+ # If you access to url below via Proxy,
+ # set environment variable 'http_proxy' before execute this.
+ # And, url scheme is https, then 'https_proxy' must be set instead of 'http_proxy'
+ url = self.server_url + '/elivepatch/api/v1.0/agent'
+
+ # https://docs.python.org/3/library/functions.html#input
+ # https://docs.python.org/3/library/getpass.html
+ auth_user='elivepatch'
+ auth_passwd='default'
+
+ # https://docs.python.org/3.4/howto/urllib2.html#id5
+ #
+ # If you would like to request Authorization header for Digest Authentication,
+ # replace HTTPBasicAuthHandler object to HTTPDigestAuthHandler
+ passman = urllib.request.HTTPPasswordMgrWithDefaultRealm()
+ passman.add_password(None, url, auth_user, auth_passwd)
+ authhandler = urllib.request.HTTPBasicAuthHandler(passman)
+ opener = urllib.request.build_opener(authhandler)
+ urllib.request.install_opener(opener)
+
+ # I can get http.client.HTTPResponse object in variable 'res'
+ # https://docs.python.org/3/library/http.client.html#httpresponse-objects
+ #
+ # ToDo: Error Handling
+ # https://docs.python.org/3/howto/urllib2.html#handling-exceptions
+ res = urllib.request.urlopen(url)
+ res_body = res.read()
+ print(res_body.decode('utf-8'))
def send_config(self, config_path, config_file):
url = self.server_url