Skip to content

Instantly share code, notes, and snippets.

@jdiverp
Created May 16, 2023 03:45
Show Gist options
  • Select an option

  • Save jdiverp/e357aefe9ee2299492b33944b3aca4a6 to your computer and use it in GitHub Desktop.

Select an option

Save jdiverp/e357aefe9ee2299492b33944b3aca4a6 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import argparse
import httplib
import sys
import time
import json
NWS_HOST = 'uwnetid.washington.edu'
NWS_PORT = '443'
NWS_BASE = '/nws/v1'
KEY_FILE = ''
CERT_FILE = ''
#httplib.HTTPConnection.debuglevel = 9
connection = httplib.HTTPSConnection(NWS_HOST, NWS_PORT, KEY_FILE, CERT_FILE)
NWS_HEAD = { "Content-type": "application/json", "Accept": "application/json"}
METHOD = 'PUT'
URL = ''
parser = argparse.ArgumentParser(prog='nws_set-forwarding.py')
parser.add_argument('uwnetid',help='UW NetID to modify')
parser.add_argument('forwarding',help='UW NetID to modify')
parser.add_argument('--sub',help='Forwarding subscription code to set', default='105')
parser.add_argument('--debug',help='show debuging timing information', action='store_true')
parser.add_argument('--eval',help="use eval IRWS/NWS", action='store_true')
args = parser.parse_args()
sub = args.sub
forwarding= args.forwarding
uwnetid = args.uwnetid
t_ = time.clock()
if args.eval:
NWS_HOST = 'netid20.s.uw.edu'
NWS_PORT = '443'
NWS_BASE = '/nws/v1-eval'
REQUEST = URL + NWS_BASE + '/uwnetid/%s/forwarding/%s' % (uwnetid, sub)
DATA = '{ "address": "%s" }' % (forwarding)
if args.debug:
print 'https://'+NWS_HOST+":"+ str(NWS_PORT) +REQUEST
connection.request(METHOD, REQUEST, DATA, NWS_HEAD)
response = connection.getresponse()
code = response.status
if args.debug:
print 'Response: ' + str(code)
print 'Response time: ' + str(time.clock() - t_)
body = response.read()
if args.debug:
print 'Read time: ' + str(time.clock() - t_)
serviceData = json.loads(body)
if args.debug:
print 'Parse time: ' + str(time.clock() - t_)
if code == 200:
print "%s: set" % ( uwnetid )
else:
print "%s: forwarding=%s status=%s" % (uwnetid, forwarding, str(code))
print serviceData
if args.debug:
print 'Print time: ' + str(time.clock() - t_)
connection.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment