Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. import json
  2. import responses
  3. import requests
  4. import threading
  5.  
  6. try:
  7.    import urlparse
  8. except ModuleNotFoundError:
  9.    import urllib.parse as urlparse
  10.  
  11.  
  12. server_url = 'http://server'
  13. headers_json = {'content-type': 'application/json'}
  14.  
  15. def init():
  16.  
  17.     def endpoint(request):
  18.         id = urlparse.parse_qs(urlparse.urlparse(request.path_url).query)["id"][0]
  19.         data = {"foo": int(id)}
  20.         return 200, headers_json, json.dumps(data)
  21.  
  22.     responses.add_callback(
  23.         responses.GET, server_url + '/',
  24.         callback=endpoint,
  25.     )
  26.  
  27. def responses_routine():
  28.     resp = requests.get(
  29.         server_url + '/?id=456',
  30.         headers=headers_json,
  31.     )
  32.  
  33.      # {"foo": "456"}
  34.     print(resp.json()["foo"])
  35.  
  36.  
  37. t = threading.Timer(2, responses_routine)
  38.  
  39. @responses.activate
  40. def test():
  41.  
  42.     init()
  43.     t.start()
  44.  
  45. test()
  46.  
  47. import time
  48. time.sleep(5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement