Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. import requests
  2. import time
  3.  
  4. gets = [
  5.     {
  6.         "name": "Get chain",
  7.         "url": "http://127.0.0.1/get_chain",
  8.         "params": []
  9.     },
  10. ]
  11.  
  12. while True:
  13.     for g in gets:
  14.         r = get(g['url'], g['params'])
  15.         if r.status_code == 200:
  16.             r = r.json()
  17.             print(str(g['name']) + ' 200')
  18.             print(r)
  19.             print('')
  20.         else:
  21.             print(str(g['name']) + ' ' + r.status_code)
  22.     time.sleep(1)
  23.  
  24. # To access values from these functions:
  25. # r.json() returns JSON response from request
  26. # r.status_code gives you the http code from the response
  27.  
  28. #
  29. # For params, pass an array of dictionaries
  30. # e.g.
  31. # [ { "name": "name", "value": "value" } ]
  32. #
  33. def get(url, params):
  34.     if len(params) > 0:
  35.         url += '?'
  36.         for param in params:
  37.             url = url + str(param['name']) + '=' + str(param['value'])
  38.  
  39.     headers = {
  40.         'accept': 'application/json',
  41.     }
  42.  
  43.     r = requests.get(url, headers=headers)
  44.  
  45.     return r
  46.  
  47. #
  48. # For params, pass the data you want to send in the POSt request
  49. # e.g.
  50. # [ { "name": "value", "name2": "value2" } ]
  51. #
  52. def post(url, params):
  53.  
  54.     headers = {
  55.         'accept': 'application/json',
  56.     }
  57.  
  58.     r = requests.post(url, data=params, headers=headers)
  59.  
  60.     return r
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement