[SOLVED] python3 get informix error

huky

Renowned Member
Jul 1, 2016
70
3
73
44
Chongqing, China
I want get some informix from pve with python3.(i am new)

Code:
url = self.urlBase + 'access/ticket'
 params = {'username':  userName, 'password':  passWord}
data = bytes(urllib.parse.urlencode(params), encoding='utf8')
 response = urllib.request.urlopen(url, data=data) 
 fields = json.loads(response.read().decode('utf-8'))
 self.TICKET = fields['data']['ticket']
the worked and I can get the ticket

but when I try to get nodes

Code:
 url = self.urlBase + 'nodes'
 params = {'PVEAuthCookie': self.TICKET}
data = bytes(urllib.parse.urlencode(params), encoding='utf8')
 response = urllib.request.urlopen(url, data=data)
fields = json.loads(response.read().decode('utf-8'))

I got error:

Code:
    response = urllib.request.urlopen(url, data=data)
  File "/usr/lib/python3.5/urllib/request.py", line 163, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.5/urllib/request.py", line 472, in open
    response = meth(req, response)
  File "/usr/lib/python3.5/urllib/request.py", line 582, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python3.5/urllib/request.py", line 510, in error
    return self._call_chain(*args)
  File "/usr/lib/python3.5/urllib/request.py", line 444, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.5/urllib/request.py", line 590, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 401: No ticket

i can print ticket in the second, the only difference is params = {}.
 
you have to set the ticket not as parameter but as a cookie
thank you
I have solved it.

get informix of vm or ct:

Code:
    def getInfo(self, urlExt, nodeName=None, vType=None, vId=None):

        url = self.urlBase + urlExt
        headers = {"Accept": "application/json", "Cookie": "PVEAuthCookie=%s" % self.TICKET}
        request = urllib.request.Request(url)
        for k,v in headers.items():
            request.add_header(k, v)
        response = urllib.request.urlopen(request)
        fields = json.loads(response.read().decode('utf-8'))
        return fields['data']

as your said, add cookies into headers.