Advertisement
Guest User

Untitled

a guest
Mar 8th, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.58 KB | None | 0 0
  1. # Kudos to Stack Overflow for the XML and Python interaction tutorials
  2. # Also kudos to Google for pointing to requests library
  3.  
  4. #!/usr/bin/env python2
  5. # -*- coding: ASCII -*-
  6. import requests
  7. import random
  8. import time
  9. import uuid
  10. import sys
  11.  
  12. username = 'haha'
  13. password = 'hoho'
  14. request_id = 'oh hi mark'
  15. slot_id = '0'
  16. headers = {'Content-Type': 'application/xml', 'Accept' : 'application/xml'} # set what server accepts
  17. xml = ''
  18. recieved = ''
  19. recieved_get = ''
  20. debug = False
  21. ######################################################################
  22. # Form the XML structures
  23. def form_reserve_xml(request_id, username, password, slot_id):
  24.     global xml
  25.  
  26.     # Form the XML structure for the reserve request
  27.     xml = """<?xml version='1.0' encoding='ASCII'?>
  28.    <reserve>
  29.        <request_id>""" + request_id + """</request_id>
  30.        <username>""" + username + """</username>
  31.        <password>""" + password + """</password>
  32.        <slot_id>""" + slot_id + """</slot_id>
  33.    </reserve>"""
  34.  
  35.     return xml
  36.  
  37. def form_cancel_xml(request_id, username, password, slot_id):
  38.     global xml
  39.  
  40.     # Form the XML structure for the cancel request
  41.     xml = """<?xml version='1.0' encoding='ASCII'?>
  42.    <cancel>
  43.        <request_id>""" + request_id + """</request_id>
  44.        <username>""" + username + """</username>
  45.        <password>""" + password + """</password>
  46.        <slot_id>""" + slot_id + """</slot_id>
  47.    </cancel>"""
  48.  
  49.     return xml
  50.  
  51. def form_available_xml(request_id, username, password):
  52.     global xml
  53.    
  54.     # Form the XML structure for the check availability request
  55.     xml_check_available = """<?xml version='1.0' encoding='utf-8'?>
  56.    <availability>
  57.        <request_id>""" + request_id + """</request_id>
  58.        <username>""" + username + """</username>
  59.        <password>""" + password + """</password>
  60.    </availability>"""
  61.    
  62.     return xml
  63.  
  64. def form_bookings_xml(request_id, username, password):
  65.     global xml
  66.     # Form the XML structure for the check bookings request
  67.     xml_check_bookings = """<?xml version='1.0' encoding='utf-8'?>
  68.    <bookings >
  69.        <request_id>""" + request_id + """</request_id>
  70.        <username>""" + username + """</username>
  71.        <password>""" + password + """</password>
  72.    </bookings >"""
  73.     return xml
  74.  
  75. ######################################################################
  76. # Reserve a slot
  77. def reserve(username, password, request_id):
  78.     global debug
  79.     url = 'http://jewel.cs.man.ac.uk:3010/queue/enqueue'
  80.  
  81.     print("\nWhich slot would you want to reserve? ")
  82.     slot_id = raw_input()
  83.  
  84.     if slot_id == 'exit':
  85.         return
  86.  
  87.     if debug:
  88.         printStats()
  89.  
  90.     print("\nThe slot you want to reserve is " + slot_id )
  91.  
  92.     form_reserve_xml(request_id, username, password, slot_id)
  93.     send_request(url)
  94.  
  95.     time.sleep(2)
  96.  
  97.     # Check the reservation
  98.     recieved_url = recieved.strip('<msg_uri>').strip('</msg_uri>')
  99.     get_response = recieved_url + '?username=' + username + '&password=' + password
  100.  
  101.     #print 'Merged server response URL with username and password: ' + get_response + '\n'
  102.     print '\nChecking status of reservation...'
  103.  
  104.     response_waiting(get_response)
  105.     print '\nResponse that is recieved from the server: \n' + recieved_get
  106.  
  107. #############################################################################
  108. # Cancel a slot
  109. def cancel(username, password, request_id):
  110.     global debug
  111.     url = 'http://jewel.cs.man.ac.uk:3010/queue/enqueue'
  112.  
  113.     print("\nWhich slot would you like to cancel? ")
  114.     slot_id = raw_input()
  115.  
  116.     if slot_id == 'exit':
  117.         return
  118.  
  119.     if debug:
  120.         printStats()
  121.  
  122.     print("\nThe slot you want to cancel is " + slot_id )
  123.  
  124.     form_cancel_xml(request_id, username, password, slot_id)
  125.     send_request(url)
  126.  
  127.     # Check the cancelation
  128.     recieved_url = recieved.strip('<msg_uri>').strip('</msg_uri>')
  129.  
  130.     get_response = recieved_url + '?username=' + username + '&password=' + password
  131.  
  132.     #print 'Merged server response URL with username and password: ' + get_response + '\n'
  133.     print '\nChecking status of cancellation...'
  134.     response_waiting(get_response)
  135.  
  136.     print '\nResponse that is recieved from the server: \n' + recieved_get
  137.  
  138. #############################################################################
  139. # Check bookings
  140. def check_bookings(username, password, request_id):
  141.     global debug
  142.     url = 'http://jewel.cs.man.ac.uk:3010/booking/'
  143.     print("\nChecking bookings for you..")
  144.  
  145.     if debug:
  146.         printStats()
  147.  
  148.     form_bookings_xml(request_id, username, password)
  149.  
  150.     recieved_bookings = requests.put(url, data=xml, headers=headers).text
  151.     time.sleep(2)
  152.     while recieved_bookings == 'Service Unavailable':
  153.         print 'Service unavailable, retrying...'
  154.         time.sleep(2)
  155.         recieved_bookings = requests.put(url, data=xml, headers=headers).text
  156.  
  157.     # Check the bookings
  158.     recieved_bookings_url = recieved_bookings.strip('<msg_uri>').strip('</msg_uri>')
  159.     print recieved_bookings_url
  160.  
  161. #############################################################################
  162. # Check available slots
  163. def check_available(username, password, request_id):
  164.     global debug
  165.     url = 'http://jewel.cs.man.ac.uk:3010/booking/available'
  166.     print("\nChecking availability...")
  167.  
  168.     if debug:
  169.         printStats()
  170.  
  171.     form_available_xml(request_id, username, password)
  172.     send_request(url)
  173.     print recieved
  174. #############################################################################
  175. # Other stuff
  176.  
  177. def printStats():
  178.     print("\nYour username is " + username)
  179.     print("Your password is " + password)
  180.     print("Generated request ID: " + request_id)
  181.     return
  182.  
  183. def send_request(url):
  184.     global recieved, xml, headers
  185.     # Reserve a slot
  186.     recieved = requests.put(url, data=xml, headers=headers).text
  187.  
  188.     print 'Request sent...'
  189.     print_warning = True
  190.  
  191.     while recieved == 'Service Unavailable':
  192.  
  193.         if print_warning:
  194.             print '\nService unavailable, retrying...'
  195.  
  196.         print_warning = False
  197.         time.sleep(2)
  198.         recieved = requests.put(url, data=xml, headers=headers).text
  199.  
  200. def response_waiting(get_response):
  201.     global recieved_get
  202.     print 'Waiting for response, might take a while...'
  203.     recieved_get = requests.get(get_response).text
  204.     time.sleep(2)
  205.  
  206.     while recieved_get == 'Message unavailable' or recieved_get == 'Service unavailable':
  207.         time.sleep(2)
  208.         recieved_get = requests.get(get_response).text
  209.  
  210. def print_menu():
  211.     global debug
  212.     print '\n##################################################'
  213.     if debug:
  214.         print 'Debugging mode on\n'
  215.     print 'What do you want to do?\n'
  216.     print '-(R)eserve a slot'
  217.     print '-(C)ancel a slot'
  218.     print '-(B)ookings check'
  219.     print '-(A)vailability check'
  220.     print '\nOr exit?'
  221.  
  222. #############################################################################
  223. while True:
  224.     request_id = str(uuid.uuid4())
  225.     print_menu()
  226.     choice = raw_input()
  227.  
  228.     if choice == 'R' or choice == 'r':
  229.         reserve(username, password, request_id)
  230.  
  231.     elif choice == 'C' or choice == 'c':
  232.         cancel(username, password, request_id)
  233.  
  234.     elif choice == 'B' or choice == 'b':
  235.         check_bookings(username, password, request_id)
  236.  
  237.     elif choice == 'A' or choice == 'a':
  238.         check_available(username, password, request_id)
  239.  
  240.     elif choice == 'd':
  241.         if debug:
  242.             debug = False
  243.         else:
  244.             debug = True
  245.  
  246.     elif choice == 'exit':
  247.         print '\nThis client WILL become better! c:'
  248.         sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement