Unbreakable Linux Network APIs example

I posted a short blog entry about the recently released ULN APIs the other day with a sample of how to call the different APIs. Here is a concrete example to use the API to find a package in a channel and download it.

$ ./ulnget.py kernel-headers.2.6.32-71.29 ol6_x86_64_latest
Searching for 'kernel-headers.2.6.32-71.29' in channel 'ol6_x86_64_latest'

Logging in...
Logged in...
Retrieving all packages...
Found kernel-headers.2.6.32-71.29.1.el6
Getting package details...
Downloading https://uln.oracle.com/XMLRPC/GET-REQ/ol6_x86_64_latest/
kernel-headers-2.6.32-71.29.1.el6.x86_64.rpm...

Logged out...

The code for the above is pasted below, this is just a very simplistic example...

#!/usr/bin/python
try:
    import os
    import sys
    import getpass
    import datetime
    import xmlrpclib

except ImportError, e:
    raise ImportError (str(e) + ': Module  not found')

if len(sys.argv) != 3:
   print "Usage : ulnget.py [packagename] [channelname]"
   exit(1) 

search = str(sys.argv[1])
channelLabel = str(sys.argv[2])

print "Searching for '%s' in channel '%s'" % (search, channelLabel)

SERVER_URL = 'https://linux-update.oracle.com/rpc/api'

USERNAME = 'username'
PASSWORD = 'password'

# channelLabel = 'ol6_x86_64_latest'

client = xmlrpclib.Server(SERVER_URL)

print ""

# login
print "Logging in..."
sessionKey = client.auth.login(USERNAME,PASSWORD)
if len(sessionKey) != 43:
   print "Invalid %d sessionKey : '%s'" % sessionKey
   exit(1)

print "Logged in..." 

print "Retrieving all packages..."
packageList = client.channel.software.listAllPackages(sessionKey, channelLabel)

for package in packageList:
   packageName = '%s.%s-%s' % (package['package_name'],package['package_version']
      ,package['package_release'])
   if search in packageName:
      print "Found %s" % packageName
      pid = package['package_id']
      print "Getting package details..."
      packageDetail = client.packages.getDetails(sessionKey, pid)
      url = packageDetail['download_urls'][0]
      import  urllib2
      req = urllib2.Request(url,headers={'X-ULN-API-User-Key': sessionKey})
      try:
          print "Downloading %s..." %url
          response = urllib2.urlopen(req)
          contents = response.read()
      except urllib2.HTTPError, e:
          print
          print "HTTP error code  : %d" %e.code
      except Exception, e:
          print
          print str(e)

print ""

retval = client.auth.logout(sessionKey)
if retval == 1:
  print "Logged out..."
else:
  print "Failed to log out..."
Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Wim Coekaerts is the Senior Vice President of Linux and Virtualization Engineering for Oracle. He is responsible for Oracle's complete desktop to data center virtualization product line and the Oracle Linux support program.

You can follow him on Twitter at @wimcoekaerts

Search

Categories
Archives
« May 2015
SunMonTueWedThuFriSat
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
      
Today