Accessing any server using http(s)
import msparser
import sys
import getopt
import ntpath
import time
def ms_range(start, stop, step=1):
i = start
while i <= stop:
yield i
i += step
msurl = ''
msaction = ''
msmgf = ''
mshttpusername = ''
mshttppassword = ''
msusername = ''
mspassword = ''
usage = """
Usage http_client.py --url=<URL> --action=<command> [options]
--help Display this message
--url Base URL, e.g
http://your-server/
--action An action for a get command.
Appended to the base URL
--httpUserName the username for an authenticated web server.
--httpPassword the password for an authenticated web server.
--username Mascot Server user name
May be required if Mascot Security is enabled
--password Mascot Server password
May be required if Mascot Security is enabled
"""
if len(sys.argv) < 2 :
print(usage)
sys.exit(1)
try:
opts, args = getopt.getopt(sys.argv[1:],"",["help","url=","action=","httpUserName=","httpPassword=","username=","password="])
except getopt.GetoptError:
sys.exit(1)
for opt, arg in opts:
if opt == '--help':
print(usage)
sys.exit(2)
elif opt in ("--url"):
msurl = arg
elif opt in ("--action"):
msaction = arg
elif opt in ("--httpUserName"):
mshttpusername = arg
elif opt in ("--httpPassword"):
mshttppassword = arg
elif opt in ("--username"):
msusername = arg
elif opt in ("--password"):
mspassword = arg
objSettings = msparser.ms_connection_settings()
objSettings.setUserAgent("PythonScriptTest/1.0 " + objSettings.getUserAgent())
objSettings.setProxyServerType(msparser.ms_connection_settings.PROXY_TYPE_AUTO)
if mshttpusername:
objSettings.setHttpUsername(mshttpusername)
if mshttppassword:
objSettings.setHttpPassword(mshttppassword)
httpHelper = msparser.ms_http_helper(msurl, objSettings)
if not httpHelper.isValid():
showErrorsAndExit(httpHelper)
errCodeString, retString = httpHelper.httpGetString(msaction)
if errCodeString.isOk():
print("httpGetString OK : " + retString)
else:
print("Error: " + errCodeString.getErrorText())
errCodeHeader, retHeader = httpHelper.httpGetHeader(msaction)
if errCodeHeader.isOk():
print("httpGetHeader OK : " + retHeader)
else:
print("Error: " + errCodeHeader.getErrorText())
errCodeBufferOpen = httpHelper.httpBufferedOpen(msaction)
if errCodeBufferOpen.isOk():
print("httpBufferedOpen OK : ")
while True:
errCodeBufferString, retBufferString = httpHelper.httpBufferedGetString(10000)
if len(retBufferString) == 0 or not errCodeBufferString.isOk():
break
print(retBufferString)
httpHelper.httpBufferedClose()
def showErrorsAndExit(ms_errs):
print("Error: %s" % ms_errs.getLastErrorString())
errs = ms_errs.getErrorHandler()
for i in ms_range(1, errs.getNumberOfErrors()):
print("Error Number: " + str(errs.getErrorNumber(i)) + " (" + str(errs.getErrorRepeats(i + 1)) + " times :")
sys.exit(1)