Advertisement
Guest User

Untitled

a guest
Aug 5th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. from requests.auth import HTTPDigestAuth
  4. import requests
  5. import json
  6.  
  7.  
  8. headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
  9. payload = '{"operation": "read-attribute", "name": "server-state", "json.pretty": 1}'
  10.  
  11. for x in range(0, 100):
  12.     try:    
  13.         response = requests.post('http://localhost:9990/management', data = payload,  headers = headers)
  14.         data = json.loads(json.dumps(response.json()))
  15.         print '%i - %s'  %(x,data['result'])
  16.  
  17.     except Exception as inst:
  18.         print(inst.args)
  19.         print(inst)
  20.  
  21.  
  22. ==========================================
  23.  
  24.  
  25. #!/usr/bin/ruby
  26.  
  27. require 'uri'
  28. require 'json'
  29. require 'net/http'
  30. require 'net/http/digest_auth'
  31.  
  32. digest_auth = Net::HTTP::DigestAuth.new
  33.  
  34. uri = URI.parse 'http://localhost:9990/management'
  35. uri.user = 'test'
  36. uri.password = 'test'
  37.  
  38. headers = {'Content-Type' => "application/json", 'Accept' => "text/plain" }
  39. params = '{"operation":"read-attribute","name":"server-state","json.pretty":1}'
  40.  
  41. h = Net::HTTP.new uri.host, uri.port
  42. #h.set_debug_output $stderr
  43.  
  44. for i in 0..100
  45.     #returns 401
  46.     req = Net::HTTP::Post.new uri.request_uri, headers
  47.     res = h.request req
  48.  
  49.     #issung the request again with the auth
  50.     digest_auth = Net::HTTP::DigestAuth.new
  51.     auth = digest_auth.auth_header uri, res['www-authenticate'], 'POST'
  52.  
  53.     req = Net::HTTP::Post.new uri.request_uri, headers
  54.     req.add_field 'Authorization', auth
  55.     req.body = params
  56.     res = h.request req
  57.  
  58.     parsed_json = JSON.parse(res.body)
  59.     puts "#{i} - " + parsed_json['result']
  60. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement