Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Kudos to Stack Overflow for the XML and Python interaction tutorials
- # Also kudos to Google for pointing to requests library
- #!/usr/bin/env python2
- # -*- coding: ASCII -*-
- import requests
- import random
- import time
- import uuid
- import sys
- username = 'haha'
- password = 'hoho'
- request_id = 'oh hi mark'
- slot_id = '0'
- headers = {'Content-Type': 'application/xml', 'Accept' : 'application/xml'} # set what server accepts
- xml = ''
- recieved = ''
- recieved_get = ''
- debug = False
- ######################################################################
- # Form the XML structures
- def form_reserve_xml(request_id, username, password, slot_id):
- global xml
- # Form the XML structure for the reserve request
- xml = """<?xml version='1.0' encoding='ASCII'?>
- <reserve>
- <request_id>""" + request_id + """</request_id>
- <username>""" + username + """</username>
- <password>""" + password + """</password>
- <slot_id>""" + slot_id + """</slot_id>
- </reserve>"""
- return xml
- def form_cancel_xml(request_id, username, password, slot_id):
- global xml
- # Form the XML structure for the cancel request
- xml = """<?xml version='1.0' encoding='ASCII'?>
- <cancel>
- <request_id>""" + request_id + """</request_id>
- <username>""" + username + """</username>
- <password>""" + password + """</password>
- <slot_id>""" + slot_id + """</slot_id>
- </cancel>"""
- return xml
- def form_available_xml(request_id, username, password):
- global xml
- # Form the XML structure for the check availability request
- xml_check_available = """<?xml version='1.0' encoding='utf-8'?>
- <availability>
- <request_id>""" + request_id + """</request_id>
- <username>""" + username + """</username>
- <password>""" + password + """</password>
- </availability>"""
- return xml
- def form_bookings_xml(request_id, username, password):
- global xml
- # Form the XML structure for the check bookings request
- xml_check_bookings = """<?xml version='1.0' encoding='utf-8'?>
- <bookings >
- <request_id>""" + request_id + """</request_id>
- <username>""" + username + """</username>
- <password>""" + password + """</password>
- </bookings >"""
- return xml
- ######################################################################
- # Reserve a slot
- def reserve(username, password, request_id):
- global debug
- url = 'http://jewel.cs.man.ac.uk:3010/queue/enqueue'
- print("\nWhich slot would you want to reserve? ")
- slot_id = raw_input()
- if slot_id == 'exit':
- return
- if debug:
- printStats()
- print("\nThe slot you want to reserve is " + slot_id )
- form_reserve_xml(request_id, username, password, slot_id)
- send_request(url)
- time.sleep(2)
- # Check the reservation
- recieved_url = recieved.strip('<msg_uri>').strip('</msg_uri>')
- get_response = recieved_url + '?username=' + username + '&password=' + password
- #print 'Merged server response URL with username and password: ' + get_response + '\n'
- print '\nChecking status of reservation...'
- response_waiting(get_response)
- print '\nResponse that is recieved from the server: \n' + recieved_get
- #############################################################################
- # Cancel a slot
- def cancel(username, password, request_id):
- global debug
- url = 'http://jewel.cs.man.ac.uk:3010/queue/enqueue'
- print("\nWhich slot would you like to cancel? ")
- slot_id = raw_input()
- if slot_id == 'exit':
- return
- if debug:
- printStats()
- print("\nThe slot you want to cancel is " + slot_id )
- form_cancel_xml(request_id, username, password, slot_id)
- send_request(url)
- # Check the cancelation
- recieved_url = recieved.strip('<msg_uri>').strip('</msg_uri>')
- get_response = recieved_url + '?username=' + username + '&password=' + password
- #print 'Merged server response URL with username and password: ' + get_response + '\n'
- print '\nChecking status of cancellation...'
- response_waiting(get_response)
- print '\nResponse that is recieved from the server: \n' + recieved_get
- #############################################################################
- # Check bookings
- def check_bookings(username, password, request_id):
- global debug
- url = 'http://jewel.cs.man.ac.uk:3010/booking/'
- print("\nChecking bookings for you..")
- if debug:
- printStats()
- form_bookings_xml(request_id, username, password)
- recieved_bookings = requests.put(url, data=xml, headers=headers).text
- time.sleep(2)
- while recieved_bookings == 'Service Unavailable':
- print 'Service unavailable, retrying...'
- time.sleep(2)
- recieved_bookings = requests.put(url, data=xml, headers=headers).text
- # Check the bookings
- recieved_bookings_url = recieved_bookings.strip('<msg_uri>').strip('</msg_uri>')
- print recieved_bookings_url
- #############################################################################
- # Check available slots
- def check_available(username, password, request_id):
- global debug
- url = 'http://jewel.cs.man.ac.uk:3010/booking/available'
- print("\nChecking availability...")
- if debug:
- printStats()
- form_available_xml(request_id, username, password)
- send_request(url)
- print recieved
- #############################################################################
- # Other stuff
- def printStats():
- print("\nYour username is " + username)
- print("Your password is " + password)
- print("Generated request ID: " + request_id)
- return
- def send_request(url):
- global recieved, xml, headers
- # Reserve a slot
- recieved = requests.put(url, data=xml, headers=headers).text
- print 'Request sent...'
- print_warning = True
- while recieved == 'Service Unavailable':
- if print_warning:
- print '\nService unavailable, retrying...'
- print_warning = False
- time.sleep(2)
- recieved = requests.put(url, data=xml, headers=headers).text
- def response_waiting(get_response):
- global recieved_get
- print 'Waiting for response, might take a while...'
- recieved_get = requests.get(get_response).text
- time.sleep(2)
- while recieved_get == 'Message unavailable' or recieved_get == 'Service unavailable':
- time.sleep(2)
- recieved_get = requests.get(get_response).text
- def print_menu():
- global debug
- print '\n##################################################'
- if debug:
- print 'Debugging mode on\n'
- print 'What do you want to do?\n'
- print '-(R)eserve a slot'
- print '-(C)ancel a slot'
- print '-(B)ookings check'
- print '-(A)vailability check'
- print '\nOr exit?'
- #############################################################################
- while True:
- request_id = str(uuid.uuid4())
- print_menu()
- choice = raw_input()
- if choice == 'R' or choice == 'r':
- reserve(username, password, request_id)
- elif choice == 'C' or choice == 'c':
- cancel(username, password, request_id)
- elif choice == 'B' or choice == 'b':
- check_bookings(username, password, request_id)
- elif choice == 'A' or choice == 'a':
- check_available(username, password, request_id)
- elif choice == 'd':
- if debug:
- debug = False
- else:
- debug = True
- elif choice == 'exit':
- print '\nThis client WILL become better! c:'
- sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement